use std::io::{self, Read}; use hyper::header::{Headers, ContentType, UserAgent}; use hyper::method::Method; use hyper::status::StatusCode; use hyper::version::HttpVersion; use hyper::{Url}; use serde::Serialize; use serde_json; use ::body::{self, Body}; static DEFAULT_USER_AGENT: &'static str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); /// A `Client` to make Requests with. /// /// The Client has various configuration values to tweak, but the defaults /// are set to what is usually the most commonly desired value. /// /// The `Client` holds a connection pool internally, so it is advised that /// you create one and reuse it. pub struct Client { inner: ::hyper::Client, } impl Client { /// Constructs a new `Client`. pub fn new() -> Client { Client { inner: new_hyper_client() } } /// Convenience method to make a `GET` request to a URL. pub fn get(&self, url: &str) -> RequestBuilder { self.request(Method::Get, Url::parse(url).unwrap()) } /// Convenience method to make a `POST` request to a URL. pub fn post(&self, url: &str) -> RequestBuilder { self.request(Method::Post, Url::parse(url).unwrap()) } /// Start building a `Request` with the `Method` and `Url`. /// /// Returns a `RequestBuilder`, which will allow setting headers and /// request body before sending. pub fn request(&self, method: Method, url: Url) -> RequestBuilder { debug!("request {:?} \"{}\"", method, url); RequestBuilder { client: self, method: method, url: url, _version: HttpVersion::Http11, headers: Headers::new(), body: None, } } } #[cfg(not(feature = "tls"))] fn new_hyper_client() -> ::hyper::Client { ::hyper::Client::new() } #[cfg(feature = "tls")] fn new_hyper_client() -> ::hyper::Client { use tls::TlsClient; ::hyper::Client::with_connector( ::hyper::client::Pool::with_connector( Default::default(), ::hyper::net::HttpsConnector::new(TlsClient::new().unwrap()) ) ) } /// A builder to construct the properties of a `Request`. pub struct RequestBuilder<'a> { client: &'a Client, method: Method, url: Url, _version: HttpVersion, headers: Headers, body: Option
, } impl<'a> RequestBuilder<'a> { /// Add a `Header` to this Request. pub fn header