http_request/request/request_builder/
impl.rs

1use crate::*;
2
3impl Default for RequestBuilder {
4    fn default() -> Self {
5        Self {
6            http_request: HttpRequest::default(),
7            builder: HttpRequest::default(),
8        }
9    }
10}
11
12impl RequestBuilder {
13    /// Creates a new instance of the builder with default values.
14    ///
15    /// This method initializes the `RequestBuilder` with default values for all
16    /// fields.
17    ///
18    /// # Returns
19    /// Returns a new instance of `RequestBuilder`.
20    #[inline]
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    /// Sets the HTTP method for the request.
26    ///
27    /// This method allows you to specify the HTTP method (e.g., GET, POST) for the
28    /// request being built.
29    ///
30    /// # Parameters
31    /// - `methods`: The HTTP method to be set for the request.
32    ///
33    /// # Returns
34    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
35    #[inline]
36    pub fn post(&mut self, url: &str) -> &mut Self {
37        self.http_request.methods = Arc::new(Methods::POST);
38        self.url(url);
39        self
40    }
41
42    /// Sets the HTTP method for the request.
43    ///
44    /// This method allows you to specify the HTTP method (e.g., GET, POST) for the
45    /// request being built.
46    ///
47    /// # Parameters
48    /// - `methods`: The HTTP method to be set for the request.
49    ///
50    /// # Returns
51    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
52    #[inline]
53    pub fn get(&mut self, url: &str) -> &mut Self {
54        self.http_request.methods = Arc::new(Methods::GET);
55        self.url(url);
56        self
57    }
58
59    /// Sets the target URL of the request.
60    ///
61    /// This method allows you to specify the URL for the request being built.
62    ///
63    /// # Parameters
64    /// - `url`: The target URL of the request.
65    ///
66    /// # Returns
67    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
68    #[inline]
69    fn url(&mut self, url: &str) -> &mut Self {
70        self.http_request.url = Arc::new(url.to_owned());
71        self
72    }
73
74    /// Sets the HTTP version to 1.1 for the request configuration.
75    ///
76    /// This method updates the HTTP version to `HTTP1_1` for the current
77    /// `http_request` configuration. It allows the user to force the
78    /// request to use HTTP 1.1 only, overriding any other version that may
79    /// have been previously set.
80    ///
81    /// # Returns
82    /// Returns a mutable reference to `self` to allow method chaining.
83    #[inline]
84    pub fn http1_1_only(&mut self) -> &mut Self {
85        if let Ok(mut config) = self.http_request.config.write() {
86            config.http_version = HttpVersion::HTTP1_1;
87        }
88        self
89    }
90
91    /// Sets the HTTP version to 2.0 for the request configuration.
92    ///
93    /// This method updates the HTTP version to `HTTP2` for the current
94    /// `http_request` configuration. It allows the user to force the
95    /// request to use HTTP 2.0 only, overriding any other version that may
96    /// have been previously set.
97    ///
98    /// # Returns
99    /// Returns a mutable reference to `self` to allow method chaining.
100    #[inline]
101    pub fn http2_only(&mut self) -> &mut Self {
102        if let Ok(mut config) = self.http_request.config.write() {
103            config.http_version = HttpVersion::HTTP2;
104        }
105        self
106    }
107
108    /// Sets the headers for the request.
109    ///
110    /// This method allows you to specify the headers for the request being built.
111    /// Existing headers may be merged with the provided ones.
112    ///
113    /// # Parameters
114    /// - `header`: The headers to be set for the request.
115    ///
116    /// # Returns
117    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
118    #[inline]
119    pub fn headers<K, V, T>(&mut self, header: T) -> &mut Self
120    where
121        T: IntoIterator<Item = (K, V)>,
122        K: Into<String>,
123        V: Into<String>,
124    {
125        if let Some(tmp_header) = Arc::get_mut(&mut self.http_request.header) {
126            for (key, value) in header {
127                tmp_header.insert(key.into(), value.into());
128            }
129        }
130        self
131    }
132
133    /// Sets the JSON body of the request.
134    ///
135    /// This method allows you to set the body of the request as JSON data. If there is existing
136    /// body data, it will be replaced with the provided JSON data.
137    ///
138    /// # Parameters
139    /// - `body`: The JSON body data to be set for the request.
140    ///
141    /// # Returns
142    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
143    #[inline]
144    pub fn json(&mut self, body: BodyJson) -> &mut Self {
145        self.http_request.body = Arc::new(Body::Json(body));
146        self
147    }
148
149    /// Sets the text body of the request.
150    ///
151    /// This method allows you to set the body of the request as plain text. If there is existing
152    /// body data, it will be replaced with the provided text data.
153    ///
154    /// # Parameters
155    /// - `body`: The text body data to be set for the request.
156    ///
157    /// # Returns
158    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
159    #[inline]
160    pub fn text(&mut self, body: BodyText) -> &mut Self {
161        self.http_request.body = Arc::new(Body::Text(body));
162        self
163    }
164
165    /// Sets the HTTP request body to the given binary content.
166    ///
167    /// This method assigns the provided binary data to the body of the HTTP request.
168    /// The body is wrapped in an `Arc` to enable shared ownership and safe concurrency.
169    ///
170    /// # Parameters
171    ///
172    /// - `body` - A `BodyBinary` representing the binary content to be used as the HTTP request body.
173    ///
174    /// # Returns
175    ///
176    /// Returns a mutable reference to the current instance of the struct, allowing method chaining.
177    ///
178    /// # Notes
179    ///
180    /// This method overrides any previously set body. Use it when you want to assign binary content
181    /// specifically as the body of the HTTP request.
182    #[inline]
183    pub fn body(&mut self, body: BodyBinary) -> &mut Self {
184        self.http_request.body = Arc::new(Body::Binary(body));
185        self
186    }
187
188    /// Sets the timeout value for the current connection.
189    ///
190    /// This method sets the timeout duration for the connection, which is used to determine
191    /// how long the system should wait for a response before considering the connection attempt
192    /// as failed. The timeout value is stored in an `Arc` to allow it to be shared safely across
193    /// multiple threads if needed.
194    ///
195    /// # Parameters
196    ///
197    /// - `timeout`: The timeout duration in seconds. This value will be used to configure the
198    ///   connection timeout.
199    ///
200    /// # Returns
201    /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
202    #[inline]
203    pub fn timeout(&mut self, timeout: u64) -> &mut Self {
204        if let Ok(mut config) = self.http_request.config.write() {
205            config.timeout = timeout;
206        }
207        self
208    }
209
210    /// Enables HTTP redirection for the request.
211    ///
212    /// This method sets the `redirect` property of the `http_request` to `true`.
213    /// It returns a mutable reference to the current instance, allowing method chaining.
214    #[inline]
215    pub fn redirect(&mut self) -> &mut Self {
216        if let Ok(mut config) = self.http_request.config.write() {
217            config.redirect = true;
218        }
219        self
220    }
221
222    /// Unenables HTTP redirection for the request.
223    ///
224    /// This method sets the `redirect` property of the `http_request` to `false`.
225    /// It returns a mutable reference to the current instance, allowing method chaining.
226    #[inline]
227    pub fn unredirect(&mut self) -> &mut Self {
228        if let Ok(mut config) = self.http_request.config.write() {
229            config.redirect = false;
230        };
231        self
232    }
233
234    /// Sets the maximum number of allowed redirections for the HTTP request.
235    ///
236    /// This method updates the `max_redirect_times` field in the configuration and returns a mutable
237    /// reference to `self` to enable method chaining.
238    ///
239    /// # Parameters
240    ///
241    /// - `num` - The maximum number of redirections allowed. A value of `0` disables redirection.
242    ///
243    /// # Returns
244    ///
245    /// A mutable reference to the current instance for method chaining.
246    ///
247    /// # Notes
248    ///
249    /// Ensure that the value provided to `num` is within a valid range. Excessively high values
250    /// may lead to performance issues or unintended behavior.
251    #[inline]
252    pub fn max_redirect_times(&mut self, num: usize) -> &mut Self {
253        if let Ok(mut config) = self.http_request.config.write() {
254            config.max_redirect_times = num;
255        }
256        self
257    }
258
259    /// Sets the buffer size for the HTTP request configuration.
260    ///
261    /// This method allows you to set the size of the buffer used for reading
262    /// the HTTP response. It modifies the `buffer` field of the HTTP request's
263    /// configuration, which will be used when processing the response data.
264    ///
265    /// # Parameters
266    /// - `buffer`: The size of the buffer to be used, in bytes.
267    ///
268    /// # Returns
269    /// Returns a mutable reference to `self`, allowing for method chaining.
270    #[inline]
271    pub fn buffer(&mut self, buffer: usize) -> &mut Self {
272        if let Ok(mut config) = self.http_request.config.write() {
273            config.buffer = buffer;
274        }
275        self
276    }
277
278    /// Enables automatic response decoding.
279    ///
280    /// When enabled, the response body will be automatically decompressed if it is encoded
281    /// using a supported compression format (e.g., `gzip`, `deflate`, `br`).
282    ///
283    /// # Returns
284    /// A mutable reference to the current instance, allowing for method chaining.
285    #[inline]
286    pub fn decode(&mut self) -> &mut Self {
287        if let Ok(mut config) = self.http_request.config.write() {
288            config.decode = true;
289        }
290        self
291    }
292
293    /// Disables automatic response decoding.
294    ///
295    /// When disabled, the response body will not be automatically decompressed,
296    /// and the raw encoded data will be returned as-is.
297    ///
298    /// # Returns
299    /// A mutable reference to the current instance, allowing for method chaining.
300    #[inline]
301    pub fn undecode(&mut self) -> &mut Self {
302        if let Ok(mut config) = self.http_request.config.write() {
303            config.decode = false;
304        }
305        self
306    }
307
308    /// Finalizes the builder and returns a fully constructed `HttpRequest` instance.
309    ///
310    /// This method takes the current configuration stored in `http_request`, creates a new
311    /// `HttpRequest` instance with the configuration, and resets the builder's temporary
312    /// state for further use.
313    ///
314    /// # Returns
315    /// Returns a fully constructed `BoxRequestTrait` instance based on the current builder state.
316    #[inline]
317    pub fn build(&mut self) -> BoxRequestTrait {
318        self.builder = self.http_request.clone();
319        self.http_request = HttpRequest::default();
320        Box::new(self.builder.clone())
321    }
322}