Skip to content

Commit

Permalink
add native-tls and serde json support
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Oct 23, 2016
1 parent bfd95e3 commit 1259128
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 20 deletions.
15 changes: 14 additions & 1 deletion Cargo.toml
Expand Up @@ -2,14 +2,27 @@
name = "reqwest"
version = "0.0.0"
description = "higher level HTTP client library"
keywords = ["http", "request", "client"]
repository = "https://github.com/seanmonstar/reqwest"
documentation = "https://docs.rs/reqwest"
authors = ["Sean McArthur <sean.monstar@gmail.com>"]
license = "MIT/Apache-2.0"

[dependencies]
hyper = { version = "0.9" }#, default-features = false }
hyper = { version = "0.9" , default-features = false }
serde = "0.8"
serde_json = "0.8"
log = "0.3"

[dependencies.native-tls]
git = "https://github.com/sfackler/rust-native-tls"
optional = true

[features]
default = ["openssl"]
openssl = ["hyper/ssl"]
tls = ["native-tls"]

[dev-dependencies]
env_logger = "0.3"
serde_derive = "0.8"
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
# request
# reqwest

An ergonomic HTTP Client for Rust

Expand Down
40 changes: 40 additions & 0 deletions examples/post.rs
@@ -0,0 +1,40 @@
//#![feature(proc_macro)]

extern crate reqwest;
extern crate env_logger;
//#[macro_use] extern crate serde_derive;

/*
#[derive(Serialize)]
struct Thingy {
a: i32,
b: bool,
c: String,
}
*/

fn main() {
env_logger::init().unwrap();

println!("POST https://httpbin.org/post");

/*
let thingy = Thingy {
a: 5,
b: true,
c: String::from("reqwest")
};
*/

let client = reqwest::Client::new();
let mut res = client.post("https://httpbin.org/post")
.body("foo=bar")
.send().unwrap();

println!("Status: {}", res.status());
println!("Headers:\n{}", res.headers());

::std::io::copy(&mut res, &mut ::std::io::stdout()).unwrap();

println!("\n\nDone.");
}
2 changes: 1 addition & 1 deletion examples/simple.rs
Expand Up @@ -6,7 +6,7 @@ fn main() {

println!("GET https://www.rust-lang.org");

let mut res = reqwest::get("http://www.rust-lang.org").unwrap();
let mut res = reqwest::get("https://www.rust-lang.org").unwrap();

println!("Status: {}", res.status());
println!("Headers:\n{}", res.headers());
Expand Down
51 changes: 45 additions & 6 deletions src/body.rs
@@ -1,26 +1,38 @@
use std::io::Read;

pub struct Body(Kind);
pub struct Body {
reader: Kind,
}

impl Body {
pub fn new<R: Read + 'static>(reader: R) -> Body {
Body {
reader: Kind::Reader(Box::new(reader), None),
}
}

/*
pub fn sized(reader: (), len: u64) -> Body {
unimplemented!()
}
pub fn chunked(reader: ()) -> Body {
unimplemented!()
}
*/
}

enum Kind {
Length,
Chunked
Reader(Box<Read>, Option<u64>),
Bytes(Vec<u8>),
}

impl From<Vec<u8>> for Body {
#[inline]
fn from(v: Vec<u8>) -> Body {
unimplemented!()
Body {
reader: Kind::Bytes(v),
}
}
}

Expand All @@ -31,7 +43,34 @@ impl From<String> for Body {
}
}

/// Wraps a `std::io::Write`.
pub struct Pipe(Kind);

impl<'a> From<&'a [u8]> for Body {
#[inline]
fn from(s: &'a [u8]) -> Body {
s.to_vec().into()
}
}

impl<'a> From<&'a str> for Body {
#[inline]
fn from(s: &'a str) -> Body {
s.as_bytes().into()
}
}



// Wraps a `std::io::Write`.
//pub struct Pipe(Kind);


pub fn as_hyper_body<'a>(body: &'a Body) -> ::hyper::client::Body<'a> {
match body.reader {
Kind::Bytes(ref bytes) => {
let len = bytes.len();
::hyper::client::Body::BufBody(bytes, len)
},
Kind::Reader(..) => unimplemented!()
}
}

82 changes: 73 additions & 9 deletions src/client.rs
@@ -1,75 +1,134 @@
use std::io::{self, Read};

use hyper::header::Headers;
use hyper::header::{Headers, ContentType, UserAgent};
use hyper::method::Method;
use hyper::status::StatusCode;
use hyper::version::HttpVersion;
use hyper::{Url};

use ::body::Body;
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: ::hyper::Client::new(),
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,
_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,
_version: HttpVersion,
headers: Headers,

body: Option<Body>,
}

impl<'a> RequestBuilder<'a> {

/// Add a `Header` to this Request.
pub fn header<H: ::header::Header + ::header::HeaderFormat>(mut self, header: H) -> RequestBuilder<'a> {
self.headers.set(header);
self
}

/// Add a set of Headers to the existing ones on this Request.
///
/// The headers will be merged in to any already set.
pub fn headers(mut self, headers: ::header::Headers) -> RequestBuilder<'a> {
self.headers.extend(headers.iter());
self
}

/// Set the request body.
pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder<'a> {
self.body = Some(body.into());
self
}

pub fn json<T: Serialize>(mut self, json: T) -> RequestBuilder<'a> {
let body = serde_json::to_vec(&json).expect("serde to_vec cannot fail");
self.headers.set(ContentType::json());
self.body = Some(body.into());
self
}

/// Constructs the Request and sends it the target URL, returning a Response.
pub fn send(mut self) -> ::Result<Response> {
let req = self.client.inner.request(self.method, self.url)
if !self.headers.has::<UserAgent>() {
self.headers.set(UserAgent(DEFAULT_USER_AGENT.to_owned()));
}

let mut req = self.client.inner.request(self.method, self.url)
.headers(self.headers);

//TODO: body
if let Some(ref b) = self.body {
let body = body::as_hyper_body(b);
req = req.body(body);
}

let res = try!(req.send());
Ok(Response {
Expand All @@ -78,24 +137,29 @@ impl<'a> RequestBuilder<'a> {
}
}

/// A Response to a submitted `Request`.
pub struct Response {
inner: ::hyper::client::Response,
}

impl Response {
/// Get the `StatusCode`.
pub fn status(&self) -> &StatusCode {
&self.inner.status
}

/// Get the `Headers`.
pub fn headers(&self) -> &Headers {
&self.inner.headers
}

/// Get the `HttpVersion`.
pub fn version(&self) -> &HttpVersion {
&self.inner.version
}
}

/// Read the body of the Response.
impl Read for Response {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
@@ -1,5 +1,7 @@
/// The Errors that may occur when processing a `Request`.
#[derive(Debug)]
pub enum Error {
/// An HTTP error from the `hyper` crate.
Http(::hyper::Error),
#[doc(hidden)]
__DontMatchMe,
Expand All @@ -11,4 +13,5 @@ impl From<::hyper::Error> for Error {
}
}

/// A `Result` alias where the `Err` case is `reqwest::Error`.
pub type Result<T> = ::std::result::Result<T, Error>;

0 comments on commit 1259128

Please sign in to comment.