fetch-kit 0.2.0

Resilient HTTP client for Rust — retry, circuit breaker, connection pooling, and typed JSON helpers built on reqwest
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
//! The core middleware chain: the [`Middleware`] trait, the [`Next`]
//! continuation, and the [`ClientWithMiddleware`] runner.
//!
//! The shape intentionally mirrors `reqwest-middleware` 0.5 so that
//! migrating an existing middleware is mechanical: swap the import path
//! from `reqwest_middleware::{Middleware, Next, ...}` to
//! `fetch_kit::middleware::{Middleware, Next, ...}` and the signature is
//! unchanged.
//!
//! # Ordering semantics
//!
//! Middleware form an onion around the transport. The middleware
//! registered **first** is the **outermost** layer: it observes the
//! request first and the response last.
//!
//! ```text
//! ClientBuilder::new(client)
//!     .with(AuthMiddleware)      // 1st registered → outermost
//!     .with(RetryMiddleware)     // 2nd registered
//!     .build();
//!
//! request  →  Auth  →  Retry  →  transport
//! response ←  Auth  ←  Retry  ←  ╹
//! ```

use std::future::Future;
use std::sync::Arc;

use async_trait::async_trait;
use http::Extensions;
use reqwest::{Method, Request, Response};

/// Result alias used throughout the middleware stack.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Error type produced by the middleware stack.
///
/// Mirrors `reqwest-middleware`'s error: either the underlying
/// `reqwest::Error`, or an opaque error raised by a middleware
/// (e.g. [`crate::FetchError::CircuitOpen`]).
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// A middleware produced an error.
    #[error("middleware failed: {0}")]
    Middleware(#[from] Box<dyn std::error::Error + Send + Sync>),

    /// The underlying `reqwest` client produced an error.
    #[error(transparent)]
    Reqwest(#[from] reqwest::Error),
}

/// Async middleware that wraps a [`ClientWithMiddleware`].
///
/// Implement `handle` to inspect or rewrite the request before it travels
/// inward, and/or the response (or error) on the way back out. Call
/// [`Next::run`] to continue the chain; returning without calling it
/// short-circuits everything inward (including the transport).
///
/// The `extensions` argument is a per-logical-request typed map shared by
/// every middleware in the chain (and across all retry attempts of one
/// logical request); use it for cross-middleware state. Request-scoped
/// typed data set via [`RequestBuilder::with_extension`] arrives in this
/// map.
#[async_trait]
pub trait Middleware: 'static + Send + Sync {
    /// Handle the request, optionally calling `next` to continue the chain.
    async fn handle(
        &self,
        req: Request,
        extensions: &mut Extensions,
        next: Next<'_>,
    ) -> Result<Response>;
}

/// The continuation passed to [`Middleware::handle`].
///
/// `run` executes the remainder of the chain (the middleware registered
/// after this one, then the transport). `Next` is cheap to clone, which is
/// how retry-style middleware issue multiple attempts.
#[derive(Clone)]
pub struct Next<'a> {
    client: &'a ClientWithMiddleware,
    remaining: &'a [Arc<dyn Middleware>],
}

impl Next<'_> {
    pub(crate) fn new<'a>(
        client: &'a ClientWithMiddleware,
        remaining: &'a [Arc<dyn Middleware>],
    ) -> Next<'a> {
        Next { client, remaining }
    }

    /// Continue the chain with the (possibly rewritten) request.
    pub fn run(
        self,
        req: Request,
        extensions: &mut Extensions,
    ) -> impl Future<Output = Result<Response>> {
        let client = self.client;
        let remaining = self.remaining;
        async move { execute_with_chain(client, remaining, req, extensions).await }
    }
}

/// Execute `req` through `middleware` and finally `client`.
pub(crate) async fn execute_with_chain(
    client: &ClientWithMiddleware,
    middleware: &[Arc<dyn Middleware>],
    req: Request,
    extensions: &mut Extensions,
) -> Result<Response> {
    if let Some((current, rest)) = middleware.split_first() {
        current
            .handle(req, extensions, Next::new(client, rest))
            .await
    } else {
        client.client.execute(req).await.map_err(Error::Reqwest)
    }
}

/// A `reqwest::Client` wrapped in a middleware chain.
///
/// Cloneable (all clones share the same chain and connection pool) and
/// `Debug`. Built with [`ClientBuilder`].
#[derive(Clone)]
pub struct ClientWithMiddleware {
    client: reqwest::Client,
    middleware: Arc<[Arc<dyn Middleware>]>,
}

