Skip to main content

http_request/request/request_builder/
impl.rs

1use super::*;
2
3impl RequestBuilder {
4    /// Create an empty builder (defaults: GET, no headers, default config).
5    pub fn new() -> Self {
6        Self::default()
7    }
8
9    /// Shortcut for `method(Method::Get)` + `url(url)`.
10    ///
11    /// # Arguments
12    ///
13    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
14    ///
15    /// # Returns
16    ///
17    /// - `&mut Self` - A mutable reference to this builder for chaining.
18    pub fn get<T: Into<String>>(&mut self, url: T) -> &mut Self {
19        self.get_mut_request().set_method(Method::Get);
20        self.get_mut_request().set_url(url);
21        self
22    }
23
24    /// Shortcut for `method(Method::Post)` + `url(url)`.
25    ///
26    /// # Arguments
27    ///
28    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
29    ///
30    /// # Returns
31    ///
32    /// - `&mut Self` - A mutable reference to this builder for chaining.
33    pub fn post<T: Into<String>>(&mut self, url: T) -> &mut Self {
34        self.get_mut_request().set_method(Method::Post);
35        self.get_mut_request().set_url(url);
36        self
37    }
38
39    /// Set HTTP method explicitly (`Method::Get` / `Method::Post` / etc.).
40    ///
41    /// # Arguments
42    ///
43    /// - `Method` - The HTTP method to apply to the request.
44    ///
45    /// # Returns
46    ///
47    /// - `&mut Self` - A mutable reference to this builder for chaining.
48    pub fn method(&mut self, method: Method) -> &mut Self {
49        self.get_mut_request().set_method(method);
50        self
51    }
52
53    /// Set URL.
54    ///
55    /// # Arguments
56    ///
57    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
58    ///
59    /// # Returns
60    ///
61    /// - `&mut Self` - A mutable reference to this builder for chaining.
62    pub fn url<T: Into<String>>(&mut self, url: T) -> &mut Self {
63        self.get_mut_request().set_url(url);
64        self
65    }
66
67    /// Set a single header (last write wins on duplicate keys).
68    ///
69    /// # Arguments
70    ///
71    /// - `K` - A header name that can be borrowed as a string slice.
72    /// - `V` - A header value that can be borrowed as a string slice.
73    ///
74    /// # Returns
75    ///
76    /// - `&mut Self` - A mutable reference to this builder for chaining.
77    pub fn header<K: AsRef<str>, V: AsRef<str>>(&mut self, key: K, value: V) -> &mut Self {
78        self.get_mut_request().set_header(key, value);
79        self
80    }
81
82    /// Set many headers at once.
83    ///
84    /// # Arguments
85    ///
86    /// - `HashMap<K, V>` - The header name-value pairs to apply, where each name and
87    ///   value can be borrowed as a string slice.
88    ///
89    /// # Returns
90    ///
91    /// - `&mut Self` - A mutable reference to this builder for chaining.
92    pub fn headers<K, V>(&mut self, headers: HashMap<K, V>) -> &mut Self
93    where
94        K: AsRef<str>,
95        V: AsRef<str>,
96    {
97        for (k, v) in headers {
98            self.get_mut_request().set_header(k, v);
99        }
100        self
101    }
102
103    /// Remove a header by key.
104    ///
105    /// # Arguments
106    ///
107    /// - `K` - The header name to remove, which can be borrowed as a string slice.
108    ///
109    /// # Returns
110    ///
111    /// - `&mut Self` - A mutable reference to this builder for chaining.
112    pub fn remove_header<K: AsRef<str>>(&mut self, key: K) -> &mut Self {
113        self.get_mut_request().remove_header(key);
114        self
115    }
116
117    /// Clear all headers.
118    ///
119    /// # Returns
120    ///
121    /// - `&mut Self` - A mutable reference to this builder for chaining.
122    pub fn clear_headers(&mut self) -> &mut Self {
123        self.get_mut_request().clear_headers();
124        self
125    }
126
127    /// Set raw body bytes.
128    ///
129    /// # Arguments
130    ///
131    /// - `B` - A value convertible into the raw body bytes, such as `Vec<u8>` or
132    ///   `&[u8]`.
133    ///
134    /// # Returns
135    ///
136    /// - `&mut Self` - A mutable reference to this builder for chaining.
137    pub fn body<B: Into<Vec<u8>>>(&mut self, bytes: B) -> &mut Self {
138        self.get_mut_request().set_body(Body::from_bytes(bytes));
139        self
140    }
141
142    /// Set UTF-8 text body (will be encoded per `Content-Type` on send).
143    ///
144    /// # Arguments
145    ///
146    /// - `T` - A value convertible into the text body, such as `&str` or `String`.
147    ///
148    /// # Returns
149    ///
150    /// - `&mut Self` - A mutable reference to this builder for chaining.
151    pub fn body_text<T: Into<String>>(&mut self, text: T) -> &mut Self {
152        self.get_mut_request()
153            .set_body(Body::from_bytes(text.into().into_bytes()));
154        self
155    }
156
157    /// Set JSON body (serialised via `serde_json`).
158    ///
159    /// # Arguments
160    ///
161    /// - `&V` - A reference to the value to serialise as the JSON body.
162    ///
163    /// # Returns
164    ///
165    /// - `&mut Self` - A mutable reference to this builder for chaining.
166    pub fn body_json<V: serde::Serialize>(&mut self, value: &V) -> &mut Self {
167        if let Ok(bytes) = serde_json::to_vec(value) {
168            self.get_mut_request().set_body(Body::from_bytes(bytes));
169        }
170        self
171    }
172
173    /// Set request timeout in milliseconds.
174    ///
175    /// # Arguments
176    ///
177    /// - `u64` - The request timeout in milliseconds.
178    ///
179    /// # Returns
180    ///
181    /// - `&mut Self` - A mutable reference to this builder for chaining.
182    pub fn timeout(&mut self, ms: u64) -> &mut Self {
183        self.get_mut_request().get_mut_config().set_timeout(ms);
184        self
185    }
186
187    /// Set per-read buffer size.
188    ///
189    /// # Arguments
190    ///
191    /// - `usize` - The per-read buffer size in bytes.
192    ///
193    /// # Returns
194    ///
195    /// - `&mut Self` - A mutable reference to this builder for chaining.
196    pub fn buffer_size(&mut self, n: usize) -> &mut Self {
197        self.get_mut_request().get_mut_config().set_buffer_size(n);
198        self
199    }
200
201    /// Force HTTP/1.1.
202    ///
203    /// # Returns
204    ///
205    /// - `&mut Self` - A mutable reference to this builder for chaining.
206    pub fn http1_1_only(&mut self) -> &mut Self {
207        self.get_mut_request()
208            .get_mut_config()
209            .set_http_version(HttpVersion::Http1_1);
210        self
211    }
212
213    /// Force HTTP/2.
214    ///
215    /// # Returns
216    ///
217    /// - `&mut Self` - A mutable reference to this builder for chaining.
218    pub fn http2_only(&mut self) -> &mut Self {
219        self.get_mut_request()
220            .get_mut_config()
221            .set_http_version(HttpVersion::Http2);
222        self
223    }
224
225    /// Enable auto-follow of 3xx redirects.
226    ///
227    /// # Returns
228    ///
229    /// - `&mut Self` - A mutable reference to this builder for chaining.
230    pub fn redirect(&mut self) -> &mut Self {
231        self.get_mut_request().get_mut_config().set_redirect(true);
232        self
233    }
234
235    /// Disable auto-follow of 3xx redirects (default).
236    ///
237    /// # Returns
238    ///
239    /// - `&mut Self` - A mutable reference to this builder for chaining.
240    pub fn no_redirect(&mut self) -> &mut Self {
241        self.get_mut_request().get_mut_config().set_redirect(false);
242        self
243    }
244
245    /// Maximum number of redirects to follow (default `DEFAULT_MAX_REDIRECT_TIMES`).
246    ///
247    /// # Arguments
248    ///
249    /// - `usize` - The maximum number of redirects to follow.
250    ///
251    /// # Returns
252    ///
253    /// - `&mut Self` - A mutable reference to this builder for chaining.
254    pub fn max_redirect_times(&mut self, n: usize) -> &mut Self {
255        self.get_mut_request()
256            .get_mut_config()
257            .set_max_redirect_times(n);
258        self
259    }
260
261    /// Enable automatic response body decompression (gzip / deflate / br).
262    ///
263    /// # Returns
264    ///
265    /// - `&mut Self` - A mutable reference to this builder for chaining.
266    pub fn decode(&mut self) -> &mut Self {
267        self.get_mut_request().get_mut_config().set_decode(true);
268        self
269    }
270
271    /// Disable automatic response body decompression.
272    ///
273    /// # Returns
274    ///
275    /// - `&mut Self` - A mutable reference to this builder for chaining.
276    pub fn no_decode(&mut self) -> &mut Self {
277        self.get_mut_request().get_mut_config().set_decode(false);
278        self
279    }
280
281    /// Set the proxy configuration. Pass `None` (via [`Proxy::default()`) to
282    /// clear an existing proxy.
283    ///
284    /// Construct via [`Proxy::http`] / [`Proxy::https`] / [`Proxy::socks5`]
285    /// and optionally chain `.auth(user, pass)`.
286    ///
287    /// # Arguments
288    ///
289    /// - `Proxy` - The proxy configuration to apply to the request.
290    ///
291    /// # Returns
292    ///
293    /// - `&mut Self` - A mutable reference to this builder for chaining.
294    pub fn proxy(&mut self, proxy: Proxy) -> &mut Self {
295        self.get_mut_request()
296            .get_mut_config()
297            .set_proxy(Some(proxy));
298        self
299    }
300
301    /// Clear the proxy (use direct connection).
302    ///
303    /// # Returns
304    ///
305    /// - `&mut Self` - A mutable reference to this builder for chaining.
306    pub fn no_proxy(&mut self) -> &mut Self {
307        self.get_mut_request().get_mut_config().set_proxy(None);
308        self
309    }
310
311    /// Finalise the builder and return the [`HttpRequest`].
312    ///
313    /// # Returns
314    ///
315    /// - `HttpRequest` - The finalised request, leaving the builder empty.
316    pub fn build(&mut self) -> HttpRequest {
317        mem::take(self.get_mut_request())
318    }
319}