refactor: Add a method per HTTP verb on Request

Instead of the `Request::new` "constructor" for requests, add a method
per HTTP verb which makes the initialisation slightly more concise.
This commit is contained in:
Vincent Ambo 2019-02-26 22:22:29 +01:00
parent e4e931661b
commit 3530baa4d0
2 changed files with 34 additions and 20 deletions

View file

@ -30,9 +30,9 @@
//! and print the result to `stdout`:
//!
//! ```rust
//! use crimp::{Method, Request};
//! use crimp::Request;
//!
//! let response = Request::new(Method::Get, "http://httpbin.org/get")
//! let response = Request::get("http://httpbin.org/get")
//! .user_agent("crimp test suite").unwrap()
//! .send().unwrap()
//! .as_string().unwrap();
@ -91,7 +91,7 @@ use std::time::Duration;
mod tests;
/// HTTP method to use for the request.
pub enum Method {
enum Method {
Get, Post, Put, Patch, Delete
}
@ -147,7 +147,7 @@ pub struct Response<T> {
impl <'a> Request<'a> {
/// Initiate an HTTP request with the given method and URL.
pub fn new(method: Method, url: &'a str) -> Self {
fn new(method: Method, url: &'a str) -> Self {
Request {
url,
method,
@ -157,6 +157,21 @@ impl <'a> Request<'a> {
}
}
/// Initiate a GET request with the given URL.
pub fn get(url: &'a str) -> Self { Request::new(Method::Get, url) }
/// Initiate a POST request with the given URL.
pub fn post(url: &'a str) -> Self { Request::new(Method::Post, url) }
/// Initiate a PUT request with the given URL.
pub fn put(url: &'a str) -> Self { Request::new(Method::Put, url) }
/// Initiate a PATCH request with the given URL.
pub fn patch(url: &'a str) -> Self { Request::new(Method::Patch, url) }
/// Initiate a DELETE request with the given URL.
pub fn delete(url: &'a str) -> Self { Request::new(Method::Delete, url) }
/// Add an HTTP header to a request.
pub fn header(mut self, k: &str, v: &str) -> Result<Self, curl::Error> {
self.headers.append(&format!("{}: {}", k, v))?;
@ -245,8 +260,8 @@ impl <'a> Request<'a> {
/// directly.
///
/// ```
/// # use crimp::{Request, Method};
/// let response = Request::new(Method::Get, "https://httpbin.org/get")
/// # use crimp::Request;
/// let response = Request::get("https://httpbin.org/get")
/// .with_handle(|mut handle| handle.referer("Example-Referer")).unwrap()
/// .send().unwrap();
/// #
@ -280,7 +295,7 @@ impl <'a> Request<'a> {
/// .contents("some-data".as_bytes())
/// .add().unwrap();
///
/// let response = Request::new(Method::Post, "https://httpbin.org/post")
/// let response = Request::post("https://httpbin.org/post")
/// .user_agent("crimp test suite").unwrap()
/// .form(form)
/// .send().unwrap();
@ -432,8 +447,9 @@ impl <T> Response<T> {
self.status >= 200 && self.status < 300
}
/// Check whether a request succeeded and let users provide a
/// closure that creates an error from the request if it did not.
/// Check whether a request succeeded using `Request::is_success`
/// and let users provide a closure that creates a custom error
/// from the request if it did not.
///
/// This function exists for convenience to avoid having to write
/// repetitive `if !response.is_success() { ... }` blocks.

View file

@ -6,7 +6,7 @@ use serde_json::{Value, json};
#[test]
fn test_http_get() {
let resp = Request::new(Method::Get, "https://httpbin.org/get")
let resp = Request::get("https://httpbin.org/get")
.send().expect("failed to send request");
assert!(resp.is_success(), "request should have succeeded");
@ -14,7 +14,7 @@ fn test_http_get() {
#[test]
fn test_http_delete() {
let resp = Request::new(Method::Delete, "https://httpbin.org/delete")
let resp = Request::delete("https://httpbin.org/delete")
.send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK");
@ -22,7 +22,7 @@ fn test_http_delete() {
#[test]
fn test_http_put() {
let resp = Request::new(Method::Put, "https://httpbin.org/put")
let resp = Request::put("https://httpbin.org/put")
.send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK");
@ -30,7 +30,7 @@ fn test_http_put() {
#[test]
fn test_http_patch() {
let resp = Request::new(Method::Patch, "https://httpbin.org/patch")
let resp = Request::patch("https://httpbin.org/patch")
.send().expect("failed to send request");
assert_eq!(200, resp.status, "response status should be 200 OK");
@ -42,7 +42,7 @@ fn test_http_patch() {
#[test]
fn test_http_post() {
let body = "test body";
let response = Request::new(Method::Post, "https://httpbin.org/post")
let response = Request::post("https://httpbin.org/post")
.user_agent("crimp test suite").expect("failed to set user-agent")
.timeout(Duration::from_secs(5)).expect("failed to set request timeout")
.body("text/plain", &body.as_bytes())
@ -69,7 +69,7 @@ fn test_http_post_json() {
"purpose": "testing!"
});
let response = Request::new(Method::Post, "https://httpbin.org/post")
let response = Request::post("https://httpbin.org/post")
.user_agent("crimp test suite").expect("failed to set user-agent")
.timeout(Duration::from_secs(5)).expect("failed to set request timeout")
.json(&body).expect("request serialization failed")
@ -96,7 +96,7 @@ fn test_http_post_json() {
#[test]
fn test_bearer_auth() {
let response = Request::new(Method::Get, "https://httpbin.org/bearer")
let response = Request::get("https://httpbin.org/bearer")
.bearer_auth("some-token").expect("failed to set auth header")
.send().expect("failed to send request");
@ -105,9 +105,7 @@ fn test_bearer_auth() {
#[test]
fn test_basic_auth() {
let request = Request::new(
Method::Get, "https://httpbin.org/basic-auth/alan_watts/oneness"
);
let request = Request::get("https://httpbin.org/basic-auth/alan_watts/oneness");
let response = request
.basic_auth("alan_watts", "oneness").expect("failed to set auth header")
@ -120,7 +118,7 @@ fn test_basic_auth() {
#[test]
fn test_error_for_status() {
let response = Request::new(Method::Get, "https://httpbin.org/patch")
let response = Request::get("https://httpbin.org/patch")
.send().expect("failed to send request")
.error_for_status(|resp| format!("Response error code: {}", resp.status));