reqx 0.1.41

Rust HTTP transport client for API SDK libraries with retry, timeout, idempotency, proxy, and pluggable TLS backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::error::Error as StdError;
use std::time::Duration;

use bytes::Bytes;
use futures_core::Stream;
use http::header::{CONTENT_LENGTH, CONTENT_TYPE, HeaderName, HeaderValue};
use http::{HeaderMap, Method};
use serde::Serialize;
use serde::de::DeserializeOwned;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::io::ReaderStream;

use crate::IDEMPOTENCY_KEY_HEADER;
use crate::body::{RequestBody, stream_req_body};
use crate::client::Client;
use crate::core::request_builder::{
    PreparedRequest, RequestExecutionOptions, RequestExecutionOverrides, RequestPreparation,
};
use crate::policy::{RedirectPolicy, StatusPolicy};
use crate::retry::RetryPolicy;
use crate::util::{mark_sensitive_header_value, parse_header_name, parse_header_value};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ContentLengthSource {
    None,
    RequestHelper,
    User,
}

/// Builds and executes a single request against an existing [`Client`].
///
/// Create a builder from [`Client::request`] or the verb helpers such as
/// [`Client::get`] and [`Client::post`].
///
/// See also:
///
/// - `examples/request_helpers.rs`
/// - `examples/request_overrides.rs`
/// - `examples/streaming.rs`
///
/// # Example
///
/// ```no_run
/// # #[cfg(feature = "_async")]
/// # async fn demo() -> reqx::Result<()> {
/// use reqx::prelude::Client;
///
/// let client = Client::builder("https://api.example.com").build()?;
/// let response = client
///     .post("/v1/items")
///     .idempotency_key("item-1")?
///     .query_pair("verbose", "true")
///     .json(&serde_json::json!({ "name": "demo" }))?
///     .send_response()
///     .await?;
///
/// let _status = response.status();
/// # Ok(())
/// # }
/// ```
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "async-tls-rustls-ring",
        feature = "async-tls-rustls-aws-lc-rs",
        feature = "async-tls-native"
    )))
)]
#[must_use = "request builders do nothing until you call a send method"]
pub struct RequestBuilder<'a> {
    client: &'a Client,
    method: Method,
    path: String,
    query_pairs: Vec<(String, String)>,
    headers: HeaderMap,
    body: Option<RequestBody>,
    content_length_source: ContentLengthSource,
    execution_overrides: RequestExecutionOverrides,
}

impl<'a> RequestBuilder<'a> {
    pub(crate) fn new(client: &'a Client, method: Method, path: String) -> Self {
        Self {
            client,
            method,
            path,
            query_pairs: Vec::new(),
            headers: HeaderMap::new(),
            body: None,
            content_length_source: ContentLengthSource::None,
            execution_overrides: RequestExecutionOverrides::default(),
        }
    }

    /// Adds a header to this request.
    ///
    /// See also `examples/request_helpers.rs`.
    pub fn header(mut self, name: HeaderName, mut value: HeaderValue) -> Self {
        mark_sensitive_header_value(&name, &mut value);
        if name == CONTENT_LENGTH {
            self.content_length_source = ContentLengthSource::User;
        }
        self.headers.insert(name, value);
        self
    }

    /// Parses and adds a header to this request.
    pub fn try_header(self, name: &str, value: &str) -> crate::Result<Self> {
        let name = parse_header_name(name)?;
        let value = parse_header_value(name.as_str(), value)?;
        Ok(self.header(name, value))
    }

    /// Sets the `Idempotency-Key` header for retry-safe mutations.
    pub fn idempotency_key(self, key: &str) -> crate::Result<Self> {
        self.try_header(IDEMPOTENCY_KEY_HEADER, key)
    }

    /// Appends one query parameter pair.
    pub fn query_pair(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.query_pairs.push((name.into(), value.into()));
        self
    }

    /// Appends multiple query parameter pairs.
    pub fn query_pairs<K, V, I>(mut self, pairs: I) -> Self
    where
        K: Into<String>,
        V: Into<String>,
        I: IntoIterator<Item = (K, V)>,
    {
        self.query_pairs.extend(
            pairs
                .into_iter()
                .map(|(name, value)| (name.into(), value.into())),
        );
        self
    }

