Skip to content

Commit

Permalink
fix: use uclient instead of arangors::client
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed May 1, 2021
1 parent cd4b6b2 commit 823e6e1
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ anyhow = "1"
[dev-dependencies.async-std]
version = "1.9"
features = [ "attributes" ]

[dev-dependencies.reqwest]
version = "0.11"
45 changes: 24 additions & 21 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,58 @@
//! Several implementations are provided: async `reqwest`, blocking `reqwest`,
//! `surf`(async-std) and later `awc`.
use anyhow::Error;
use http::{HeaderMap, Method};
use http::{HeaderMap, HeaderValue, Method};
#[cfg(feature = "reqwest_async")]
use reqwest::Client;
use uclient::{ClientError, ClientExt};
use url::Url;

use arangors::{client::ClientExt, ClientError, GenericConnection};
use arangors::GenericConnection;
use std::convert::TryInto;

/// when use async http client, `blocking` feature MUST be disabled
// This cfg is only to make rust compiler happy in Github Action, you can just ignore it
#[cfg(feature = "reqwest_async")]
#[derive(Debug, Clone)]
pub struct ReqwestClient(pub Client);
pub struct ReqwestClient(pub Client, HeaderMap);

/// you can also use macro: maybe_async::async_impl, with which the whole code
/// block will just vanish when you enabled `blocking` feature.
/// 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 `arangors::client::reqwest` source code.
/// and sync implementation. See `uclient::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]
impl ClientExt for ReqwestClient {
fn new<U: Into<Option<HeaderMap>>>(headers: U) -> Result<Self, ClientError> {
match headers.into() {
Some(h) => Client::builder().default_headers(h),
None => Client::builder(),
}
.build()
.map(|c| ReqwestClient(c))
.map_err(|e| ClientError::HttpClient(format!("{:?}", e)))
let client = Client::builder().gzip(true);
let headers = match headers.into() {
Some(h) => h,
None => HeaderMap::new(),
};

client
.build()
.map(|c| ReqwestClient(c, headers))
.map_err(|e| ClientError::HttpClient(format!("{:?}", e)))
}

fn clone_with_transaction(&self, transaction_id: String) -> Result<Self, ClientError> {
let request = self.0.get("/").build().unwrap();
let original_headers = request.headers();
let mut headers = HeaderMap::new();
for (name, value) in original_headers.iter() {
headers.insert(name, value.clone());
}
headers.insert("x-arango-trx-id", transaction_id.parse().unwrap());
ReqwestClient::new(headers)
fn headers(&mut self) -> &mut HeaderMap<HeaderValue> {
&mut self.1
}

async fn request(
&self,
request: http::Request<String>,
mut request: http::Request<String>,
) -> Result<http::Response<String>, ClientError> {
let headers = request.headers_mut();
for (header, value) in self.1.iter() {
if !headers.contains_key(header) {
headers.insert(header, value.clone());
}
}
let req = request.try_into().unwrap();

let resp = self
Expand Down
7 changes: 6 additions & 1 deletion examples/reqwest_rustls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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;

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

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

fn headers(&mut self) -> &mut HeaderMap<HeaderValue> {
&mut self.headers
}

fn new<U: Into<Option<HeaderMap>>>(headers: U) -> Result<Self, ClientError> {
let client = Client::builder().gzip(true);
let headers = match headers.into() {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ pub use crate::{

pub mod analyzer;
pub mod aql;
// pub mod client;
pub mod collection;
pub mod connection;
pub mod database;
Expand Down
11 changes: 6 additions & 5 deletions tests/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
#![allow(unused_parens)]
use crate::common::{collection, connection};

use log::{info, trace};
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use uclient::ClientExt;

use arangors::analyzer::{
AnalyzerCase, AnalyzerFeature, AnalyzerInfo, NgramAnalyzerProperties, NgramStreamType,
NormAnalyzerProperties,
};
use arangors::{
client::ClientExt,
collection::{
options::{ChecksumOptions, PropertiesOptions},
response::Status,
Expand All @@ -17,10 +22,6 @@ use arangors::{
ClientError, Connection, Database, Document,
};
use common::{get_arangodb_host, get_normal_password, get_normal_user, test_setup};
use log::{info, trace};
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use std::collections::HashMap;

pub mod common;

Expand Down
4 changes: 2 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub async fn connection() -> arangors::Connection {
pub async fn collection<'a>(
conn: &'a arangors::Connection,
name: &str,
) -> Collection<arangors::client::reqwest::ReqwestClient> {
) -> Collection<uclient::reqwest::ReqwestClient> {
let database = conn.db("test_db").await.unwrap();

match database.drop_collection(name).await {
Expand All @@ -77,7 +77,7 @@ pub async fn collection<'a>(
pub async fn collection<'a>(
conn: &'a arangors::Connection,
name: &str,
) -> Collection<arangors::client::surf::SurfClient> {
) -> Collection<uclient::surf::SurfClient> {
let database = conn.db("test_db").await.unwrap();

match database.drop_collection(name).await {
Expand Down
3 changes: 2 additions & 1 deletion tests/connection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(unused_imports)]
#![allow(unused_parens)]
use pretty_assertions::assert_eq;
use uclient::ClientExt;

use arangors::{client::ClientExt, connection::Permission, Connection};
use arangors::{connection::Permission, Connection};
use common::{
connection, get_arangodb_host, get_normal_password, get_normal_user, test_root_and_normal,
test_setup,
Expand Down
2 changes: 1 addition & 1 deletion tests/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use log::trace;
use pretty_assertions::assert_eq;
use serde_json::{json, Value};
use uclient::ClientExt;

use arangors::client::ClientExt;
use arangors::{
collection::{
options::{ChecksumOptions, PropertiesOptions},
Expand Down
11 changes: 6 additions & 5 deletions tests/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#![allow(unused_imports)]
#![allow(unused_parens)]
use log::trace;
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use serde_json::{json, Value};
use uclient::ClientExt;

use crate::common::{collection, connection};
use arangors::{
client::ClientExt,
collection::{
options::{ChecksumOptions, PropertiesOptions},
response::Status,
Expand All @@ -14,10 +19,6 @@ use arangors::{
ClientError, Connection, Database, Document,
};
use common::{get_arangodb_host, get_normal_password, get_normal_user, test_setup};
use log::trace;
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use serde_json::{json, Value};

pub mod common;

Expand Down
11 changes: 6 additions & 5 deletions tests/view.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#![allow(unused_imports)]
#![allow(unused_parens)]
use std::collections::HashMap;

use crate::common::{collection, connection};
use log::{info, trace};
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use uclient::ClientExt;

use arangors::view::{ArangoSearchViewLink, ArangoSearchViewPropertiesOptions, ViewOptions};
use arangors::{
client::ClientExt,
collection::{
options::{ChecksumOptions, PropertiesOptions},
response::Status,
Expand All @@ -14,10 +19,6 @@ use arangors::{
ClientError, Connection, Database, Document,
};
use common::{get_arangodb_host, get_normal_password, get_normal_user, test_setup};
use log::{info, trace};
use maybe_async::maybe_async;
use pretty_assertions::assert_eq;
use std::collections::HashMap;

pub mod common;

Expand Down

0 comments on commit 823e6e1

Please sign in to comment.