impl ClientWithMiddleware {
    pub(crate) fn new(client: reqwest::Client, middleware: Arc<[Arc<dyn Middleware>]>) -> Self {
        Self { client, middleware }
    }

    /// The number of middleware registered on this client.
    pub fn middleware_count(&self) -> usize {
        self.middleware.len()
    }

    /// Return a new client with `middleware` appended **inside** the
    /// existing chain (closest to the transport).
    ///
    /// This is a composition point for clients that were already built
    /// (e.g. [`crate::Client::inner`]). When building from scratch, prefer
    /// [`ClientBuilder::with`], which registers middleware outermost-first.
    pub fn with_middleware(mut self, middleware: Arc<dyn Middleware>) -> Self {
        let mut mws = Vec::with_capacity(self.middleware.len() + 1);
        mws.extend(self.middleware.iter().cloned());
        mws.push(middleware);
        self.middleware = mws.into();
        self
    }

    /// Execute the request through the middleware chain with a fresh,
    /// empty per-request extension map.
    pub async fn execute(&self, req: Request) -> Result<Response> {
        let mut extensions = Extensions::new();
        self.execute_with_extensions(req, &mut extensions).await
    }

    /// Execute the request through the middleware chain, seeding the
    /// per-request extension map with `extensions`.
    ///
    /// The map is shared by every middleware in the chain and survives
    /// across retry attempts of the same logical request; it is the
    /// request-scoped typed map that [`RequestBuilder::with_extension`]
    /// populates.
    pub async fn execute_with_extensions(
        &self,
        req: Request,
        extensions: &mut Extensions,
    ) -> Result<Response> {
        execute_with_chain(self, &self.middleware, req, extensions).await
    }

    /// Convenience method to make a `GET` request to a URL.
    pub fn get(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::GET, url)
    }

    /// Convenience method to make a `POST` request to a URL.
    pub fn post(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::POST, url)
    }

    /// Convenience method to make a `PUT` request to a URL.
    pub fn put(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::PUT, url)
    }

    /// Convenience method to make a `PATCH` request to a URL.
    pub fn patch(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::PATCH, url)
    }

    /// Convenience method to make a `HEAD` request to a URL.
    pub fn head(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::HEAD, url)
    }

    /// Convenience method to make a `DELETE` request to a URL.
    pub fn delete(&self, url: impl Into<String>) -> RequestBuilder {
        self.request(Method::DELETE, url)
    }

    /// Start building a request with the given method and URL.
    pub fn request(&self, method: Method, url: impl Into<String>) -> RequestBuilder {
        RequestBuilder {
            client: self.clone(),
            inner: self.client.request(method, url.into()),
            extensions: Vec::new(),
        }
    }
}

impl std::fmt::Debug for ClientWithMiddleware {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientWithMiddleware")
            .field("client", &self.client)
            .field("middleware", &self.middleware.len())
            .finish()
    }
}

/// Builder for [`ClientWithMiddleware`]: wrap a `reqwest::Client` with
/// middleware, **first-registered = outermost**.
///
/// Mirrors `reqwest-middleware`'s `ClientBuilder` so migration is
/// mechanical.
#[derive(Clone)]
pub struct ClientBuilder {
    client: reqwest::Client,
    middleware: Vec<Arc<dyn Middleware>>,
}

impl ClientBuilder {
    /// Wrap the given `reqwest::Client` with a middleware chain.
    pub fn new(client: reqwest::Client) -> Self {
        Self {
            client,
            middleware: Vec::new(),
        }
    }

    /// Register a middleware. The first middleware registered runs
    /// outermost (sees the request first, the response last).
    pub fn with<M: Middleware>(self, middleware: M) -> Self {
        self.with_middleware(Arc::new(middleware))
    }

    /// Register an already-erased middleware (same ordering as
    /// [`ClientBuilder::with`]).
    pub fn with_middleware(mut self, middleware: Arc<dyn Middleware>) -> Self {
        self.middleware.push(middleware);
        self
    }

    /// Build the [`ClientWithMiddleware`].
    pub fn build(self) -> ClientWithMiddleware {
        ClientWithMiddleware::new(self.client, self.middleware.into())
    }
}

/// One registered "insert this typed value into the request-scoped
/// extensions map" operation, applied when the request is sent.
type ExtensionApplier = Arc<dyn Fn(&mut Extensions) + Send + Sync>;

