reqsign_http_send_reqwest/
lib.rs

1//! Reqwest-based HTTP client implementation for reqsign.
2//!
3//! This crate provides `ReqwestHttpSend`, an HTTP client that implements
4//! the `HttpSend` trait from `reqsign_core` using the popular reqwest library.
5//!
6//! ## Overview
7//!
8//! `ReqwestHttpSend` enables reqsign to send HTTP requests using reqwest's
9//! powerful and feature-rich HTTP client. It handles the conversion between
10//! standard `http` types and reqwest's types seamlessly.
11//!
12//! ## Example
13//!
14//! ```no_run
15//! use reqsign_core::Context;
16//! use reqsign_http_send_reqwest::ReqwestHttpSend;
17//! use reqwest::Client;
18//!
19//! #[tokio::main]
20//! async fn main() {
21//!     // Use default client
22//!     let ctx = Context::new()
23//!         .with_http_send(ReqwestHttpSend::default());
24//!
25//!     // Or use a custom configured client
26//!     let client = Client::builder()
27//!         .timeout(std::time::Duration::from_secs(30))
28//!         .build()
29//!         .unwrap();
30//!
31//!     let ctx = Context::new()
32//!         .with_http_send(ReqwestHttpSend::new(client));
33//! }
34//! ```
35//!
36//! ## Usage with Service Signers
37//!
38//! ```no_run
39//! use reqsign_core::{Context, Signer};
40//! use reqsign_http_send_reqwest::ReqwestHttpSend;
41//! use bytes::Bytes;
42//!
43//! # async fn example() -> anyhow::Result<()> {
44//! // Create context with reqwest HTTP client
45//! let ctx = Context::new()
46//!     .with_http_send(ReqwestHttpSend::default());
47//!
48//! // The context can send HTTP requests
49//! let req = http::Request::builder()
50//!     .method("GET")
51//!     .uri("https://api.example.com")
52//!     .body(Bytes::new())?;
53//!
54//! let resp = ctx.http_send(req).await?;
55//! println!("Response status: {}", resp.status());
56//! # Ok(())
57//! # }
58//! ```
59//!
60//! ## Custom Client Configuration
61//!
62//! ```no_run
63//! use reqsign_http_send_reqwest::ReqwestHttpSend;
64//! use reqwest::Client;
65//! use std::time::Duration;
66//!
67//! // Configure reqwest client with custom settings
68//! let client = Client::builder()
69//!     .timeout(Duration::from_secs(60))
70//!     .pool_max_idle_per_host(10)
71//!     .user_agent("my-app/1.0")
72//!     .build()
73//!     .unwrap();
74//!
75//! // Use the custom client
76//! let http_send = ReqwestHttpSend::new(client);
77//! ```
78
79use async_trait::async_trait;
80use bytes::Bytes;
81use http_body_util::BodyExt;
82use reqsign_core::{Error, HttpSend, Result};
83use reqwest::{Client, Request};
84
85/// Reqwest-based implementation of the `HttpSend` trait.
86///
87/// This struct wraps a `reqwest::Client` and provides HTTP request
88/// functionality for the reqsign ecosystem.
89#[derive(Debug, Default)]
90pub struct ReqwestHttpSend {
91    client: Client,
92}
93
94impl ReqwestHttpSend {
95    /// Create a new ReqwestHttpSend with a custom reqwest::Client.
96    ///
97    /// This allows you to configure the client with specific settings
98    /// like timeouts, proxies, or custom headers.
99    ///
100    /// # Example
101    ///
102    /// ```no_run
103    /// use reqsign_http_send_reqwest::ReqwestHttpSend;
104    /// use reqwest::Client;
105    ///
106    /// let client = Client::builder()
107    ///     .timeout(std::time::Duration::from_secs(30))
108    ///     .build()
109    ///     .unwrap();
110    ///
111    /// let http_send = ReqwestHttpSend::new(client);
112    /// ```
113    pub fn new(client: Client) -> Self {
114        Self { client }
115    }
116}
117
118#[async_trait]
119impl HttpSend for ReqwestHttpSend {
120    async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>> {
121        let req = Request::try_from(req)
122            .map_err(|e| Error::unexpected("failed to convert request").with_source(e))?;
123        let resp: http::Response<_> = self
124            .client
125            .execute(req)
126            .await
127            .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?
128            .into();
129
130        let (parts, body) = resp.into_parts();
131        let bs = BodyExt::collect(body)
132            .await
133            .map(|buf| buf.to_bytes())
134            .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
135        Ok(http::Response::from_parts(parts, bs))
136    }
137}