reqsign_http_send_reqwest/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Reqwest-based HTTP client implementation for reqsign.
19//!
20//! This crate provides `ReqwestHttpSend`, an HTTP client that implements
21//! the `HttpSend` trait from `reqsign_core` using the popular reqwest library.
22//!
23//! ## Overview
24//!
25//! `ReqwestHttpSend` enables reqsign to send HTTP requests using reqwest's
26//! powerful and feature-rich HTTP client. It handles the conversion between
27//! standard `http` types and reqwest's types seamlessly.
28//!
29//! ## Example
30//!
31//! ```no_run
32//! use reqsign_core::Context;
33//! use reqsign_http_send_reqwest::ReqwestHttpSend;
34//! use reqwest::Client;
35//!
36//! #[tokio::main]
37//! async fn main() {
38//!     // Use default client
39//!     let ctx = Context::new()
40//!         .with_http_send(ReqwestHttpSend::default());
41//!
42//!     // Or use a custom configured client
43//!     let client = Client::builder()
44//!         .timeout(std::time::Duration::from_secs(30))
45//!         .build()
46//!         .unwrap();
47//!
48//!     let ctx = Context::new()
49//!         .with_http_send(ReqwestHttpSend::new(client));
50//! }
51//! ```
52//!
53//! ## Usage with Service Signers
54//!
55//! ```no_run
56//! use reqsign_core::{Context, Signer};
57//! use reqsign_http_send_reqwest::ReqwestHttpSend;
58//! use bytes::Bytes;
59//!
60//! # async fn example() -> anyhow::Result<()> {
61//! // Create context with reqwest HTTP client
62//! let ctx = Context::new()
63//!     .with_http_send(ReqwestHttpSend::default());
64//!
65//! // The context can send HTTP requests
66//! let req = http::Request::builder()
67//!     .method("GET")
68//!     .uri("https://api.example.com")
69//!     .body(Bytes::new())?;
70//!
71//! let resp = ctx.http_send(req).await?;
72//! println!("Response status: {}", resp.status());
73//! # Ok(())
74//! # }
75//! ```
76//!
77//! ## Custom Client Configuration
78//!
79//! ```no_run
80//! use reqsign_http_send_reqwest::ReqwestHttpSend;
81//! use reqwest::Client;
82//! use std::time::Duration;
83//!
84//! // Configure reqwest client with custom settings
85//! let client = Client::builder()
86//!     .timeout(Duration::from_secs(60))
87//!     .pool_max_idle_per_host(10)
88//!     .user_agent("my-app/1.0")
89//!     .build()
90//!     .unwrap();
91//!
92//! // Use the custom client
93//! let http_send = ReqwestHttpSend::new(client);
94//! ```
95
96use async_trait::async_trait;
97use bytes::Bytes;
98#[cfg(target_arch = "wasm32")]
99use futures_channel::oneshot;
100#[cfg(not(target_arch = "wasm32"))]
101use http_body_util::BodyExt;
102use reqsign_core::{Error, HttpSend, Result};
103use reqwest::{Client, Request};
104#[cfg(target_arch = "wasm32")]
105use wasm_bindgen_futures::spawn_local;
106
107/// Reqwest-based implementation of the `HttpSend` trait.
108///
109/// This struct wraps a `reqwest::Client` and provides HTTP request
110/// functionality for the reqsign ecosystem.
111#[derive(Debug, Default)]
112pub struct ReqwestHttpSend {
113    client: Client,
114}
115
116impl ReqwestHttpSend {
117    /// Create a new ReqwestHttpSend with a custom reqwest::Client.
118    ///
119    /// This allows you to configure the client with specific settings
120    /// like timeouts, proxies, or custom headers.
121    ///
122    /// # Example
123    ///
124    /// ```no_run
125    /// use reqsign_http_send_reqwest::ReqwestHttpSend;
126    /// use reqwest::Client;
127    ///
128    /// let client = Client::builder()
129    ///     .timeout(std::time::Duration::from_secs(30))
130    ///     .build()
131    ///     .unwrap();
132    ///
133    /// let http_send = ReqwestHttpSend::new(client);
134    /// ```
135    pub fn new(client: Client) -> Self {
136        Self { client }
137    }
138}
139
140#[async_trait]
141impl HttpSend for ReqwestHttpSend {
142    async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>> {
143        let req = Request::try_from(req)
144            .map_err(|e| Error::unexpected("failed to convert request").with_source(e))?;
145
146        #[cfg(not(target_arch = "wasm32"))]
147        {
148            return http_send_native(&self.client, req).await;
149        }
150
151        #[cfg(target_arch = "wasm32")]
152        {
153            return http_send_wasm(&self.client, req).await;
154        }
155    }
156}
157
158#[cfg(not(target_arch = "wasm32"))]
159async fn http_send_native(client: &Client, req: Request) -> Result<http::Response<Bytes>> {
160    let resp = client
161        .execute(req)
162        .await
163        .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?;
164
165    let resp: http::Response<_> = resp.into();
166    let (parts, body) = resp.into_parts();
167    let bs = BodyExt::collect(body)
168        .await
169        .map(|buf| buf.to_bytes())
170        .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
171    Ok(http::Response::from_parts(parts, bs))
172}
173
174#[cfg(target_arch = "wasm32")]
175async fn http_send_wasm(client: &Client, req: Request) -> Result<http::Response<Bytes>> {
176    let (tx, rx) = oneshot::channel();
177    let client = client.clone();
178
179    // reqwest's wasm client is !Send, so drive the request on the local executor
180    // and forward the result back through a channel to satisfy HttpSend's Send requirement.
181    spawn_local(async move {
182        let result = async move {
183            let resp = client
184                .execute(req)
185                .await
186                .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?;
187
188            let status = resp.status();
189            let headers = resp.headers().clone();
190            let body = resp
191                .bytes()
192                .await
193                .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
194
195            let mut response = http::Response::builder()
196                .status(status)
197                .body(body)
198                .map_err(|e| Error::unexpected("failed to build HTTP response").with_source(e))?;
199            *response.headers_mut() = headers;
200            Ok(response)
201        }
202        .await;
203
204        let _ = tx.send(result);
205    });
206
207    rx.await
208        .map_err(|_| Error::unexpected("failed to receive response from wasm task"))?
209}