/// A request builder bound to a [`ClientWithMiddleware`].
///
/// Wraps `reqwest::RequestBuilder` and adds
/// [`RequestBuilder::with_extension`] for request-scoped typed data that
/// every middleware can read (`extensions.get::<T>()` in
/// [`Middleware::handle`]).
///
/// Not `Clone` (reqwest's builder isn't either); use
/// [`RequestBuilder::try_clone`] for bufferable requests.
pub struct RequestBuilder {
    client: ClientWithMiddleware,
    inner: reqwest::RequestBuilder,
    extensions: Vec<ExtensionApplier>,
}

impl RequestBuilder {
    /// Attach request-scoped typed data, visible to every middleware in
    /// the chain through the `extensions` argument of
    /// [`Middleware::handle`] (`extensions.get::<T>()`). The map is shared
    /// across retry attempts of the same logical request, so the data
    /// survives retries.
    pub fn with_extension<T: Clone + Send + Sync + 'static>(mut self, value: T) -> Self {
        self.extensions.push(Arc::new(move |ext: &mut Extensions| {
            ext.insert(value.clone());
        }));
        self
    }

    /// Add a single header.
    pub fn header<K, V>(mut self, key: K, value: V) -> Self
    where
        http::header::HeaderName: TryFrom<K>,
        <http::header::HeaderName as TryFrom<K>>::Error: Into<http::Error>,
        http::HeaderValue: TryFrom<V>,
        <http::HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
    {
        self.inner = self.inner.header(key, value);
        self
    }

    /// Set all headers from a `HeaderMap`.
    pub fn headers(mut self, headers: reqwest::header::HeaderMap) -> Self {
        self.inner = self.inner.headers(headers);
        self
    }

    /// Append query parameters.
    pub fn query<T: serde::Serialize + ?Sized>(mut self, params: &T) -> Self {
        self.inner = self.inner.query(params);
        self
    }

    /// Override the request timeout.
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.inner = self.inner.timeout(timeout);
        self
    }

    /// Set a JSON body (requires the `json` feature).
    #[cfg(feature = "json")]
    pub fn json<T: serde::Serialize + ?Sized>(mut self, body: &T) -> Self {
        self.inner = self.inner.json(body);
        self
    }

    /// Set a `Bearer` authorization token.
    pub fn bearer_auth(mut self, token: impl std::fmt::Display) -> Self {
        self.inner = self.inner.bearer_auth(token);
        self
    }

    /// Set HTTP basic auth credentials.
    pub fn basic_auth<U, P>(mut self, username: U, password: Option<P>) -> Self
    where
        U: std::fmt::Display,
        P: std::fmt::Display,
    {
        self.inner = self.inner.basic_auth(username, password);
        self
    }

    /// Set the request body directly.
    pub fn body(mut self, body: impl Into<reqwest::Body>) -> Self {
        self.inner = self.inner.body(body);
        self
    }

    /// Set form-encoded body from a serializable value.
    pub fn form<T: serde::Serialize + ?Sized>(mut self, form: &T) -> Self {
        self.inner = self.inner.form(form);
        self
    }

    /// Set a multipart form body (requires the `multipart` feature).
    #[cfg(feature = "multipart")]
    pub fn multipart(mut self, form: reqwest::multipart::Form) -> Self {
        self.inner = self.inner.multipart(form);
        self
    }

    /// Build the request without sending it.
    ///
    /// Extension values registered via [`RequestBuilder::with_extension`]
    /// are applied by [`RequestBuilder::send`] (they seed the per-request
    /// extension map passed to middleware, which reqwest's `Request` type
    /// does not expose).
    pub fn build(self) -> Result<Request> {
        Ok(self.inner.build()?)
    }

    /// Clone the builder, if the underlying request is cloneable
    /// (buffered bodies are; streaming bodies are not).
    pub fn try_clone(&self) -> Option<Self> {
        Some(Self {
            client: self.client.clone(),
            inner: self.inner.try_clone()?,
            extensions: self.extensions.clone(),
        })
    }

    /// Build and execute the request through the middleware chain,
    /// seeding the per-request extension map with any
    /// [`RequestBuilder::with_extension`] values.
    pub async fn send(self) -> Result<Response> {
        let Self {
            client,
            inner,
            extensions: appliers,
        } = self;
        let request = inner.build()?;
        let mut extensions = Extensions::new();
        for apply in &appliers {
            apply(&mut extensions);
        }
        client
            .execute_with_extensions(request, &mut extensions)
            .await
    }
}