Skip to content

Commit

Permalink
Merge pull request #77 from sephynox/standardized_client_interface
Browse files Browse the repository at this point in the history
Standardized client interface
  • Loading branch information
LimpidCrypto authored Jun 11, 2024
2 parents 4dfcfcd + c1dfb55 commit db3e2aa
Show file tree
Hide file tree
Showing 59 changed files with 1,482 additions and 563 deletions.
43 changes: 25 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,35 @@ chrono = { version = "0.4.19", default-features = false, features = [
"clock",
] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
rand = { version = "0.8.4", default-features = false, features = ["getrandom"] }
rand = { version = "0.8.5", default-features = false, features = ["getrandom"] }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.68", default-features = false, features = [
"alloc",
] }
serde_with = "3.2.0"
serde_repr = "0.1"
zeroize = "1.5.7"
hashbrown = { version = "0.14.0", default-features = false, features = [
"serde",
] }
hashbrown = { version = "0.14.5", features = ["serde"] }
fnv = { version = "1.0.7", default-features = false }
derive-new = { version = "0.5.9", default-features = false }
thiserror-no-std = "2.0.2"
anyhow = { version = "1.0.69", default-features = false }
tokio = { version = "1.28.0", default-features = false, optional = true }
url = { version = "2.2.2", default-features = false, optional = true }
futures = { version = "0.3.28", default-features = false, optional = true }
futures = { version = "0.3.28", default-features = false, features = [
"alloc",
], optional = true }
rand_core = { version = "0.6.4", default-features = false }
tokio-tungstenite = { version = "0.20.0", optional = true }

[dependencies.embedded-websocket]
# git version needed to use `framer_async`
git = "https://github.com/ninjasource/embedded-websocket"
version = "0.9.2"
rev = "8d87d46f46fa0c75e099ca8aad37e8d00c8854f8"
default-features = false
optional = true
embassy-sync = { version = "0.6.0", default-features = false }
embedded-io-async = "0.6.1"
futures-sink = { version = "0.3.30", default-features = false }
futures-core = { version = "0.3.30", default-features = false }
futures-util = { version = "0.3.30", optional = true }
tokio-util = { version = "0.7.7", features = ["codec"], optional = true }
bytes = { version = "1.4.0", default-features = false }
embassy-futures = "0.1.1"
embedded-websocket = { version = "0.9.3", optional = true }

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -82,9 +83,7 @@ cargo-husky = { version = "1.5.0", default-features = false, features = [
] }
tokio = { version = "1.28.0", features = ["full"] }
tokio-util = { version = "0.7.7", features = ["codec"] }
futures-util = "0.3.30"
bytes = { version = "1.4.0", default-features = false }
rand = { version = "0.8.4", default-features = false, features = [
rand = { version = "0.8.5", default-features = false, features = [
"getrandom",
"std",
"std_rng",
Expand All @@ -96,13 +95,20 @@ harness = false

[features]
default = ["std", "core", "models", "utils", "tungstenite"]
models = ["core", "transactions", "requests", "ledger"]
models = ["core", "transactions", "requests", "ledger", "results"]
transactions = ["core", "amounts", "currencies"]
requests = ["core", "amounts", "currencies"]
results = ["core", "amounts", "currencies"]
ledger = ["core", "amounts", "currencies"]
amounts = ["core"]
currencies = ["core"]
tungstenite = ["url", "futures", "tokio/full", "tokio-tungstenite/native-tls"]
tungstenite = [
"url",
"futures",
"tokio/net",
"tokio-tungstenite/native-tls",
"futures-util",
]
embedded-ws = ["url", "futures", "embedded-websocket"]
core = ["utils"]
utils = []
Expand All @@ -119,4 +125,5 @@ std = [
"serde/std",
"indexmap/std",
"secp256k1/std",
"dep:tokio-util",
]
19 changes: 19 additions & 0 deletions src/asynch/clients/async_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::client::Client;
use crate::models::{requests::Request, results::XRPLResponse};
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[allow(async_fn_in_trait)]
pub trait AsyncClient<'a>: Client<'a> {
async fn request<
Res: Serialize + for<'de> Deserialize<'de>,
Req: Serialize + for<'de> Deserialize<'de> + Request<'a>,
>(
&'a self,
request: Req,
) -> Result<XRPLResponse<'_, Res, Req>> {
self.request_impl(request).await
}
}

impl<'a, T: Client<'a>> AsyncClient<'a> for T {}
40 changes: 40 additions & 0 deletions src/asynch/clients/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::models::{requests::Request, results::XRPLResponse};
#[cfg(feature = "std")]
use crate::utils::get_random_id;
use alloc::borrow::Cow;
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[allow(async_fn_in_trait)]
pub trait Client<'a> {
async fn request_impl<
Res: Serialize + for<'de> Deserialize<'de>,
Req: Serialize + for<'de> Deserialize<'de> + Request<'a>,
>(
&'a self,
request: Req,
) -> Result<XRPLResponse<'_, Res, Req>>;
fn set_request_id<
Res: Serialize + for<'de> Deserialize<'de>,
Req: Serialize + for<'de> Deserialize<'de> + Request<'a>,
>(
&'a self,
request: &mut Req,
) -> Cow<'_, str> {
let common_fields = request.get_common_fields();
let request_id: Cow<'_, str> = match common_fields.id.clone() {
Some(id) => id,
None => {
#[cfg(feature = "std")]
{
let mut rng = rand::thread_rng();
Cow::Owned(get_random_id(&mut rng))
}
#[cfg(not(feature = "std"))]
unimplemented!("get_random_id is not yet implemented for no_std. Please provide an `id` in the request.");
}
};
request.get_common_fields_mut().id = Some(request_id.clone());
request_id
}
}
168 changes: 0 additions & 168 deletions src/asynch/clients/embedded_ws.rs

This file was deleted.

18 changes: 5 additions & 13 deletions src/asynch/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
pub mod exceptions;
mod async_client;
mod client;
mod websocket;

pub struct WebsocketOpen;
pub struct WebsocketClosed;

#[cfg(all(feature = "embedded-ws", not(feature = "tungstenite")))]
mod embedded_websocket;
#[cfg(all(feature = "tungstenite", not(feature = "embedded-ws")))]
mod tungstenite;

#[cfg(all(feature = "embedded-ws", not(feature = "tungstenite")))]
pub use embedded_websocket::*;
#[cfg(all(feature = "tungstenite", not(feature = "embedded-ws")))]
pub use tungstenite::*;
pub use async_client::*;
pub use websocket::*;
Loading

0 comments on commit db3e2aa

Please sign in to comment.