-
Notifications
You must be signed in to change notification settings - Fork 9
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 #77 from sephynox/standardized_client_interface
Standardized client interface
- Loading branch information
Showing
59 changed files
with
1,482 additions
and
563 deletions.
There are no files selected for viewing
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,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 {} |
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,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 | ||
} | ||
} |
This file was deleted.
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 |
---|---|---|
@@ -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::*; |
Oops, something went wrong.