Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump to 0.5.4; add reqwest middleware #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.4] - 2023-06-28

### Fixed

- Add support for reqwest middleware client

## [0.5.3] - 2022-03-15

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustify"
version = "0.5.3"
version = "0.5.4"
authors = ["Joshua Gilman <[email protected]>"]
description = "A Rust library for interacting with HTTP API endpoints."
license = "MIT"
Expand Down Expand Up @@ -28,6 +28,7 @@ async-trait = "0.1.52"
bytes = "1.1.0"
http = "0.2.6"
reqwest = { version = "0.11.10", default-features = false, optional = true }
reqwest-middleware = { version = "0.2.2", optional = true }
rustify_derive = { version = "0.5.2", path = "rustify_derive" }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
Expand Down
2 changes: 2 additions & 0 deletions src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
//! varying HTTP clients.
#[cfg(feature = "reqwest")]
pub mod reqwest;
#[cfg(all(feature = "reqwest", feature = "reqwest-middleware"))]
pub mod reqwest_middleware;
101 changes: 101 additions & 0 deletions src/clients/reqwest_middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Contains an implementation of [Client][crate::client::Client] being backed
//! by the [reqwest](https://docs.rs/reqwest/) crate.

use crate::{client::Client as RustifyClient, errors::ClientError};
use async_trait::async_trait;
use http::{Request, Response};
use std::convert::TryFrom;

/// A client based on the
/// [reqwest_middleware::ClientWithMiddleware][1] which can be used for executing
/// [Endpoints][crate::endpoint::Endpoint]. A backing instance of a
/// [reqwest_middleware::ClientWithMiddleware][1] is used to increase performance and to save certain
/// characteristics across sessions. A base URL is required and is used to
/// qualify the full path of any [Endpoints][crate::endpoint::Endpoint] which
/// are executed by this client.
///
/// # Example
/// ```
/// use rustify::clients::reqwest_middleware::ClientWithMiddleware;
/// use rustify::Endpoint;
/// use rustify_derive::Endpoint;
/// use serde::Serialize;
///
/// #[derive(Debug, Endpoint, Serialize)]
/// #[endpoint(path = "my/endpoint")]
/// struct MyEndpoint {}
///
/// # tokio_test::block_on(async {
/// let client = ClientWithMiddleware::default("http://myapi.com");
/// let endpoint = MyEndpoint {};
/// let result = endpoint.exec(&client).await;
/// # })
/// ```
///
/// [1]: https://docs.rs/reqwest-middleware/latest/reqwest_middleware/struct.ClientWithMiddleware.html
pub struct ClientWithMiddleware {
pub http: reqwest_middleware::ClientWithMiddleware,
pub base: String,
}

impl ClientWithMiddleware {
/// Creates a new instance of [ClientWithMiddleware] using the provided parameters.
pub fn new(base: &str, http: reqwest_middleware::ClientWithMiddleware) -> Self {
Self {
base: base.to_string(),
http,
}
}

/// Creates a new instance of [ClientWithMiddleware] with a default instance of
/// [reqwest_middleware::ClientWithMiddleware][1].
///
/// [1]: https://docs.rs/reqwest-middleware/latest/reqwest_middleware/struct.ClientWithMiddleware.html
pub fn default(base: &str) -> Self {
Self {
base: base.to_string(),
http: reqwest_middleware::ClientBuilder::new(reqwest::Client::default()).build(),
}
}
}

#[async_trait]
impl RustifyClient for ClientWithMiddleware {
fn base(&self) -> &str {
self.base.as_str()
}

#[instrument(skip(self, req), err)]
async fn send(&self, req: Request<Vec<u8>>) -> Result<Response<Vec<u8>>, ClientError> {
let request = reqwest::Request::try_from(req)
.map_err(|e| ClientError::ReqwestBuildError { source: e })?;

let url_err = request.url().to_string();
let method_err = request.method().to_string();
let response = self
.http
.execute(request)
.await
.map_err(|e| ClientError::RequestError {
source: e.into(),
url: url_err,
method: method_err,
})?;

let status_code = response.status().as_u16();
let mut http_resp = http::Response::builder().status(status_code);
for v in response.headers().into_iter() {
http_resp = http_resp.header(v.0, v.1);
}

http_resp
.body(
response
.bytes()
.await
.map_err(|e| ClientError::ResponseError { source: e.into() })?
.to_vec(),
)
.map_err(|e| ClientError::ResponseError { source: e.into() })
}
}