multiversx_sdk_http/
gateway_http_proxy.rs1mod http_account;
2mod http_block;
3mod http_chain_simulator;
4mod http_network;
5mod http_tx;
6
7use std::time::Duration;
8
9use multiversx_sdk::gateway::{GatewayAsyncService, GatewayRequest};
10
11#[derive(Clone, Debug)]
13pub struct GatewayHttpProxy {
14 pub(crate) proxy_uri: String,
15 pub(crate) client: reqwest::Client,
16}
17
18impl GatewayHttpProxy {
19 pub fn new(proxy_uri: String) -> Self {
20 Self {
21 proxy_uri,
22 client: reqwest::Client::new(),
23 }
24 }
25
26 pub async fn http_request<G>(&self, request: G) -> anyhow::Result<G::Result>
29 where
30 G: GatewayRequest,
31 {
32 let url = format!("{}/{}", self.proxy_uri, request.get_endpoint());
33 let mut request_builder;
34 match request.request_type() {
35 multiversx_sdk::gateway::GatewayRequestType::Get => {
36 request_builder = self.client.get(url);
37 },
38 multiversx_sdk::gateway::GatewayRequestType::Post => {
39 request_builder = self.client.post(url);
40 },
41 }
42
43 if let Some(payload) = request.get_payload() {
44 request_builder = request_builder.json(&payload);
45 }
46
47 let decoded = request_builder
48 .send()
49 .await?
50 .json::<G::DecodedJson>()
51 .await?;
52
53 request.process_json(decoded)
54 }
55}
56
57impl GatewayAsyncService for GatewayHttpProxy {
58 type Instant = std::time::Instant;
59
60 fn from_uri(uri: &str) -> Self {
61 Self::new(uri.to_owned())
62 }
63
64 fn request<G>(&self, request: G) -> impl std::future::Future<Output = anyhow::Result<G::Result>>
65 where
66 G: multiversx_sdk::gateway::GatewayRequest,
67 {
68 self.http_request(request)
69 }
70
71 fn sleep(&self, millis: u64) -> impl std::future::Future<Output = ()> {
72 tokio::time::sleep(Duration::from_millis(millis))
73 }
74
75 fn now(&self) -> Self::Instant {
76 std::time::Instant::now()
77 }
78
79 fn elapsed_seconds(&self, instant: &Self::Instant) -> f32 {
80 instant.elapsed().as_secs_f32()
81 }
82}