breez_sdk_spark/common/
rest.rs

1use 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    /// Makes a GET request and logs on DEBUG.
9    /// ### Arguments
10    /// - `url`: the URL on which GET will be called
11    /// - `headers`: optional headers that will be set on the request
12    async fn get_request(
13        &self,
14        url: String,
15        headers: Option<HashMap<String, String>>,
16    ) -> Result<RestResponse, ServiceConnectivityError>;
17
18    /// Makes a POST request, and logs on DEBUG.
19    /// ### Arguments
20    /// - `url`: the URL on which POST will be called
21    /// - `headers`: the optional POST headers
22    /// - `body`: the optional POST body
23    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    /// Makes a DELETE request, and logs on DEBUG.
31    /// ### Arguments
32    /// - `url`: the URL on which DELETE will be called
33    /// - `headers`: the optional DELETE headers
34    /// - `body`: the optional DELETE body
35    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}