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;
98use http_body_util::BodyExt;
99use reqsign_core::{Error, HttpSend, Result};
100use reqwest::{Client, Request};
101
102/// Reqwest-based implementation of the `HttpSend` trait.
103///
104/// This struct wraps a `reqwest::Client` and provides HTTP request
105/// functionality for the reqsign ecosystem.
106#[derive(Debug, Default)]
107pub struct ReqwestHttpSend {
108    client: Client,
109}
110
111impl ReqwestHttpSend {
112    /// Create a new ReqwestHttpSend with a custom reqwest::Client.
113    ///
114    /// This allows you to configure the client with specific settings
115    /// like timeouts, proxies, or custom headers.
116    ///
117    /// # Example
118    ///
119    /// ```no_run
120    /// use reqsign_http_send_reqwest::ReqwestHttpSend;
121    /// use reqwest::Client;
122    ///
123    /// let client = Client::builder()
124    ///     .timeout(std::time::Duration::from_secs(30))
125    ///     .build()
126    ///     .unwrap();
127    ///
128    /// let http_send = ReqwestHttpSend::new(client);
129    /// ```
130    pub fn new(client: Client) -> Self {
131        Self { client }
132    }
133}
134
135#[async_trait]
136impl HttpSend for ReqwestHttpSend {
137    async fn http_send(&self, req: http::Request<Bytes>) -> Result<http::Response<Bytes>> {
138        let req = Request::try_from(req)
139            .map_err(|e| Error::unexpected("failed to convert request").with_source(e))?;
140        let resp: http::Response<_> = self
141            .client
142            .execute(req)
143            .await
144            .map_err(|e| Error::unexpected("failed to send HTTP request").with_source(e))?
145            .into();
146
147        let (parts, body) = resp.into_parts();
148        let bs = BodyExt::collect(body)
149            .await
150            .map(|buf| buf.to_bytes())
151            .map_err(|e| Error::unexpected("failed to collect response body").with_source(e))?;
152        Ok(http::Response::from_parts(parts, bs))
153    }
154}