breez_sdk_spark/common/
rest.rs1use std::{collections::HashMap, sync::Arc};
2
3use crate::ServiceConnectivityError;
4
5#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
6#[macros::async_trait]
7pub trait RestClient: Send + Sync {
8 async fn get_request(
13 &self,
14 url: String,
15 headers: Option<HashMap<String, String>>,
16 ) -> Result<RestResponse, ServiceConnectivityError>;
17
18 async fn post_request(
24 &self,
25 url: String,
26 headers: Option<HashMap<String, String>>,
27 body: Option<String>,
28 ) -> Result<RestResponse, ServiceConnectivityError>;
29
30 async fn delete_request(
36 &self,
37 url: String,
38 headers: Option<HashMap<String, String>>,
39 body: Option<String>,
40 ) -> Result<RestResponse, ServiceConnectivityError>;
41}
42
43#[macros::derive_from(breez_sdk_common::rest::RestResponse)]
44#[macros::derive_into(breez_sdk_common::rest::RestResponse)]
45#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
46pub struct RestResponse {
47 pub status: u16,
48 pub body: String,
49}
50
51pub(crate) struct RestClientWrapper {
52 inner: Arc<dyn RestClient>,
53}
54
55impl RestClientWrapper {
56 pub fn new(inner: Arc<dyn RestClient>) -> Self {
57 RestClientWrapper { inner }
58 }
59}
60
61#[macros::async_trait]
62impl breez_sdk_common::rest::RestClient for RestClientWrapper {
63 async fn get_request(
64 &self,
65 url: String,
66 headers: Option<HashMap<String, String>>,
67 ) -> Result<
68 breez_sdk_common::rest::RestResponse,
69 breez_sdk_common::error::ServiceConnectivityError,
70 > {
71 Ok(self.inner.get_request(url, headers).await?.into())
72 }
73
74 async fn post_request(
75 &self,
76 url: String,
77 headers: Option<HashMap<String, String>>,
78 body: Option<String>,
79 ) -> Result<
80 breez_sdk_common::rest::RestResponse,
81 breez_sdk_common::error::ServiceConnectivityError,
82 > {
83 Ok(self.inner.post_request(url, headers, body).await?.into())
84 }
85
86 async fn delete_request(
87 &self,
88 url: String,
89 headers: Option<HashMap<String, String>>,
90 body: Option<String>,
91 ) -> Result<
92 breez_sdk_common::rest::RestResponse,
93 breez_sdk_common::error::ServiceConnectivityError,
94 > {
95 Ok(self.inner.delete_request(url, headers, body).await?.into())
96 }
97}