Skip to content

Commit

Permalink
build: remove uclient
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed Jun 6, 2024
1 parent 11db245 commit 344a784
Show file tree
Hide file tree
Showing 22 changed files with 420 additions and 180 deletions.
244 changes: 102 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ status = "actively-developed"

[features]
default = [ "rocksdb", "reqwest_async" ]
blocking = [ "maybe-async/is_sync", "uclient/blocking" ]
reqwest_async = [ "uclient/async_reqwest" ]
reqwest_async_rustls = [ "uclient/async_reqwest_rustls" ]
reqwest_blocking = [ "uclient/blocking_reqwest", "blocking" ]
reqwest_blocking_rustls = [ "uclient/blocking_reqwest_rustls", "blocking" ]
surf_async = [ "uclient/async_surf", "http-types" ]

blocking = [ "maybe-async/is_sync" ]
reqwest_async = [ "reqwest" ]
reqwest_blocking = [ "reqwest/blocking", "blocking" ]
surf_async = [ "http-types", "surf" ]

cluster = [ ]
enterprise = [ ]
mmfiles = [ ]
Expand All @@ -35,23 +35,22 @@ arango3_7 = [ ]
[dependencies]
async-trait = "0.1"
base64 = "0.22"
http = "0.2"
http = "1"
log = "0.4"
maybe-async = "0.2"
serde_json = "1"
serde_qs = "0.13"
thiserror = "1"
typed-builder = "0.18"
url = "2"
uclient = "0.1"
serde_repr = "0.1"

[dependencies.serde]
version = "1"
features = [ "derive" ]

[dependencies.reqwest]
version = "0.11"
version = "0.12"
features = [ "gzip", "json" ]
optional = true

Expand All @@ -78,4 +77,4 @@ anyhow = "1"
features = [ "attributes" ]

[dev-dependencies.reqwest]
version = "0.11"
version = "0.12"
4 changes: 2 additions & 2 deletions examples/blocking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
4 changes: 2 additions & 2 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
//! Several implementations are provided: async `reqwest`, blocking `reqwest`,
//! `surf`(async-std) and later `awc`.
use anyhow::Error;
use arangors::{client::ClientExt, ClientError};
use http::{HeaderMap, HeaderValue, Method};
#[cfg(feature = "reqwest_async")]
use reqwest::Client;
use uclient::{ClientError, ClientExt};
use url::Url;

use arangors::GenericConnection;
Expand All @@ -32,7 +32,7 @@ pub struct ReqwestClient(pub Client, HeaderMap);
/// Also, the API of reqwest is almost the same for async and sync. You can also
/// use maybe_async::maybe_async to remove async/await keyword in need, and just
/// import reqwesat::Client and rewest::blocking::Client respectively in async
/// and sync implementation. See `uclient::reqwest` source code.
/// and sync implementation. See `arangors::client::reqwest` source code.
// This cfg is only to make rust compiler happy in Github Action, you can just ignore it
#[cfg(feature = "reqwest_async")]
#[async_trait::async_trait]
Expand Down
8 changes: 4 additions & 4 deletions examples/reqwest_rustls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 2 additions & 3 deletions examples/reqwest_rustls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::convert::TryInto;

use ::reqwest::Client;
use http::header::HeaderMap;
use uclient::ClientExt;

use arangors::ClientError;
use arangors::client::ClientExt;
use arangors::transaction::TRANSACTION_HEADER;
use arangors::ClientError;

#[derive(Debug, Clone)]
pub struct ReqwestClient {
Expand All @@ -16,7 +16,6 @@ pub struct ReqwestClient {

#[async_trait::async_trait]
impl ClientExt for ReqwestClient {

fn headers(&mut self) -> &mut HeaderMap<HeaderValue> {
&mut self.headers
}
Expand Down
98 changes: 98 additions & 0 deletions src/client/mod.rs
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>;
}
78 changes: 78 additions & 0 deletions src/client/reqwest.rs
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)))
}
}
Loading

0 comments on commit 344a784

Please sign in to comment.