breez_sdk_spark/common/
rest.rs

1use std::{collections::HashMap, sync::Arc};
2
3use crate::ServiceConnectivityError;
4
5/// REST client trait for making HTTP requests.
6///
7/// This trait provides a way for users to supply their own HTTP client implementation
8/// for use with the SDK. The SDK will use this client for all HTTP operations including
9/// LNURL flows and chain service requests.
10#[cfg_attr(feature = "uniffi", uniffi::export(with_foreign))]
11#[macros::async_trait]
12pub trait RestClient: Send + Sync {
13    /// Makes a GET request and logs on DEBUG.
14    /// ### Arguments
15    /// - `url`: the URL on which GET will be called
16    /// - `headers`: optional headers that will be set on the request
17    async fn get_request(
18        &self,
19        url: String,
20        headers: Option<HashMap<String, String>>,
21    ) -> Result<RestResponse, ServiceConnectivityError>;
22
23    /// Makes a POST request, and logs on DEBUG.
24    /// ### Arguments
25    /// - `url`: the URL on which POST will be called
26    /// - `headers`: the optional POST headers
27    /// - `body`: the optional POST body
28    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    /// Makes a DELETE request, and logs on DEBUG.
36    /// ### Arguments
37    /// - `url`: the URL on which DELETE will be called
38    /// - `headers`: the optional DELETE headers
39    /// - `body`: the optional DELETE body
40    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
56/// Wrapper that adapts an external `RestClient` to `platform_utils::HttpClient`
57pub(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}