-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from SfietKonstantin/awc
awc support
- Loading branch information
Showing
8 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
members = [ | ||
"pretend", | ||
"pretend-codegen", | ||
|
||
# Clients | ||
"pretend-awc", | ||
"pretend-isahc", | ||
"pretend-reqwest", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters