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 /// Add the header for the request.
109 ///
110 /// This method allows you to add the header for the request being built.
111 /// Existing header may be replace with the provided ones.
112 ///
113 /// # Parameters
114 /// - `key`: The key of header
115 /// - `value`: The value of header
116 ///
117 /// # Returns
118 /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
119 #[inline]
120 pub fn header<K, V, T>(&mut self, key: K, value: V) -> &mut Self
121 where
122 T: IntoIterator<Item = (K, V)>,
123 K: Into<String>,
124 V: Into<String>,
125 {
126 if let Some(tmp_header) = Arc::get_mut(&mut self.http_request.header) {
127 tmp_header.insert(key.into(), value.into());
128 }
129 self
130 }
131
132 /// Sets the headers for the request.
133 ///
134 /// This method allows you to specify the headers for the request being built.
135 /// Existing headers may be merged with the provided ones.
136 ///
137 /// # Parameters
138 /// - `header`: The headers to be set for the request.
139 ///
140 /// # Returns
141 /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
142 #[inline]
143 pub fn headers<K, V, T>(&mut self, header: T) -> &mut Self
144 where
145 T: IntoIterator<Item = (K, V)>,
146 K: Into<String>,
147 V: Into<String>,
148 {
149 if let Some(tmp_header) = Arc::get_mut(&mut self.http_request.header) {
150 for (key, value) in header {
151 tmp_header.insert(key.into(), value.into());
152 }
153 }
154 self
155 }
156
157 /// Sets the JSON body of the request.
158 ///
159 /// This method allows you to set the body of the request as JSON data. If there is existing
160 /// body data, it will be replaced with the provided JSON data.
161 ///
162 /// # Parameters
163 /// - `body`: The JSON body data to be set for the request.
164 ///
165 /// # Returns
166 /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
167 #[inline]
168 pub fn json(&mut self, body: BodyJson) -> &mut Self {
169 self.http_request.body = Arc::new(Body::Json(body));
170 self
171 }
172
173 /// Sets the text body of the request.
174 ///
175 /// This method allows you to set the body of the request as plain text. If there is existing
176 /// body data, it will be replaced with the provided text data.
177 ///
178 /// # Parameters
179 /// - `body`: The text body data to be set for the request.
180 ///
181 /// # Returns
182 /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
183 #[inline]
184 pub fn text(&mut self, body: BodyText) -> &mut Self {
185 self.http_request.body = Arc::new(Body::Text(body));
186 self
187 }
188
189 /// Sets the HTTP request body to the given binary content.
190 ///
191 /// This method assigns the provided binary data to the body of the HTTP request.
192 /// The body is wrapped in an `Arc` to enable shared ownership and safe concurrency.
193 ///
194 /// # Parameters
195 ///
196 /// - `body` - A `BodyBinary` representing the binary content to be used as the HTTP request body.
197 ///
198 /// # Returns
199 ///
200 /// Returns a mutable reference to the current instance of the struct, allowing method chaining.
201 ///
202 /// # Notes
203 ///
204 /// This method overrides any previously set body. Use it when you want to assign binary content
205 /// specifically as the body of the HTTP request.
206 #[inline]
207 pub fn body(&mut self, body: BodyBinary) -> &mut Self {
208 self.http_request.body = Arc::new(Body::Binary(body));
209 self
210 }
211
212 /// Sets the timeout value for the current connection.
213 ///
214 /// This method sets the timeout duration for the connection, which is used to determine
215 /// how long the system should wait for a response before considering the connection attempt
216 /// as failed. The timeout value is stored in an `Arc` to allow it to be shared safely across
217 /// multiple threads if needed.
218 ///
219 /// # Parameters
220 ///
221 /// - `timeout`: The timeout duration in seconds. This value will be used to configure the
222 /// connection timeout.
223 ///
224 /// # Returns
225 /// Returns a mutable reference to the `RequestBuilder` to allow method chaining.
226 #[inline]
227 pub fn timeout(&mut self, timeout: u64) -> &mut Self {
228 if let Ok(mut config) = self.http_request.config.write() {
229 config.timeout = timeout;
230 }
231 self
232 }
233
234 /// Enables HTTP redirection for the request.
235 ///
236 /// This method sets the `redirect` property of the `http_request` to `true`.
237 /// It returns a mutable reference to the current instance, allowing method chaining.
238 #[inline]
239 pub fn redirect(&mut self) -> &mut Self {
240 if let Ok(mut config) = self.http_request.config.write() {
241 config.redirect = true;
242 }
243 self
244 }
245
246 /// Unenables HTTP redirection for the request.
247 ///
248 /// This method sets the `redirect` property of the `http_request` to `false`.
249 /// It returns a mutable reference to the current instance, allowing method chaining.
250 #[inline]
251 pub fn unredirect(&mut self) -> &mut Self {
252 if let Ok(mut config) = self.http_request.config.write() {
253 config.redirect = false;
254 };
255 self
256 }
257
258 /// Sets the maximum number of allowed redirections for the HTTP request.
259 ///
260 /// This method updates the `max_redirect_times` field in the configuration and returns a mutable
261 /// reference to `self` to enable method chaining.
262 ///
263 /// # Parameters
264 ///
265 /// - `num` - The maximum number of redirections allowed. A value of `0` disables redirection.
266 ///
267 /// # Returns
268 ///
269 /// A mutable reference to the current instance for method chaining.
270 ///
271 /// # Notes
272 ///
273 /// Ensure that the value provided to `num` is within a valid range. Excessively high values
274 /// may lead to performance issues or unintended behavior.
275 #[inline]
276 pub fn max_redirect_times(&mut self, num: usize) -> &mut Self {
277 if let Ok(mut config) = self.http_request.config.write() {
278 config.max_redirect_times = num;
279 }
280 self
281 }
282
283 /// Sets the buffer size for the HTTP request configuration.
284 ///
285 /// This method allows you to set the size of the buffer used for reading
286 /// the HTTP response. It modifies the `buffer` field of the HTTP request's
287 /// configuration, which will be used when processing the response data.
288 ///
289 /// # Parameters
290 /// - `buffer`: The size of the buffer to be used, in bytes.
291 ///
292 /// # Returns
293 /// Returns a mutable reference to `self`, allowing for method chaining.
294 #[inline]
295 pub fn buffer(&mut self, buffer: usize) -> &mut Self {
296 if let Ok(mut config) = self.http_request.config.write() {
297 config.buffer = buffer;
298 }
299 self
300 }
301
302 /// Enables automatic response decoding.
303 ///
304 /// When enabled, the response body will be automatically decompressed if it is encoded
305 /// using a supported compression format (e.g., `gzip`, `deflate`, `br`).
306 ///
307 /// # Returns
308 /// A mutable reference to the current instance, allowing for method chaining.
309 #[inline]
310 pub fn decode(&mut self) -> &mut Self {
311 if let Ok(mut config) = self.http_request.config.write() {
312 config.decode = true;
313 }
314 self
315 }
316
317 /// Disables automatic response decoding.
318 ///
319 /// When disabled, the response body will not be automatically decompressed,
320 /// and the raw encoded data will be returned as-is.
321 ///
322 /// # Returns
323 /// A mutable reference to the current instance, allowing for method chaining.
324 #[inline]
325 pub fn undecode(&mut self) -> &mut Self {
326 if let Ok(mut config) = self.http_request.config.write() {
327 config.decode = false;
328 }
329 self
330 }
331
332 /// Finalizes the builder and returns a fully constructed `HttpRequest` instance.
333 ///
334 /// This method takes the current configuration stored in `http_request`, creates a new
335 /// `HttpRequest` instance with the configuration, and resets the builder's temporary
336 /// state for further use.
337 ///
338 /// # Returns
339 /// Returns a fully constructed `BoxRequestTrait` instance based on the current builder state.
340 #[inline]
341 pub fn build(&mut self) -> BoxRequestTrait {
342 self.builder = self.http_request.clone();
343 self.http_request = HttpRequest::default();
344 Box::new(self.builder.clone())
345 }
346}