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))]
11#[macros::async_trait]
12pub trait RestClient: Send + Sync {
13 async fn get_request(
18 &self,
19 url: String,
20 headers: Option<HashMap<String, String>>,
21 ) -> Result<RestResponse, ServiceConnectivityError>;
22
23 async fn post_request(
29 &self,
30 url: String,
31 headers: Option<HashMap<String, String>>,
32 body: Option<String>,
33 ) -> Result<RestResponse, ServiceConnectivityError>;
34
35 async fn delete_request(
41 &self,
42 url: String,
43 headers: Option<HashMap<String, String>>,
44 body: Option<String>,
45 ) -> Result<RestResponse, ServiceConnectivityError>;
46}
47
48#[macros::derive_from(platform_utils::HttpResponse)]
49#[macros::derive_into(platform_utils::HttpResponse)]
50#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
51pub struct RestResponse {
52 pub status: u16,
53 pub body: String,
54}
55
56pub(crate) struct RestClientWrapper {
58 inner: Arc<dyn RestClient>,
59}
60
61impl RestClientWrapper {
62 pub fn new(inner: Arc<dyn RestClient>) -> Self {
63 RestClientWrapper { inner }
64 }
65}
66
67#[macros::async_trait]
68impl platform_utils::HttpClient for RestClientWrapper {
69 async fn get(
70 &self,
71 url: String,
72 headers: Option<HashMap<String, String>>,
73 ) -> Result<platform_utils::HttpResponse, platform_utils::HttpError> {
74 Ok(self.inner.get_request(url, headers).await?.into())
75 }
76
77 async fn post(
78 &self,
79 url: String,
80 headers: Option<HashMap<String, String>>,
81 body: Option<String>,
82 ) -> Result<platform_utils::HttpResponse, platform_utils::HttpError> {
83 Ok(self.inner.post_request(url, headers, body).await?.into())
84 }
85
86 async fn delete(
87 &self,
88 url: String,
89 headers: Option<HashMap<String, String>>,
90 body: Option<String>,
91 ) -> Result<platform_utils::HttpResponse, platform_utils::HttpError> {
92 Ok(self.inner.delete_request(url, headers, body).await?.into())
93 }
94}