-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
420 additions
and
180 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 |
---|---|---|
|
@@ -2,11 +2,11 @@ | |
name = "blocking" | ||
version = "0.1.0" | ||
authors = [ "Guoli Lyu <[email protected]>" ] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
arangors = { version = "0.4", features = [ | ||
arangors = { version = "0.5", features = [ | ||
"blocking", | ||
], default-features = false } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,22 @@ | |
name = "reqwest_rustls" | ||
version = "0.1.0" | ||
authors = [ "Guoli Lyu <[email protected]>" ] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
# you can add features like rocksdb or blocking if you want | ||
# but DO NOT add `reqwest_async`, `reqwest_blocking` or `surf_async`. | ||
arangors = { path = "../../", default-features = false } | ||
arangors = { path = "../..", default-features = false } | ||
anyhow = "1" | ||
tokio = { version = "1", features = [ "macros", "rt-multi-thread" ] } | ||
|
||
# required dependencies for ReqwestClient | ||
reqwest = { version = "0.11", features = [ | ||
reqwest = { version = "0.12", features = [ | ||
"gzip", | ||
"json", | ||
"rustls-tls", | ||
], default-features = false } | ||
async-trait = "0.1" | ||
http = "0.2" | ||
http = "1" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use http::{HeaderMap, Request, Response}; | ||
use url::Url; | ||
|
||
use crate::ClientError; | ||
|
||
#[cfg(any(all(feature = "reqwest_async", feature = "reqwest_blocking"),))] | ||
compile_error!(r#"Enabling both async and blocking version of reqwest client is not allowed."#); | ||
|
||
#[cfg(any(feature = "reqwest_async", feature = "reqwest_blocking",))] | ||
pub mod reqwest; | ||
#[cfg(any(feature = "surf_async"))] | ||
pub mod surf; | ||
|
||
#[maybe_async::maybe_async] | ||
pub trait ClientExt: Sync + Clone { | ||
fn new<U: Into<Option<HeaderMap>>>(headers: U) -> Result<Self, ClientError>; | ||
|
||
fn headers(&mut self) -> &mut HeaderMap; | ||
|
||
#[inline] | ||
async fn get<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::get(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
#[inline] | ||
async fn post<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::post(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
#[inline] | ||
async fn put<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::put(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
#[inline] | ||
async fn delete<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::delete(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
#[inline] | ||
async fn patch<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::patch(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
|
||
#[inline] | ||
async fn connect<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::connect(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
|
||
#[inline] | ||
async fn head<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::head(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
|
||
#[inline] | ||
async fn options<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::options(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
|
||
#[inline] | ||
async fn trace<T>(&self, url: Url, text: T) -> Result<Response<String>, ClientError> | ||
where | ||
T: Into<String> + Send, | ||
{ | ||
self.request(Request::trace(url.to_string()).body(text.into()).unwrap()) | ||
.await | ||
} | ||
|
||
async fn request(&self, request: Request<String>) -> Result<Response<String>, ClientError>; | ||
} |
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,78 @@ | ||
//! Reqwest HTTP client | ||
use std::convert::TryInto; | ||
|
||
#[cfg(any(feature = "reqwest_blocking"))] | ||
use ::reqwest::blocking::Client; | ||
|
||
#[cfg(any(feature = "reqwest_async"))] | ||
use ::reqwest::Client; | ||
|
||
use http::header::HeaderMap; | ||
|
||
use super::ClientExt; | ||
use crate::ClientError; | ||
use http::HeaderValue; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ReqwestClient { | ||
pub client: Client, | ||
headers: HeaderMap, | ||
} | ||
|
||
#[maybe_async::maybe_async] | ||
impl ClientExt for ReqwestClient { | ||
fn new<U: Into<Option<HeaderMap>>>(headers: U) -> Result<Self, ClientError> { | ||
let client = Client::builder().gzip(true); | ||
let headers = match headers.into() { | ||
Some(h) => h, | ||
None => HeaderMap::new(), | ||
}; | ||
|
||
client | ||
.build() | ||
.map(|c| ReqwestClient { client: c, headers }) | ||
.map_err(|e| ClientError::HttpClient(format!("{:?}", e))) | ||
} | ||
|
||
fn headers(&mut self) -> &mut HeaderMap<HeaderValue> { | ||
&mut self.headers | ||
} | ||
|
||
async fn request( | ||
&self, | ||
mut request: http::Request<String>, | ||
) -> Result<http::Response<String>, ClientError> { | ||
let headers = request.headers_mut(); | ||
for (header, value) in self.headers.iter() { | ||
if !headers.contains_key(header) { | ||
headers.insert(header, value.clone()); | ||
} | ||
} | ||
let req = request.try_into().unwrap(); | ||
|
||
let resp = self | ||
.client | ||
.execute(req) | ||
.await | ||
.map_err(|e| ClientError::HttpClient(format!("{:?}", e)))?; | ||
|
||
let status_code = resp.status(); | ||
let headers = resp.headers().clone(); | ||
let version = resp.version(); | ||
let content = resp | ||
.text() | ||
.await | ||
.map_err(|e| ClientError::HttpClient(format!("{:?}", e)))?; | ||
let mut build = http::Response::builder(); | ||
|
||
for header in headers.iter() { | ||
build = build.header(header.0, header.1); | ||
} | ||
|
||
build | ||
.status(status_code) | ||
.version(version) | ||
.body(content) | ||
.map_err(|e| ClientError::HttpClient(format!("{:?}", e))) | ||
} | ||
} |
Oops, something went wrong.