isahc/
request.rs

1use crate::{
2    body::{AsyncBody, Body},
3    client::ResponseFuture,
4    config::{
5        request::{RequestConfig, WithRequestConfig},
6        Configurable,
7    },
8    error::Error,
9};
10use http::{Request, Response};
11
12/// Extension methods on an HTTP request.
13pub trait RequestExt<T> {
14    /// Create a new request builder with the method, URI, and headers cloned
15    /// from this request.
16    ///
17    /// Note that third-party extensions are not cloned.
18    fn to_builder(&self) -> http::request::Builder;
19
20    /// Send the HTTP request synchronously using the default client.
21    ///
22    /// This is a convenience method that is equivalent to
23    /// [`send`](crate::send).
24    ///
25    /// # Examples
26    ///
27    /// ```no_run
28    /// use isahc::{prelude::*, Request};
29    ///
30    /// let response = Request::post("https://httpbin.org/post")
31    ///     .header("Content-Type", "application/json")
32    ///     .body(r#"{
33    ///         "speed": "fast",
34    ///         "cool_name": true
35    ///     }"#)?
36    ///     .send()?;
37    /// # Ok::<(), isahc::Error>(())
38    /// ```
39    fn send(self) -> Result<Response<Body>, Error>
40    where
41        T: Into<Body>;
42
43    /// Sends the HTTP request asynchronously using the default client.
44    ///
45    /// This is a convenience method that is equivalent to
46    /// [`send_async`](crate::send_async).
47    fn send_async(self) -> ResponseFuture<'static>
48    where
49        T: Into<AsyncBody>;
50}
51
52impl<T> RequestExt<T> for Request<T> {
53    fn to_builder(&self) -> http::request::Builder {
54        let mut builder = Request::builder()
55            .method(self.method().clone())
56            .uri(self.uri().clone())
57            .version(self.version());
58
59        *builder.headers_mut().unwrap() = self.headers().clone();
60
61        if let Some(config) = self.extensions().get::<RequestConfig>() {
62            builder = builder.extension(config.clone());
63        }
64
65        #[cfg(feature = "cookies")]
66        {
67            if let Some(cookie_jar) = self.extensions().get::<crate::cookies::CookieJar>() {
68                builder = builder.extension(cookie_jar.clone());
69            }
70        }
71
72        builder
73    }
74
75    fn send(self) -> Result<Response<Body>, Error>
76    where
77        T: Into<Body>,
78    {
79        crate::send(self)
80    }
81
82    fn send_async(self) -> ResponseFuture<'static>
83    where
84        T: Into<AsyncBody>,
85    {
86        crate::send_async(self)
87    }
88}
89
90impl Configurable for http::request::Builder {
91    #[cfg(feature = "cookies")]
92    fn cookie_jar(self, cookie_jar: crate::cookies::CookieJar) -> Self {
93        self.extension(cookie_jar)
94    }
95}
96
97impl WithRequestConfig for http::request::Builder {
98    #[inline]
99    fn with_config(mut self, f: impl FnOnce(&mut RequestConfig)) -> Self {
100        if let Some(extensions) = self.extensions_mut() {
101            if let Some(config) = extensions.get_mut() {
102                f(config);
103            } else {
104                extensions.insert(RequestConfig::default());
105                f(extensions.get_mut().unwrap());
106            }
107        }
108
109        self
110    }
111}