Skip to content

Commit

Permalink
Merge pull request #16 from SfietKonstantin/awc
Browse files Browse the repository at this point in the history
awc support
  • Loading branch information
SfietKonstantin authored Jun 1, 2021
2 parents b8d3dff + 063e03c commit 57b13f0
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
members = [
"pretend",
"pretend-codegen",

# Clients
"pretend-awc",
"pretend-isahc",
"pretend-reqwest",
]
17 changes: 17 additions & 0 deletions pretend-awc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "pretend-awc"
edition = "2018"
version = "0.1.0"
description = "awc based client for pretend."
authors = ["Lucien XU <[email protected]>"]
license = "MIT"
homepage = "https://github.com/SfietKonstantin/pretend"
documentation = "https://docs.rs/pretend-isahc/latest/pretend_awc/"
repository = "https://github.com/SfietKonstantin/pretend"
keywords = ["http", "client", "web", "async", "declarative"]
categories = ["web-programming::http-client"]
readme = "README.md"

[dependencies]
pretend = { path = "../pretend", version = "0.1.0" }
awc = "2.0"
5 changes: 5 additions & 0 deletions pretend-awc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pretend awc client

This crate provides a `awc` based client implementation for `pretend`.

See [`pretend`](../pretend/README.md) for more information.
64 changes: 64 additions & 0 deletions pretend-awc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! `awc` based `pretend` client
#![warn(missing_docs)]
#![deny(unsafe_code)]

pub use awc;

use awc::http::{HeaderName, HeaderValue};
use awc::Client as AClient;
use pretend::client::{async_trait, Bytes, LocalClient, Method};
use pretend::http::header::{HeaderName as PHeaderName, HeaderValue as PHeaderValue};
use pretend::{Error, HeaderMap, Response, Result, Url};

/// `awc` based `pretend` client
#[derive(Clone, Default)]
pub struct Client {
client: AClient,
}

impl Client {
/// Constructor with custom client
///
/// This constructor creates a client implementation
/// for `pretend` wrapping the supplied `awc` client.
pub fn new(client: AClient) -> Self {
Client { client }
}
}

#[async_trait(?Send)]
impl LocalClient for Client {
async fn execute(
&self,
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Bytes>,
) -> Result<Response<Bytes>> {
let mut request = self.client.request(method, url.as_str());
for (name, value) in headers.iter() {
request = request.set_header(name, value.as_bytes());
}

let future = if let Some(body) = body {
request.send_body(body.to_vec())
} else {
request.send()
};

let mut response = future.await.map_err(|err| Error::Response(Box::new(err)))?;
let status = response.status();
let headers = response.headers();
let headers = headers.iter().map(create_header).collect::<HeaderMap>();
let result = response
.body()
.await
.map_err(|err| Error::Body(Box::new(err)))?;
Ok(Response::new(status, headers, Bytes::from(result.to_vec())))
}
}

fn create_header((name, value): (&HeaderName, &HeaderValue)) -> (PHeaderName, PHeaderValue) {
(PHeaderName::from(name), PHeaderValue::from(value))
}
1 change: 1 addition & 0 deletions pretend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ url = "2.2"

[dev-dependencies]
actix-web = "3.3"
pretend-awc = { path = "../pretend-awc" }
pretend-isahc = { path = "../pretend-isahc" }
pretend-reqwest = { path = "../pretend-reqwest", features = ["blocking"] }
tokio = { version = "1.5", features = ["macros", "rt-multi-thread"] }
Expand Down
1 change: 0 additions & 1 deletion pretend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@
//!
//! Here is a quick roadmap
//!
//! - Support more clients (awc)
//! - Introduce more attributes to mark method parameters (body, json, params)
//! - Introduce interceptors
Expand Down
30 changes: 28 additions & 2 deletions pretend/tests/test_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ mod server;
use clients_tester::{
ClientsTester, TestableClient, TokioTestableClient, TokioTestableLocalClient,
};
use pretend::client::{Client, LocalClient};
use pretend::Url;
use pretend::client::{Bytes, Client, LocalClient, Method};
use pretend::{HeaderMap, Response, Result, Url};
use pretend_awc::Client as AClient;
use pretend_isahc::Client as IClient;
use pretend_reqwest::{BlockingClient as RBlockingClient, Client as RClient};

Expand All @@ -30,6 +31,30 @@ where
))
}

struct TestableAwcClient;

#[actix_web::main]
async fn awc_execute(
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Bytes>,
) -> Result<Response<Bytes>> {
AClient::default().execute(method, url, headers, body).await
}

impl TestableClient for TestableAwcClient {
fn execute(
&self,
method: Method,
url: Url,
headers: HeaderMap,
body: Option<Bytes>,
) -> Result<Response<Bytes>> {
awc_execute(method, url, headers, body)
}
}

#[test]
fn test_all_clients() {
server::test(|| {
Expand All @@ -53,6 +78,7 @@ fn test_local_clients(url: Url) {
let clients: Vec<Box<dyn TestableClient>> = vec![
create_testable_local(RClient::default()),
create_testable_local(IClient::new().unwrap()),
Box::new(TestableAwcClient),
];
let tester = ClientsTester::new(url, clients);
tester.test();
Expand Down

0 comments on commit 57b13f0

Please sign in to comment.