simpler api

This commit is contained in:
Martin Algesten
2018-09-04 10:59:10 +02:00
parent 38168e30cc
commit b16b14c6aa
4 changed files with 51 additions and 245 deletions

View File

@@ -112,53 +112,15 @@ impl Agent {
/// println!("Oh no error!"); /// println!("Oh no error!");
/// } /// }
/// ``` /// ```
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Agent pub fn set(&mut self, header: &str, value: &str) -> &mut Agent
where
K: Into<String>,
V: Into<String>,
{ {
add_header( add_header(
&mut self.headers, &mut self.headers,
Header::new(&header.into(), &value.into()), Header::new(header, value),
); );
self self
} }
/// Set many headers that will be present in all requests using the agent.
///
/// ```
/// #[macro_use]
/// extern crate ureq;
///
/// fn main() {
/// let agent = ureq::agent()
/// .set_map(map! {
/// "X-API-Key" => "foobar",
/// "Accept" => "text/plain"
/// })
/// .build();
///
/// let r = agent
/// .get("/my_page")
/// .call();
///
/// if r.ok() {
/// println!("yay got {}", r.into_string().unwrap());
/// }
/// }
/// ```
pub fn set_map<K, V, I>(&mut self, headers: I) -> &mut Agent
where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
{
for (k, v) in headers.into_iter() {
self.set(k, v);
}
self
}
/// Basic auth that will be present in all requests using the agent. /// Basic auth that will be present in all requests using the agent.
/// ///
/// ``` /// ```
@@ -171,15 +133,10 @@ impl Agent {
/// .call(); /// .call();
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Agent pub fn auth(&mut self, user: &str, pass: &str) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
{ {
let u = user.into(); let pass = basic_auth(user, pass);
let p = pass.into(); self.auth_kind("Basic", &pass)
let pass = basic_auth(&u, &p);
self.auth_kind("Basic", pass)
} }
/// Auth of other kinds such as `Digest`, `Token` etc, that will be present /// Auth of other kinds such as `Digest`, `Token` etc, that will be present
@@ -195,13 +152,10 @@ impl Agent {
/// .get("/my_page") /// .get("/my_page")
/// .call(); /// .call();
/// ``` /// ```
pub fn auth_kind<S, T>(&mut self, kind: S, pass: T) -> &mut Agent pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
{ {
let value = format!("{} {}", kind.into(), pass.into()); let value = format!("{} {}", kind, pass);
self.set("Authorization", value); self.set("Authorization", &value);
self self
} }
@@ -215,10 +169,7 @@ impl Agent {
/// .call(); /// .call();
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn request<M, S>(&self, method: M, path: S) -> Request pub fn request(&self, method: &str, path: &str) -> Request
where
M: Into<String>,
S: Into<String>,
{ {
Request::new(&self, method.into(), path.into()) Request::new(&self, method.into(), path.into())
} }
@@ -261,73 +212,55 @@ impl Agent {
} }
/// Make a GET request from this agent. /// Make a GET request from this agent.
pub fn get<S>(&self, path: S) -> Request pub fn get(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("GET", path) self.request("GET", path)
} }
/// Make a HEAD request from this agent. /// Make a HEAD request from this agent.
pub fn head<S>(&self, path: S) -> Request pub fn head(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("HEAD", path) self.request("HEAD", path)
} }
/// Make a POST request from this agent. /// Make a POST request from this agent.
pub fn post<S>(&self, path: S) -> Request pub fn post(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("POST", path) self.request("POST", path)
} }
/// Make a PUT request from this agent. /// Make a PUT request from this agent.
pub fn put<S>(&self, path: S) -> Request pub fn put(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("PUT", path) self.request("PUT", path)
} }
/// Make a DELETE request from this agent. /// Make a DELETE request from this agent.
pub fn delete<S>(&self, path: S) -> Request pub fn delete(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("DELETE", path) self.request("DELETE", path)
} }
/// Make a TRACE request from this agent. /// Make a TRACE request from this agent.
pub fn trace<S>(&self, path: S) -> Request pub fn trace(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("TRACE", path) self.request("TRACE", path)
} }
/// Make a OPTIONS request from this agent. /// Make a OPTIONS request from this agent.
pub fn options<S>(&self, path: S) -> Request pub fn options(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("OPTIONS", path) self.request("OPTIONS", path)
} }
/// Make a CONNECT request from this agent. /// Make a CONNECT request from this agent.
pub fn connect<S>(&self, path: S) -> Request pub fn connect(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("CONNECT", path) self.request("CONNECT", path)
} }
/// Make a PATCH request from this agent. /// Make a PATCH request from this agent.
pub fn patch<S>(&self, path: S) -> Request pub fn patch(&self, path: &str) -> Request
where
S: Into<String>,
{ {
self.request("PATCH", path) self.request("PATCH", path)
} }

