refactor: Move URL & method configuration to send()

This lets the builder proceed without returning a `Result` from the
initial call, which makes for a slightly nicer API.
This commit is contained in:
Vincent Ambo 2019-02-26 16:16:07 +01:00
parent 9ce0098bc0
commit d3a47d3b1c
2 changed files with 25 additions and 25 deletions

View file

@ -14,7 +14,7 @@
//! ```rust //! ```rust
//! use crimp::{Method, Request}; //! use crimp::{Method, Request};
//! //!
//! let response = Request::new(Method::Get, "http://httpbin.org/get").unwrap() //! let response = Request::new(Method::Get, "http://httpbin.org/get")
//! .user_agent("crimp test suite").unwrap() //! .user_agent("crimp test suite").unwrap()
//! .send().unwrap(); //! .send().unwrap();
//! //!
@ -65,6 +65,8 @@ pub enum Method {
/// request its parameters are modified using the various builder /// request its parameters are modified using the various builder
/// methods until it is consumed by `send()`. /// methods until it is consumed by `send()`.
pub struct Request<'a> { pub struct Request<'a> {
url: &'a str,
method: Method,
handle: Easy, handle: Easy,
headers: List, headers: List,
body: Body<'a>, body: Body<'a>,
@ -101,23 +103,14 @@ pub struct Response<T> {
impl <'a> Request<'a> { impl <'a> Request<'a> {
/// Initiate an HTTP request with the given method and URL. /// Initiate an HTTP request with the given method and URL.
pub fn new(method: Method, url: &str) -> Result<Self, curl::Error> { pub fn new(method: Method, url: &'a str) -> Self {
let mut handle = Easy::new(); Request {
handle.url(url)?; url,
method,
match method { handle: Easy::new(),
Method::Get => handle.get(true)?,
Method::Post => handle.post(true)?,
Method::Put => handle.put(true)?,
Method::Patch => handle.custom_request("PATCH")?,
Method::Delete => handle.custom_request("DELETE")?,
}
Ok(Request {
handle,
headers: List::new(), headers: List::new(),
body: Body::NoBody, body: Body::NoBody,
}) }
} }
/// Add an HTTP header to a request. /// Add an HTTP header to a request.
@ -151,6 +144,17 @@ impl <'a> Request<'a> {
/// Send the HTTP request and return a response structure /// Send the HTTP request and return a response structure
/// containing the raw body. /// containing the raw body.
pub fn send(mut self) -> Result<Response<Vec<u8>>, curl::Error> { pub fn send(mut self) -> Result<Response<Vec<u8>>, curl::Error> {
// Configure request basics:
self.handle.url(self.url)?;
match self.method {
Method::Get => self.handle.get(true)?,
Method::Post => self.handle.post(true)?,
Method::Put => self.handle.put(true)?,
Method::Patch => self.handle.custom_request("PATCH")?,
Method::Delete => self.handle.custom_request("DELETE")?,
}
// Create structures in which to store the response data: // Create structures in which to store the response data:
let mut headers = HashMap::new(); let mut headers = HashMap::new();
let mut body = vec![]; let mut body = vec![];

View file

@ -7,9 +7,7 @@ use serde_json::{Value, json};
#[test] #[test]
fn test_http_get() { fn test_http_get() {
let resp = Request::new(Method::Get, "https://httpbin.org/get") let resp = Request::new(Method::Get, "https://httpbin.org/get")
.expect("failed to create request") .send().expect("failed to send request");
.send()
.expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
} }
@ -17,7 +15,6 @@ fn test_http_get() {
#[test] #[test]
fn test_http_delete() { fn test_http_delete() {
let resp = Request::new(Method::Delete, "https://httpbin.org/delete") let resp = Request::new(Method::Delete, "https://httpbin.org/delete")
.expect("failed to create request")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
@ -26,7 +23,6 @@ fn test_http_delete() {
#[test] #[test]
fn test_http_put() { fn test_http_put() {
let resp = Request::new(Method::Put, "https://httpbin.org/put") let resp = Request::new(Method::Put, "https://httpbin.org/put")
.expect("failed to create request")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
@ -35,17 +31,18 @@ fn test_http_put() {
#[test] #[test]
fn test_http_patch() { fn test_http_patch() {
let resp = Request::new(Method::Patch, "https://httpbin.org/patch") let resp = Request::new(Method::Patch, "https://httpbin.org/patch")
.expect("failed to create request")
.send().expect("failed to send request"); .send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK"); assert_eq!(200, resp.status, "response status should be 200 OK");
} }
// These tests perform various requests with different body payloads
// and verify that those were received correctly by the remote side.
#[test] #[test]
fn test_http_post() { fn test_http_post() {
let body = "test body"; let body = "test body";
let response = Request::new(Method::Post, "https://httpbin.org/post") let response = Request::new(Method::Post, "https://httpbin.org/post")
.expect("failed to create request")
.user_agent("crimp test suite").expect("failed to set user-agent") .user_agent("crimp test suite").expect("failed to set user-agent")
.body("text/plain", &body.as_bytes()) .body("text/plain", &body.as_bytes())
.send().expect("failed to send request") .send().expect("failed to send request")
@ -65,14 +62,13 @@ fn test_http_post() {
); );
} }
#[test] #[cfg(feature = "json")] #[test]
fn test_http_post_json() { fn test_http_post_json() {
let body = json!({ let body = json!({
"purpose": "testing!" "purpose": "testing!"
}); });
let response = Request::new(Method::Post, "https://httpbin.org/post") let response = Request::new(Method::Post, "https://httpbin.org/post")
.expect("failed to create request")
.user_agent("crimp test suite").expect("failed to set user-agent") .user_agent("crimp test suite").expect("failed to set user-agent")
.json(&body).expect("request serialization failed") .json(&body).expect("request serialization failed")
.send().expect("failed to send request") .send().expect("failed to send request")