    /// Serializes and appends query parameters from `params`.
    pub fn query<T>(mut self, params: &T) -> crate::Result<Self>
    where
        T: Serialize + ?Sized,
    {
        let encoded = serde_urlencoded::to_string(params)
            .map_err(|source| crate::error::Error::SerializeQuery { source })?;
        self.query_pairs.extend(
            url::form_urlencoded::parse(encoded.as_bytes())
                .map(|(name, value)| (name.into_owned(), value.into_owned())),
        );
        Ok(self)
    }

    /// Sets a fully buffered request body.
    pub fn body(mut self, body: impl Into<Bytes>) -> Self {
        self.clear_content_length();
        self.body = Some(RequestBody::Buffered(body.into()));
        self
    }

    /// Sets a streaming request body.
    pub fn body_stream<S, E>(mut self, stream: S) -> Self
    where
        S: Stream<Item = Result<Bytes, E>> + Send + 'static,
        E: StdError + Send + Sync + 'static,
    {
        if self.content_length_source == ContentLengthSource::RequestHelper {
            self.clear_content_length();
        }
        self.body = Some(RequestBody::Streaming(stream_req_body(stream)));
        self
    }

    /// Streams an async reader as the request body.
    ///
    /// See also `examples/streaming.rs`.
    pub fn body_reader<R>(self, reader: R) -> Self
    where
        R: AsyncRead + Send + 'static,
    {
        let mut builder = self;
        builder.clear_content_length();
        builder.body_stream(ReaderStream::new(reader))
    }

    /// Streams an async reader as the request body and sets `Content-Length`.
    pub fn body_reader_with_length<R>(self, reader: R, content_length: u64) -> crate::Result<Self>
    where
        R: AsyncRead + Send + 'static,
    {
        let value = HeaderValue::from_str(&content_length.to_string()).map_err(|source| {
            crate::error::Error::InvalidHeaderValue {
                name: CONTENT_LENGTH.as_str().to_owned(),
                source,
            }
        })?;
        let mut builder = self.body_reader(reader);
        builder.set_helper_content_length(value);
        Ok(builder)
    }

    fn body_bytes(mut self, body: Bytes) -> Self {
        self.clear_content_length();
        self.body = Some(RequestBody::Buffered(body));
        self
    }

    fn clear_content_length(&mut self) {
        self.headers.remove(CONTENT_LENGTH);
        self.content_length_source = ContentLengthSource::None;
    }

    fn set_helper_content_length(&mut self, value: HeaderValue) {
        self.headers.insert(CONTENT_LENGTH, value);
        self.content_length_source = ContentLengthSource::RequestHelper;
    }

    /// Serializes `payload` as JSON and sets `Content-Type: application/json`.
    ///
    /// See also `examples/basic_json.rs`.
    pub fn json<T>(self, payload: &T) -> crate::Result<Self>
    where
        T: Serialize + ?Sized,
    {
        let body = serde_json::to_vec(payload)
            .map_err(|source| crate::error::Error::SerializeJson { source })?;
        let with_body = self.body_bytes(Bytes::from(body));
        Ok(with_body.header(CONTENT_TYPE, HeaderValue::from_static("application/json")))
    }

    /// Serializes `payload` as form data and sets the form content type.
    pub fn form<T>(self, payload: &T) -> crate::Result<Self>
    where
        T: Serialize + ?Sized,
    {
        let encoded = serde_urlencoded::to_string(payload)
            .map_err(|source| crate::error::Error::SerializeForm { source })?;
        let with_body = self.body_bytes(Bytes::from(encoded));
        Ok(with_body.header(
            CONTENT_TYPE,
            HeaderValue::from_static("application/x-www-form-urlencoded"),
        ))
    }

