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