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!");
/// }
/// ```
pub fn set<K, V>(&mut self, header: K, value: V) -> &mut Agent
where
K: Into<String>,
V: Into<String>,
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<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.
///
/// ```
@@ -171,15 +133,10 @@ impl Agent {
/// .call();
/// println!("{:?}", r);
/// ```
pub fn auth<S, T>(&mut self, user: S, pass: T) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
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<S, T>(&mut self, kind: S, pass: T) -> &mut Agent
where
S: Into<String>,
T: Into<String>,
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<M, S>(&self, method: M, path: S) -> Request
where
M: Into<String>,
S: Into<String>,
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<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn get(&self, path: &str) -> Request
{
self.request("GET", path)
}
/// Make a HEAD request from this agent.
pub fn head<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn head(&self, path: &str) -> Request
{
self.request("HEAD", path)
}
/// Make a POST request from this agent.
pub fn post<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn post(&self, path: &str) -> Request
{
self.request("POST", path)
}
/// Make a PUT request from this agent.
pub fn put<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn put(&self, path: &str) -> Request
{
self.request("PUT", path)
}
/// Make a DELETE request from this agent.
pub fn delete<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn delete(&self, path: &str) -> Request
{
self.request("DELETE", path)
}
/// Make a TRACE request from this agent.
pub fn trace<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn trace(&self, path: &str) -> Request
{
self.request("TRACE", path)
}
/// Make a OPTIONS request from this agent.
pub fn options<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn options(&self, path: &str) -> Request
{
self.request("OPTIONS", path)
}
/// Make a CONNECT request from this agent.
pub fn connect<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn connect(&self, path: &str) -> Request
{
self.request("CONNECT", path)
}
/// Make a PATCH request from this agent.
pub fn patch<S>(&self, path: S) -> Request
where
S: Into<String>,
pub fn patch(&self, path: &str) -> Request
{
self.request("PATCH", path)
}

View File

@@ -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<M, S>(method: M, path: S) -> Request
where
M: Into<String>,
S: Into<String>,
pub fn request(method: &str, path: &str) -> Request
{
Agent::new().request(method, path)
}
/// Make a GET request.
pub fn get<S>(path: S) -> Request
where
S: Into<String>,
pub fn get(path: &str) -> Request
{
request("GET", path)
}
/// Make a HEAD request.
pub fn head<S>(path: S) -> Request
where
S: Into<String>,
pub fn head(path: &str) -> Request
{
request("HEAD", path)
}
/// Make a POST request.
pub fn post<S>(path: S) -> Request
where
S: Into<String>,
pub fn post(path: &str) -> Request
{
request("POST", path)
}
/// Make a PUT request.
pub fn put<S>(path: S) -> Request
where
S: Into<String>,
pub fn put(path: &str) -> Request
{
request("PUT", path)
}
/// Make a DELETE request.
pub fn delete<S>(path: S) -> Request
where
S: Into<String>,
pub fn delete(path: &str) -> Request
{
request("DELETE", path)
}
/// Make a TRACE request.
pub fn trace<S>(path: S) -> Request
where
S: Into<String>,
pub fn trace(path: &str) -> Request
{
request("TRACE", path)
}
/// Make an OPTIONS request.
pub fn options<S>(path: S) -> Request
where
S: Into<String>,
pub fn options(path: &str) -> Request
{
request("OPTIONS", path)
}
/// Make an CONNECT request.
pub fn connect<S>(path: S) -> Request
where
S: Into<String>,
pub fn connect(path: &str) -> Request
{
request("CONNECT", path)
}
/// Make an PATCH request.
pub fn patch<S>(path: S) -> Request
where
S: Into<String>,
pub fn patch(path: &str) -> Request
{
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!");
/// println!("{:?}", r);
/// ```
pub fn send_string<S>(&mut self, data: S) -> Response
where
S: Into<String>,
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<K, V>(&mut self, header: K, value: V) -> &mut Request
where
K: Into<String>,
V: Into<String>,
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<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.
///
/// For example, to set `?format=json&dest=/login`
@@ -281,43 +245,9 @@ impl Request {
///
/// println!("{:?}", r);
/// ```
pub fn query<K, V>(&mut self, param: K, value: V) -> &mut Request
where
K: Into<String>,
V: Into<String>,
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<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.query.add_pair((param, value));
self
}
@@ -331,12 +261,9 @@ impl Request {
/// .call();
/// println!("{:?}", r);
/// ```
pub fn query_str<S>(&mut self, query: S) -> &mut Request
where
S: Into<String>,
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<S, T>(&mut self, user: S, pass: T) -> &mut Request
where
S: Into<String>,
T: Into<String>,
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<S, T>(&mut self, kind: S, pass: T) -> &mut Request
where
S: Into<String>,
T: Into<String>,
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<String, Error> {
pub fn get_host(&self) -> Result<String, Error> {
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<String, Error> {
pub fn get_scheme(&self) -> Result<String, Error> {
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<String, Error> {
pub fn get_query(&self) -> Result<String, Error> {
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<String, Error> {
pub fn get_path(&self) -> Result<String, Error> {
self.to_url()
.map(|u| u.path().to_string())
}