    /// Overrides the per-attempt request timeout for this request.
    ///
    /// A zero duration is rejected by `send` or `send_stream`.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.execution_overrides.request_timeout = Some(timeout);
        self
    }

    /// Overrides the overall deadline for this request.
    ///
    /// A zero duration is rejected by `send` or `send_stream`.
    pub fn total_timeout(mut self, total_timeout: Duration) -> Self {
        self.execution_overrides.total_timeout = Some(total_timeout);
        self
    }

    /// Overrides the buffered response body size limit for this request.
    pub fn max_response_body_bytes(mut self, max_response_body_bytes: usize) -> Self {
        self.execution_overrides.max_response_body_bytes = Some(max_response_body_bytes);
        self
    }

    /// Overrides the retry policy for this request.
    ///
    /// See also `examples/request_overrides.rs`.
    pub fn retry_policy(mut self, retry_policy: RetryPolicy) -> Self {
        self.execution_overrides.retry_policy = Some(retry_policy);
        self
    }

    /// Overrides redirect handling for this request.
    pub fn redirect_policy(mut self, redirect_policy: RedirectPolicy) -> Self {
        self.execution_overrides.redirect_policy = Some(redirect_policy);
        self
    }

    /// Overrides status handling for this request.
    pub fn status_policy(mut self, status_policy: StatusPolicy) -> Self {
        self.execution_overrides.status_policy = Some(status_policy);
        self
    }

    /// Overrides automatic `Accept-Encoding` injection for this request.
    pub fn auto_accept_encoding(mut self, enabled: bool) -> Self {
        self.execution_overrides.auto_accept_encoding = Some(enabled);
        self
    }

    fn into_prepared_request(
        self,
        forced_status_policy: Option<StatusPolicy>,
    ) -> PreparedRequest<'a, Client, RequestBody, RequestExecutionOptions> {
        RequestPreparation {
            client: self.client,
            method: self.method,
            path: self.path,
            query_pairs: self.query_pairs,
            headers: self.headers,
            body: self.body,
            execution_overrides: self.execution_overrides,
        }
        .prepare(forced_status_policy, RequestExecutionOptions::from)
    }

    /// Executes the request and applies the effective [`StatusPolicy`].
    pub async fn send(self) -> crate::Result<crate::response::Response> {
        let PreparedRequest {
            client,
            method,
            path,
            headers,
            body,
            execution_options,
        } = self.into_prepared_request(None);
        client
            .send_request(method, path, headers, body, execution_options)
            .await
    }

    /// Executes the request and returns a streaming response body.
    ///
    /// Non-success HTTP statuses still follow the effective [`StatusPolicy`].
    /// See also `examples/streaming.rs`.
    pub async fn send_stream(self) -> crate::Result<crate::response::ResponseStream> {
        let PreparedRequest {
            client,
            method,
            path,
            headers,
            body,
            execution_options,
        } = self.into_prepared_request(None);
        client
            .send_request_stream(method, path, headers, body, execution_options)
            .await
    }

    /// Streams the response body into `writer`.
    ///
    /// See also `examples/streaming.rs`.
    pub async fn download_to_writer<W>(self, writer: &mut W) -> crate::Result<u64>
    where
        W: AsyncWrite + Unpin + Send + ?Sized,
    {
        self.send_stream().await?.copy_to_writer(writer).await
    }

    /// Streams the response body into `writer`, enforcing `max_bytes`.
    ///
    /// See also `examples/streaming.rs`.
    pub async fn download_to_writer_limited<W>(
        self,
        writer: &mut W,
        max_bytes: usize,
    ) -> crate::Result<u64>
    where
        W: AsyncWrite + Unpin + Send + ?Sized,
    {
        self.send_stream()
            .await?
            .copy_to_writer_limited(writer, max_bytes)
            .await
    }

    /// Executes the request, buffers the body, and deserializes it as JSON.
    pub async fn send_json<T>(self) -> crate::Result<T>
    where
        T: DeserializeOwned,
    {
        let response = self.send().await?;
        response.json()
    }

    /// Executes the request and always returns a buffered [`crate::Response`]
    /// for HTTP status responses.
    pub async fn send_response(self) -> crate::Result<crate::response::Response> {
        let PreparedRequest {
            client,
            method,
            path,
            headers,
            body,
            execution_options,
        } = self.into_prepared_request(Some(StatusPolicy::Response));
        client
            .send_request(method, path, headers, body, execution_options)
            .await
    }

    /// Executes the request and always returns a streaming response for
    /// HTTP status responses.
    pub async fn send_response_stream(self) -> crate::Result<crate::response::ResponseStream> {
        let PreparedRequest {
            client,
            method,
            path,
            headers,
            body,
            execution_options,
        } = self.into_prepared_request(Some(StatusPolicy::Response));
        client
            .send_request_stream(method, path, headers, body, execution_options)
            .await
    }
}