Skip to main content

xrpl/clients/
mod.rs

1use crate::{
2    asynch::clients::{exceptions::XRPLClientResult, CommonFields, XRPLClient},
3    models::{requests::XRPLRequest, results::XRPLResponse},
4};
5
6pub use crate::asynch::clients::SingleExecutorMutex;
7
8pub trait XRPLSyncClient: XRPLClient {
9    fn request<'a: 'b, 'b>(&self, request: XRPLRequest<'a>) -> XRPLClientResult<XRPLResponse<'b>>;
10
11    fn get_common_fields(&self) -> XRPLClientResult<CommonFields<'_>>;
12}
13
14#[cfg(all(feature = "json-rpc", feature = "std"))]
15pub mod json_rpc {
16    use tokio::runtime::Runtime;
17    use url::Url;
18
19    #[cfg(feature = "helpers")]
20    use crate::{asynch::clients::XRPLFaucet, models::requests::FundFaucet};
21    use crate::{
22        asynch::clients::{
23            exceptions::XRPLClientResult, AsyncJsonRpcClient, CommonFields, XRPLAsyncClient,
24            XRPLClient,
25        },
26        models::{requests::XRPLRequest, results::XRPLResponse},
27    };
28
29    use super::XRPLSyncClient;
30
31    pub struct JsonRpcClient(AsyncJsonRpcClient);
32    impl JsonRpcClient {
33        pub fn connect(url: Url) -> Self {
34            Self(AsyncJsonRpcClient::connect(url))
35        }
36    }
37
38    impl XRPLClient for JsonRpcClient {
39        async fn request_impl<'a: 'b, 'b>(
40            &self,
41            request: XRPLRequest<'a>,
42        ) -> XRPLClientResult<XRPLResponse<'b>> {
43            self.0.request_impl(request).await
44        }
45
46        fn get_host(&self) -> Url {
47            self.0.get_host()
48        }
49
50        fn get_random_id<'a>(&self) -> alloc::borrow::Cow<'a, str> {
51            self.0.get_random_id()
52        }
53    }
54
55    impl XRPLSyncClient for JsonRpcClient {
56        fn request<'a: 'b, 'b>(
57            &self,
58            request: XRPLRequest<'a>,
59        ) -> XRPLClientResult<XRPLResponse<'b>> {
60            match Runtime::new() {
61                Ok(rt) => rt.block_on(self.0.request_impl(request)),
62                Err(e) => Err(e.into()),
63            }
64        }
65
66        fn get_common_fields(&self) -> XRPLClientResult<CommonFields<'_>> {
67            match Runtime::new() {
68                Ok(rt) => rt.block_on(self.0.get_common_fields()),
69                Err(e) => Err(e.into()),
70            }
71        }
72    }
73
74    #[cfg(feature = "helpers")]
75    impl XRPLFaucet for JsonRpcClient {
76        async fn request_funding(
77            &self,
78            url: Option<Url>,
79            request: FundFaucet<'_>,
80        ) -> XRPLClientResult<()> {
81            self.0.request_funding(url, request).await
82        }
83    }
84}
85
86#[cfg(all(feature = "json-rpc", not(feature = "std")))]
87pub mod json_rpc {
88    use embassy_sync::blocking_mutex::raw::RawMutex;
89    use embedded_nal_async::{Dns, TcpConnect};
90    use url::Url;
91
92    #[cfg(feature = "helpers")]
93    use crate::{asynch::clients::XRPLFaucet, models::requests::FundFaucet};
94    use crate::{
95        asynch::clients::{exceptions::XRPLClientResult, AsyncJsonRpcClient, XRPLClient},
96        models::{requests::XRPLRequest, results::XRPLResponse},
97    };
98
99    pub struct JsonRpcClient<'a, const BUF: usize, T, D, M>(
100        pub(crate) AsyncJsonRpcClient<'a, BUF, T, D, M>,
101    )
102    where
103        M: RawMutex,
104        T: TcpConnect + 'a,
105        D: Dns + 'a;
106
107    impl<'a, const BUF: usize, T, D, M> JsonRpcClient<'a, BUF, T, D, M>
108    where
109        M: RawMutex,
110        T: TcpConnect + 'a,
111        D: Dns + 'a,
112    {
113        pub fn connect(url: Url, tcp: &'a T, dns: &'a D) -> Self {
114            Self(AsyncJsonRpcClient::connect(url, tcp, dns))
115        }
116    }
117
118    impl<const BUF: usize, T, D, M> XRPLClient for JsonRpcClient<'_, BUF, T, D, M>
119    where
120        M: RawMutex,
121        T: TcpConnect,
122        D: Dns,
123    {
124        async fn request_impl<'a: 'b, 'b>(
125            &self,
126            request: XRPLRequest<'a>,
127        ) -> XRPLClientResult<XRPLResponse<'b>> {
128            self.0.request_impl(request).await
129        }
130
131        fn get_host(&self) -> Url {
132            self.0.get_host()
133        }
134    }
135
136    #[cfg(feature = "helpers")]
137    impl<'a, const BUF: usize, T, D, M> XRPLFaucet for JsonRpcClient<'a, BUF, T, D, M>
138    where
139        M: RawMutex,
140        T: TcpConnect + 'a,
141        D: Dns + 'a,
142    {
143        async fn request_funding(
144            &self,
145            url: Option<Url>,
146            request: FundFaucet<'_>,
147        ) -> XRPLClientResult<()> {
148            self.0.request_funding(url, request).await
149        }
150    }
151}
152
153pub trait XRPLSyncWebsocketIO {
154    fn xrpl_send(&mut self, message: XRPLRequest<'_>) -> XRPLClientResult<()>;
155
156    fn xrpl_receive(&mut self) -> XRPLClientResult<Option<XRPLResponse<'_>>>;
157}
158
159#[cfg(all(feature = "websocket", feature = "std"))]
160pub mod websocket {
161    use embassy_sync::blocking_mutex::raw::RawMutex;
162    use tokio::runtime::Runtime;
163    use url::Url;
164
165    use super::{XRPLSyncClient, XRPLSyncWebsocketIO};
166    use crate::{
167        asynch::clients::{
168            exceptions::XRPLClientResult, AsyncWebSocketClient, CommonFields, XRPLAsyncClient,
169            XRPLAsyncWebsocketIO, XRPLClient,
170        },
171        models::{requests::XRPLRequest, results::XRPLResponse},
172    };
173
174    pub use crate::asynch::clients::{WebSocketClosed, WebSocketOpen};
175
176    pub struct WebSocketClient<M: RawMutex, Status = WebSocketClosed> {
177        pub(crate) inner: AsyncWebSocketClient<M, Status>,
178        rt: Runtime,
179    }
180
181    impl<M: RawMutex> WebSocketClient<M, WebSocketClosed> {
182        pub fn open(url: Url) -> XRPLClientResult<WebSocketClient<M, WebSocketOpen>> {
183            match Runtime::new() {
184                Ok(rt) => {
185                    let client: AsyncWebSocketClient<M, WebSocketOpen> =
186                        rt.block_on(AsyncWebSocketClient::open(url))?;
187
188                    Ok(WebSocketClient { inner: client, rt })
189                }
190                Err(e) => Err(e.into()),
191            }
192        }
193    }
194
195    impl<M> XRPLClient for WebSocketClient<M, WebSocketOpen>
196    where
197        M: RawMutex,
198    {
199        fn get_host(&self) -> Url {
200            self.inner.get_host()
201        }
202
203        async fn request_impl<'a: 'b, 'b>(
204            &self,
205            request: XRPLRequest<'a>,
206        ) -> XRPLClientResult<XRPLResponse<'b>> {
207            match Runtime::new() {
208                Ok(rt) => rt.block_on(self.inner.request_impl(request)),
209                Err(e) => Err(e.into()),
210            }
211        }
212    }
213
214    impl<M> XRPLSyncClient for WebSocketClient<M, WebSocketOpen>
215    where
216        M: RawMutex,
217    {
218        fn request<'a: 'b, 'b>(
219            &self,
220            request: XRPLRequest<'a>,
221        ) -> XRPLClientResult<XRPLResponse<'b>> {
222            self.rt.block_on(self.inner.request_impl(request))
223        }
224
225        fn get_common_fields(&self) -> XRPLClientResult<CommonFields<'_>> {
226            self.rt.block_on(self.inner.get_common_fields())
227        }
228    }
229
230    impl<M> XRPLSyncWebsocketIO for WebSocketClient<M, WebSocketOpen>
231    where
232        M: RawMutex,
233    {
234        fn xrpl_send(&mut self, message: XRPLRequest<'_>) -> XRPLClientResult<()> {
235            self.rt.block_on(self.inner.xrpl_send(message))
236        }
237
238        fn xrpl_receive(&mut self) -> XRPLClientResult<Option<XRPLResponse<'_>>> {
239            self.rt.block_on(self.inner.xrpl_receive())
240        }
241    }
242}
243
244#[cfg(all(feature = "websocket", not(feature = "std")))]
245pub mod websocket {
246    use super::XRPLSyncWebsocketIO;
247    use embassy_futures::block_on;
248    use embassy_sync::blocking_mutex::raw::RawMutex;
249    use embedded_io_async::{Read, Write};
250    use rand::RngCore;
251    use url::Url;
252
253    use crate::{
254        asynch::clients::{
255            exceptions::XRPLClientResult, AsyncWebSocketClient, WebSocketOpen,
256            XRPLAsyncWebsocketIO, XRPLClient,
257        },
258        models::{requests::XRPLRequest, results::XRPLResponse},
259    };
260
261    pub struct WebSocketClient<const BUF: usize, Tcp, Rng, M, Status = WebSocketOpen>(
262        pub(crate) AsyncWebSocketClient<BUF, Tcp, Rng, M, Status>,
263    )
264    where
265        Tcp: Read + Write + Unpin,
266        Rng: RngCore,
267        M: RawMutex;
268
269    impl<const BUF: usize, Tcp, Rng, M> XRPLClient for WebSocketClient<BUF, Tcp, Rng, M, WebSocketOpen>
270    where
271        Tcp: Read + Write + Unpin,
272        Rng: RngCore,
273        M: RawMutex,
274    {
275        fn get_host(&self) -> Url {
276            self.0.get_host()
277        }
278
279        async fn request_impl<'a: 'b, 'b>(
280            &self,
281            request: XRPLRequest<'a>,
282        ) -> XRPLClientResult<XRPLResponse<'b>> {
283            block_on(self.0.request_impl(request))
284        }
285    }
286
287    impl<const BUF: usize, Tcp, Rng, M> XRPLSyncWebsocketIO
288        for WebSocketClient<BUF, Tcp, Rng, M, WebSocketOpen>
289    where
290        Tcp: Read + Write + Unpin,
291        Rng: RngCore,
292        M: RawMutex,
293    {
294        fn xrpl_send(
295            &mut self,
296            message: crate::models::requests::XRPLRequest<'_>,
297        ) -> XRPLClientResult<()> {
298            block_on(self.0.xrpl_send(message))
299        }
300
301        fn xrpl_receive(
302            &mut self,
303        ) -> XRPLClientResult<Option<crate::models::results::XRPLResponse<'_>>> {
304            block_on(self.0.xrpl_receive())
305        }
306    }
307}