From b16b14c6aa4914e5396bf029c7adbf9c301f5c40 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Tue, 4 Sep 2018 10:59:10 +0200 Subject: [PATCH] simpler api --- src/agent.rs | 103 +++++++--------------------------------- src/lib.rs | 42 ++++------------- src/macros.rs | 25 ---------- src/request.rs | 126 +++++++++---------------------------------------- 4 files changed, 51 insertions(+), 245 deletions(-) delete mode 100644 src/macros.rs diff --git a/src/agent.rs b/src/agent.rs index f574f11..d4b220d 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -112,53 +112,15 @@ impl Agent { /// println!("Oh no error!"); /// } /// ``` - pub fn set(&mut self, header: K, value: V) -> &mut Agent - where - K: Into, - V: Into, + pub fn set(&mut self, header: &str, value: &str) -> &mut Agent { add_header( &mut self.headers, - Header::new(&header.into(), &value.into()), + Header::new(header, value), ); 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(&mut self, headers: I) -> &mut Agent - where - K: Into, - V: Into, - I: IntoIterator, - { - for (k, v) in headers.into_iter() { - self.set(k, v); - } - self - } - /// Basic auth that will be present in all requests using the agent. /// /// ``` @@ -171,15 +133,10 @@ impl Agent { /// .call(); /// println!("{:?}", r); /// ``` - pub fn auth(&mut self, user: S, pass: T) -> &mut Agent - where - S: Into, - T: Into, + pub fn auth(&mut self, user: &str, pass: &str) -> &mut Agent { - let u = user.into(); - let p = pass.into(); - let pass = basic_auth(&u, &p); - self.auth_kind("Basic", pass) + let pass = basic_auth(user, pass); + self.auth_kind("Basic", &pass) } /// Auth of other kinds such as `Digest`, `Token` etc, that will be present @@ -195,13 +152,10 @@ impl Agent { /// .get("/my_page") /// .call(); /// ``` - pub fn auth_kind(&mut self, kind: S, pass: T) -> &mut Agent - where - S: Into, - T: Into, + pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Agent { - let value = format!("{} {}", kind.into(), pass.into()); - self.set("Authorization", value); + let value = format!("{} {}", kind, pass); + self.set("Authorization", &value); self } @@ -215,10 +169,7 @@ impl Agent { /// .call(); /// println!("{:?}", r); /// ``` - pub fn request(&self, method: M, path: S) -> Request - where - M: Into, - S: Into, + pub fn request(&self, method: &str, path: &str) -> Request { Request::new(&self, method.into(), path.into()) } @@ -261,73 +212,55 @@ impl Agent { } /// Make a GET request from this agent. - pub fn get(&self, path: S) -> Request - where - S: Into, + pub fn get(&self, path: &str) -> Request { self.request("GET", path) } /// Make a HEAD request from this agent. - pub fn head(&self, path: S) -> Request - where - S: Into, + pub fn head(&self, path: &str) -> Request { self.request("HEAD", path) } /// Make a POST request from this agent. - pub fn post(&self, path: S) -> Request - where - S: Into, + pub fn post(&self, path: &str) -> Request { self.request("POST", path) } /// Make a PUT request from this agent. - pub fn put(&self, path: S) -> Request - where - S: Into, + pub fn put(&self, path: &str) -> Request { self.request("PUT", path) } /// Make a DELETE request from this agent. - pub fn delete(&self, path: S) -> Request - where - S: Into, + pub fn delete(&self, path: &str) -> Request { self.request("DELETE", path) } /// Make a TRACE request from this agent. - pub fn trace(&self, path: S) -> Request - where - S: Into, + pub fn trace(&self, path: &str) -> Request { self.request("TRACE", path) } /// Make a OPTIONS request from this agent. - pub fn options(&self, path: S) -> Request - where - S: Into, + pub fn options(&self, path: &str) -> Request { self.request("OPTIONS", path) } /// Make a CONNECT request from this agent. - pub fn connect(&self, path: S) -> Request - where - S: Into, + pub fn connect(&self, path: &str) -> Request { self.request("CONNECT", path) } /// Make a PATCH request from this agent. - pub fn patch(&self, path: S) -> Request - where - S: Into, + pub fn patch(&self, path: &str) -> Request { self.request("PATCH", path) } diff --git a/src/lib.rs b/src/lib.rs index 3d19158..4f03f16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,6 @@ mod agent; mod body; mod error; mod header; -mod macros; mod pool; mod response; mod stream; @@ -141,82 +140,61 @@ pub fn agent() -> Agent { /// ``` /// ureq::request("GET", "https://www.google.com").call(); /// ``` -pub fn request(method: M, path: S) -> Request -where - M: Into, - S: Into, +pub fn request(method: &str, path: &str) -> Request { Agent::new().request(method, path) } /// Make a GET request. -pub fn get(path: S) -> Request -where - S: Into, +pub fn get(path: &str) -> Request { request("GET", path) } /// Make a HEAD request. -pub fn head(path: S) -> Request -where - S: Into, +pub fn head(path: &str) -> Request { request("HEAD", path) } /// Make a POST request. -pub fn post(path: S) -> Request -where - S: Into, +pub fn post(path: &str) -> Request { request("POST", path) } /// Make a PUT request. -pub fn put(path: S) -> Request -where - S: Into, +pub fn put(path: &str) -> Request { request("PUT", path) } /// Make a DELETE request. -pub fn delete(path: S) -> Request -where - S: Into, +pub fn delete(path: &str) -> Request { request("DELETE", path) } /// Make a TRACE request. -pub fn trace(path: S) -> Request -where - S: Into, +pub fn trace(path: &str) -> Request { request("TRACE", path) } /// Make an OPTIONS request. -pub fn options(path: S) -> Request -where - S: Into, +pub fn options(path: &str) -> Request { request("OPTIONS", path) } /// Make an CONNECT request. -pub fn connect(path: S) -> Request -where - S: Into, +pub fn connect(path: &str) -> Request { request("CONNECT", path) } /// Make an PATCH request. -pub fn patch(path: S) -> Request -where - S: Into, +pub fn patch(path: &str) -> Request { request("PATCH", path) } diff --git a/src/macros.rs b/src/macros.rs deleted file mode 100644 index d0c3674..0000000 --- a/src/macros.rs +++ /dev/null @@ -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 - } - }; -); diff --git a/src/request.rs b/src/request.rs index 543dc2d..859778e 100644 --- a/src/request.rs +++ b/src/request.rs @@ -147,9 +147,7 @@ impl Request { /// .send_string("Hällo Wörld!"); /// println!("{:?}", r); /// ``` - pub fn send_string(&mut self, data: S) -> Response - where - S: Into, + pub fn send_string(&mut self, data: &str) -> Response { let text = data.into(); let charset = response::charset_from_content_type(self.header("content-type")).to_string(); @@ -189,12 +187,9 @@ impl Request { /// println!("Oh no error!"); /// } /// ``` - pub fn set(&mut self, header: K, value: V) -> &mut Request - where - K: Into, - V: Into, + pub fn set(&mut self, header: &str, value: &str) -> &mut Request { - add_header(&mut self.headers, Header::new(&header.into(), &value.into())); + add_header(&mut self.headers, Header::new(header, value)); self } @@ -238,37 +233,6 @@ impl Request { 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(&mut self, headers: I) -> &mut Request - where - K: Into, - V: Into, - I: IntoIterator, - { - for (k, v) in headers.into_iter() { - self.set(k, v); - } - self - } - /// Set a query parameter. /// /// For example, to set `?format=json&dest=/login` @@ -281,43 +245,9 @@ impl Request { /// /// println!("{:?}", r); /// ``` - pub fn query(&mut self, param: K, value: V) -> &mut Request - where - K: Into, - V: Into, + pub fn query(&mut self, param: &str, value: &str) -> &mut Request { - self.query.add_pair((param.into(), value.into())); - 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(&mut self, params: I) -> &mut Request - where - K: Into, - V: Into, - I: IntoIterator, - { - for (k, v) in params.into_iter() { - self.query.add_pair((k.into(), v.into())); - } + self.query.add_pair((param, value)); self } @@ -331,12 +261,9 @@ impl Request { /// .call(); /// println!("{:?}", r); /// ``` - pub fn query_str(&mut self, query: S) -> &mut Request - where - S: Into, + pub fn query_str(&mut self, query: &str) -> &mut Request { - let s = query.into(); - self.query.add_str(&s); + self.query.add_str(query); self } @@ -398,15 +325,10 @@ impl Request { /// let r2 = ureq::get("http://martin:rubbermashgum@localhost/my_page").call(); /// println!("{:?}", r2); /// ``` - pub fn auth(&mut self, user: S, pass: T) -> &mut Request - where - S: Into, - T: Into, + pub fn auth(&mut self, user: &str, pass: &str) -> &mut Request { - let u = user.into(); - let p = pass.into(); - let pass = basic_auth(&u, &p); - self.auth_kind("Basic", pass) + let pass = basic_auth(user, pass); + self.auth_kind("Basic", &pass) } /// Auth of other kinds such as `Digest`, `Token` etc. @@ -417,13 +339,10 @@ impl Request { /// .call(); /// println!("{:?}", r); /// ``` - pub fn auth_kind(&mut self, kind: S, pass: T) -> &mut Request - where - S: Into, - T: Into, + pub fn auth_kind(&mut self, kind: &str, pass: &str) -> &mut Request { - let value = format!("{} {}", kind.into(), pass.into()); - self.set("Authorization", value); + let value = format!("{} {}", kind, pass); + self.set("Authorization", &value); self } @@ -483,6 +402,7 @@ impl Request { /// Get the url this request was created with. /// /// This value is not normalized, it is exactly as set. + /// It does not contain any added query parameters. /// /// Example: /// ``` @@ -500,13 +420,13 @@ impl Request { /// ``` /// let req1 = ureq::post("https://cool.server/innit") /// .build(); - /// assert_eq!(req1.to_host().unwrap(), "cool.server"); + /// assert_eq!(req1.get_host().unwrap(), "cool.server"); /// /// let req2 = ureq::post("/some/path") /// .build(); - /// assert_eq!(req2.to_host().unwrap(), "localhost"); + /// assert_eq!(req2.get_host().unwrap(), "localhost"); /// ``` - pub fn to_host(&self) -> Result { + pub fn get_host(&self) -> Result { self.to_url() .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") /// .build(); - /// assert_eq!(req.to_scheme().unwrap(), "https"); + /// assert_eq!(req.get_scheme().unwrap(), "https"); /// ``` - pub fn to_scheme(&self) -> Result { + pub fn get_scheme(&self) -> Result { self.to_url() .map(|u| u.scheme().to_string()) } @@ -531,9 +451,9 @@ impl Request { /// let req = ureq::post("https://cool.server/innit?foo=bar") /// .query("format", "json") /// .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 { + pub fn get_query(&self) -> Result { self.to_url() .map(|u| combine_query(&u, &self.query)) } @@ -544,9 +464,9 @@ impl Request { /// ``` /// let req = ureq::post("https://cool.server/innit") /// .build(); - /// assert_eq!(req.to_path().unwrap(), "/innit"); + /// assert_eq!(req.get_path().unwrap(), "/innit"); /// ``` - pub fn to_path(&self) -> Result { + pub fn get_path(&self) -> Result { self.to_url() .map(|u| u.path().to_string()) }