feat: Add Request::with_handle method

This method acts as an "escape-hatch" which lets the user configure
whatever they want on the cURL easy handle. No warranty!
This commit is contained in:
Vincent Ambo 2019-02-26 17:55:00 +01:00
parent df117f855b
commit cf58060fcc

View file

@ -228,6 +228,27 @@ impl <'a> Request<'a> {
Ok(self) Ok(self)
} }
/// Set custom configuration on the cURL `Easy` handle.
///
/// This function can be considered an "escape-hatch" from the
/// high-level API which lets users access the internal
/// `curl::easy::Easy` handle and configure options on it
/// directly.
///
/// ```
/// # use crimp::{Request, Method};
/// let response = Request::new(Method::Get, "https://httpbin.org/get")
/// .with_handle(|mut handle| handle.referer("Example-Referer")).unwrap()
/// .send().unwrap();
/// #
/// # assert!(response.is_success());
/// ```
pub fn with_handle<F>(mut self, function: F) -> Result<Self, curl::Error>
where F: FnOnce(&mut Easy) -> Result<(), curl::Error> {
function(&mut self.handle)?;
Ok(self)
}
/// Add a byte-array body to a request using the specified /// Add a byte-array body to a request using the specified
/// `Content-Type`. /// `Content-Type`.
pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self { pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self {