about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2019-02-26T15·16+0100
committerVincent Ambo <mail@tazj.in>2019-02-26T16·30+0100
commitd3a47d3b1c97fdd3497dd79ce66724876c383d12 (patch)
tree58c406670e2c80e48eb5fd42ee72f6b7b7f30d3c /src/lib.rs
parent9ce0098bc02ba385c0fc28651fd67a9c0c15c51d (diff)
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 080e7d9c6670..d25153388927 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,7 +14,7 @@
 //! ```rust
 //! 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()
 //!     .send().unwrap();
 //!
@@ -65,6 +65,8 @@ pub enum Method {
 /// request its parameters are modified using the various builder
 /// methods until it is consumed by `send()`.
 pub struct Request<'a> {
+    url: &'a str,
+    method: Method,
     handle: Easy,
     headers: List,
     body: Body<'a>,
@@ -101,23 +103,14 @@ pub struct Response<T> {
 
 impl <'a> Request<'a> {
     /// Initiate an HTTP request with the given method and URL.
-    pub fn new(method: Method, url: &str) -> Result<Self, curl::Error> {
-        let mut handle = Easy::new();
-        handle.url(url)?;
-
-        match method {
-            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,
+    pub fn new(method: Method, url: &'a str) -> Self {
+        Request {
+            url,
+            method,
+            handle: Easy::new(),
             headers: List::new(),
             body: Body::NoBody,
-        })
+        }
     }
 
     /// Add an HTTP header to a request.
@@ -151,6 +144,17 @@ impl <'a> Request<'a> {
     /// Send the HTTP request and return a response structure
     /// containing the raw body.
     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:
         let mut headers = HashMap::new();
         let mut body = vec![];