View File

@@ -110,7 +110,6 @@ mod agent;
mod body; mod body;
mod error; mod error;
mod header; mod header;
mod macros;
mod pool; mod pool;
mod response; mod response;
mod stream; mod stream;
@@ -141,82 +140,61 @@ pub fn agent() -> Agent {
/// ``` /// ```
/// ureq::request("GET", "https://www.google.com").call(); /// ureq::request("GET", "https://www.google.com").call();
/// ``` /// ```
pub fn request<M, S>(method: M, path: S) -> Request pub fn request(method: &str, path: &str) -> Request
where
M: Into<String>,
S: Into<String>,
{ {
Agent::new().request(method, path) Agent::new().request(method, path)
} }
/// Make a GET request. /// Make a GET request.
pub fn get<S>(path: S) -> Request pub fn get(path: &str) -> Request
where
S: Into<String>,
{ {
request("GET", path) request("GET", path)
} }
/// Make a HEAD request. /// Make a HEAD request.
pub fn head<S>(path: S) -> Request pub fn head(path: &str) -> Request
where
S: Into<String>,
{ {
request("HEAD", path) request("HEAD", path)
} }
/// Make a POST request. /// Make a POST request.
pub fn post<S>(path: S) -> Request pub fn post(path: &str) -> Request
where
S: Into<String>,
{ {
request("POST", path) request("POST", path)
} }
/// Make a PUT request. /// Make a PUT request.
pub fn put<S>(path: S) -> Request pub fn put(path: &str) -> Request
where
S: Into<String>,
{ {
request("PUT", path) request("PUT", path)
} }
/// Make a DELETE request. /// Make a DELETE request.
pub fn delete<S>(path: S) -> Request pub fn delete(path: &str) -> Request
where
S: Into<String>,
{ {
request("DELETE", path) request("DELETE", path)
} }
/// Make a TRACE request. /// Make a TRACE request.
pub fn trace<S>(path: S) -> Request pub fn trace(path: &str) -> Request
where
S: Into<String>,
{ {
request("TRACE", path) request("TRACE", path)
} }
/// Make an OPTIONS request. /// Make an OPTIONS request.
pub fn options<S>(path: S) -> Request pub fn options(path: &str) -> Request
where
S: Into<String>,
{ {
request("OPTIONS", path) request("OPTIONS", path)
} }
/// Make an CONNECT request. /// Make an CONNECT request.
pub fn connect<S>(path: S) -> Request pub fn connect(path: &str) -> Request
where
S: Into<String>,
{ {
request("CONNECT", path) request("CONNECT", path)
} }
/// Make an PATCH request. /// Make an PATCH request.
pub fn patch<S>(path: S) -> Request pub fn patch(path: &str) -> Request
where
S: Into<String>,
{ {
request("PATCH", path) request("PATCH", path)
} }

View File

@@ -1,25 +0,0 @@
/// Create a `HashMap` from a shorthand notation.
///
/// ```
/// #[macro_use]
/// extern crate ureq;
///
/// fn main() {
/// let headers = map! {
/// "X-API-Key" => "foobar",
/// "Accept" => "application/json"
/// };
///
/// let agent = ureq::agent().set_map(headers).build();
/// }
/// ```
#[macro_export]
macro_rules! map(
{ $($key:expr => $value:expr),* } => {
{
let mut m = ::std::collections::HashMap::new();
$(m.insert($key, $value);)+
m
}
};
);

View File

@@ -147,9 +147,7 @@ impl Request {
/// .send_string("Hällo Wörld!"); /// .send_string("Hällo Wörld!");
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn send_string<S>(&mut self, data: S) -> Response pub fn send_string(&mut self, data: &str) -> Response
where
S: Into<String>,
{ {
let text = data.into(); let text = data.into();
let charset = response::charset_from_content_type(self.header("content-type")).to_string(); let charset = response::charset_from_content_type(self.header("content-type")).to_string();
@@ -189,12 +187,9 @@ impl Request {
/// println!("Oh no error!"); /// println!("Oh no error!");
/// } /// }
/// ``` /// ```
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Request pub fn set(&mut self, header: &str, value: &str) -> &mut Request
where
K: Into<String>,
V: Into<String>,
{ {
add_header(&mut self.headers, Header::new(&header.into(), &value.into())); add_header(&mut self.headers, Header::new(header, value));
self self
} }
@@ -238,37 +233,6 @@ impl Request {
get_all_headers(&self.headers, name) get_all_headers(&self.headers, name)
} }
/// Set many headers.
///
/// ```
/// #[macro_use]
/// extern crate ureq;
///
/// fn main() {
/// let r = ureq::get("/my_page")
/// .set_map(map! {
/// "X-API-Key" => "foobar",
/// "Accept" => "text/plain"
/// })
/// .call();
///
/// if r.ok() {
/// println!("yay got {}", r.into_string().unwrap());
/// }
/// }
/// ```
pub fn set_map<K, V, I>(&mut self, headers: I) -> &mut Request
where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
{
for (k, v) in headers.into_iter() {
self.set(k, v);
}
self
}
/// Set a query parameter. /// Set a query parameter.
/// ///
/// For example, to set `?format=json&dest=/login` /// For example, to set `?format=json&dest=/login`
@@ -281,43 +245,9 @@ impl Request {
/// ///
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn query<K, V>(&mut self, param: K, value: V) -> &mut Request pub fn query(&mut self, param: &str, value: &str) -> &mut Request
where
K: Into<String>,
V: Into<String>,
{ {
self.query.add_pair((param.into(), value.into())); self.query.add_pair((param, value));
self
}
/// Set many query parameters.
///
/// For example, to set `?format=json&dest=/login`
///
/// ```
/// #[macro_use]
/// extern crate ureq;
///
/// fn main() {
/// let r = ureq::get("/my_page")
/// .query_map(map! {
/// "format" => "json",
/// "dest" => "/login"
/// })
/// .call();
///
/// println!("{:?}", r);
/// }
/// ```
pub fn query_map<K, V, I>(&mut self, params: I) -> &mut Request
where
K: Into<String>,
V: Into<String>,
I: IntoIterator<Item = (K, V)>,
{
for (k, v) in params.into_iter() {
self.query.add_pair((k.into(), v.into()));
}
self self
} }
@@ -331,12 +261,9 @@ impl Request {
/// .call(); /// .call();
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn query_str<S>(&mut self, query: S) -> &mut Request pub fn query_str(&mut self, query: &str) -> &mut Request
where
S: Into<String>,
{ {
let s = query.into(); self.query.add_str(query);
self.query.add_str(&s);
self self
} }
@@ -398,15 +325,10 @@ impl Request {
/// let r2 = ureq::get("http://martin:rubbermashgum@localhost/my_page").call(); /// let r2 = ureq::get("http://martin:rubbermashgum@localhost/my_page").call();
/// println!("{:?}", r2); /// println!("{:?}", r2);
/// ``` /// ```
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Request pub fn auth(&mut self, user: &str, pass: &str) -> &mut Request
where
S: Into<String>,
T: Into<String>,
{ {
let u = user.into(); let pass = basic_auth(user, pass);
let p = pass.into(); self.auth_kind("Basic", &pass)
let pass = basic_auth(&u, &p);
self.auth_kind("Basic", pass)
} }
/// Auth of other kinds such as `Digest`, `Token` etc. /// Auth of other kinds such as `Digest`, `Token` etc.
@@ -417,13 +339,10 @@ impl Request {
/// .call(); /// .call();
/// println!("{:?}", r); /// println!("{:?}", r);
/// ``` /// ```
pub fn auth_kind<S, T>(&mut self, kind: S, pass: T) -> &mut Request pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Request
where
S: Into<String>,
T: Into<String>,
{ {
let value = format!("{} {}", kind.into(), pass.into()); let value = format!("{} {}", kind, pass);
self.set("Authorization", value); self.set("Authorization", &value);
self self
} }
@@ -483,6 +402,7 @@ impl Request {
/// Get the url this request was created with. /// Get the url this request was created with.
/// ///
/// This value is not normalized, it is exactly as set. /// This value is not normalized, it is exactly as set.
/// It does not contain any added query parameters.
/// ///
/// Example: /// Example:
/// ``` /// ```
@@ -500,13 +420,13 @@ impl Request {
/// ``` /// ```
/// let req1 = ureq::post("https://cool.server/innit") /// let req1 = ureq::post("https://cool.server/innit")
/// .build(); /// .build();
/// assert_eq!(req1.to_host().unwrap(), "cool.server"); /// assert_eq!(req1.get_host().unwrap(), "cool.server");
/// ///
/// let req2 = ureq::post("/some/path") /// let req2 = ureq::post("/some/path")
/// .build(); /// .build();
/// assert_eq!(req2.to_host().unwrap(), "localhost"); /// assert_eq!(req2.get_host().unwrap(), "localhost");
/// ``` /// ```
pub fn to_host(&self) -> Result<String, Error> { pub fn get_host(&self) -> Result<String, Error> {
self.to_url() self.to_url()
.map(|u| u.host_str().unwrap_or(DEFAULT_HOST).to_string()) .map(|u| u.host_str().unwrap_or(DEFAULT_HOST).to_string())
} }
@@ -517,9 +437,9 @@ impl Request {
/// ``` /// ```
/// let req = ureq::post("https://cool.server/innit") /// let req = ureq::post("https://cool.server/innit")
/// .build(); /// .build();
/// assert_eq!(req.to_scheme().unwrap(), "https"); /// assert_eq!(req.get_scheme().unwrap(), "https");
/// ``` /// ```
pub fn to_scheme(&self) -> Result<String, Error> { pub fn get_scheme(&self) -> Result<String, Error> {
self.to_url() self.to_url()
.map(|u| u.scheme().to_string()) .map(|u| u.scheme().to_string())
} }
@@ -531,9 +451,9 @@ impl Request {
/// let req = ureq::post("https://cool.server/innit?foo=bar") /// let req = ureq::post("https://cool.server/innit?foo=bar")
/// .query("format", "json") /// .query("format", "json")
/// .build(); /// .build();
/// assert_eq!(req.to_query().unwrap(), "?foo=bar&format=json"); /// assert_eq!(req.get_query().unwrap(), "?foo=bar&format=json");
/// ``` /// ```
pub fn to_query(&self) -> Result<String, Error> { pub fn get_query(&self) -> Result<String, Error> {
self.to_url() self.to_url()
.map(|u| combine_query(&u, &self.query)) .map(|u| combine_query(&u, &self.query))
} }
@@ -544,9 +464,9 @@ impl Request {
/// ``` /// ```
/// let req = ureq::post("https://cool.server/innit") /// let req = ureq::post("https://cool.server/innit")
/// .build(); /// .build();
/// assert_eq!(req.to_path().unwrap(), "/innit"); /// assert_eq!(req.get_path().unwrap(), "/innit");
/// ``` /// ```
pub fn to_path(&self) -> Result<String, Error> { pub fn get_path(&self) -> Result<String, Error> {
self.to_url() self.to_url()
.map(|u| u.path().to_string()) .map(|u| u.path().to_string())
} }