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