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