about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2019-02-26T21·22+0100
committerVincent Ambo <mail@tazj.in>2019-02-26T22·10+0100
commit3530baa4d00c3b355f3f3a8a8cb6fd47489db83a (patch)
tree09daa70e1e367a04b2c59fa91cc22afcb377a22c /src/lib.rs
parente4e931661b23d1af41b97df3f17f937cd68cc77e (diff)
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a1f7e4de6432..76d4c68e8b90 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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.