xpx_chain_sdk/api/service/transport/client/
mod.rs1use std::{
10 fmt,
11 task::{Context, Poll},
12};
13
14use bytes::Bytes;
15use futures_util::future;
16use hyper::http::uri::InvalidUri;
17use hyper::Uri;
18use tower::Service;
19
20pub use api_node::ApiNode;
21
22use crate::api;
23use crate::api::routes::{
24 account_routes_api::AccountRoutes, chain_routes_api::ChainRoutes,
25 metadata_v2_routes_api::MetadataV2Routes, mosaic_routes_api::MosaicRoutes,
26 namespace_routes_api::NamespaceRoutes, network_routes_api::NetworkRoutes,
27 node_routes_api::NodeRoutes, resolver_routes_api::ResolverRoutes,
28 transaction_routes_api::TransactionRoutes,
29};
30use crate::api::transport::service::connection::{Request, Response, TimeoutConnector};
31
32use super::service::Connection;
33
34mod api_node;
35
36const DEFAULT_BUFFER_SIZE: usize = 1024;
37
38#[derive(Clone)]
62pub struct Client {
63 inner: Connection,
64}
65
66impl Client {
67 pub fn builder(uri: Uri) -> ApiNode {
69 ApiNode::from(uri)
70 }
71
72 pub fn from_static(s: &'static str) -> ApiNode {
79 let uri = Uri::from_static(s);
80 Self::builder(uri)
81 }
82
83 pub fn from_shared(s: impl Into<Bytes>) -> Result<ApiNode, InvalidUri> {
90 let uri = Uri::from_maybe_shared(s.into())?;
91 Ok(Self::builder(uri))
92 }
93
94 pub(crate) fn new(connector: TimeoutConnector, endpoint: ApiNode) -> Self {
95 let svc = Connection::lazy(connector, endpoint);
96
97 Client { inner: svc }
98 }
99
100 pub(crate) async fn connect(
101 connector: TimeoutConnector,
102 endpoint: ApiNode,
103 ) -> Result<Self, super::Error> {
104 let svc = Connection::connect(connector, endpoint)
105 .await
106 .map_err(super::Error::from_source)?;
107
108 Ok(Client { inner: svc })
109 }
110}
111
112impl Client {
113 fn __client(&self) -> Connection {
114 self.inner.clone()
115 }
116
117 pub fn account_api(&self) -> Box<AccountRoutes> {
119 Box::new(AccountRoutes::new(self.__client()))
120 }
121
122 pub fn chain_api(&self) -> Box<ChainRoutes> {
124 Box::new(ChainRoutes::new(self.__client()))
125 }
126
127 pub fn node_api(&self) -> Box<NodeRoutes> {
138 Box::new(NodeRoutes::new(self.__client()))
139 }
140
141 pub fn network_api(&self) -> Box<NetworkRoutes> {
143 Box::new(NetworkRoutes::new(self.__client()))
144 }
145
146 pub fn mosaic_api(&self) -> Box<MosaicRoutes> {
148 Box::new(MosaicRoutes::new(self.__client()))
149 }
150
151 pub fn namespace_api(&self) -> Box<NamespaceRoutes> {
153 Box::new(NamespaceRoutes::new(self.__client()))
154 }
155
156 pub fn transaction_api(&self) -> Box<TransactionRoutes> {
158 Box::new(TransactionRoutes::new(self.__client()))
159 }
160
161 pub fn resolver_api(&self) -> Box<ResolverRoutes> {
163 Box::new(ResolverRoutes::new(self.__client(), *self.namespace_api(), *self.mosaic_api()))
164 }
165
166 pub fn metadata_v2_api(&self) -> Box<MetadataV2Routes> {
173 Box::new(MetadataV2Routes::new(self.__client()))
174 }
175}
176
177impl Service<Request> for Client {
178 type Response = Response;
179 type Error = api::error::Error;
180 type Future = future::BoxFuture<'static, Result<Self::Response, Self::Error>>;
181
182 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
183 self.inner.poll_ready(cx).map_err(Into::into)
184 }
185
186 fn call(&mut self, request: Request) -> Self::Future {
187 let fut = self.inner.call(request);
188 Box::pin(async move { fut.await.map_err(Into::into) })
189 }
190}
191
192impl fmt::Debug for Client {
193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194 f.debug_struct("Channel").finish()
195 }
196}