breez_sdk_core/lsps0/
client.rs

1use serde::{Deserialize, Serialize};
2use std::{sync::Arc, time::Duration};
3use tokio::sync::mpsc;
4
5use super::{error::Error, transport::Transport};
6
7#[derive(Debug, Serialize)]
8pub struct ListProtocolsRequest {}
9
10#[derive(Debug, Deserialize)]
11pub struct ListProtocolsResponse {
12    #[allow(dead_code)]
13    pub protocols: Vec<i32>,
14}
15
16pub struct Client {
17    transport: Arc<Transport>,
18    peer_id: Vec<u8>,
19    timeout: Duration,
20}
21
22impl Client {
23    #[allow(dead_code)]
24    pub fn new(transport: Arc<Transport>, peer_id: Vec<u8>, timeout: Duration) -> Self {
25        Self {
26            transport,
27            peer_id,
28            timeout,
29        }
30    }
31
32    pub async fn call<TRequest, TResponse>(
33        &self,
34        method: String,
35        req: TRequest,
36    ) -> Result<TResponse, Error>
37    where
38        TRequest: serde::Serialize,
39        TResponse: serde::de::DeserializeOwned,
40    {
41        self.transport
42            .request_response(method, self.peer_id.clone(), &req, self.timeout)
43            .await
44    }
45
46    #[allow(dead_code)]
47    pub async fn stream_notifications<TNotification>(
48        &self,
49        method: String,
50    ) -> Result<mpsc::Receiver<TNotification>, Error>
51    where
52        TNotification: serde::de::DeserializeOwned + std::marker::Send + 'static,
53    {
54        self.transport
55            .stream_notifications(method, self.peer_id.clone())
56            .await
57    }
58
59    #[allow(dead_code)]
60    pub async fn list_protocols(&self) -> Result<ListProtocolsResponse, Error> {
61        self.call(
62            String::from("lsps0.list_protocols"),
63            ListProtocolsRequest {},
64        )
65        .await
66    }
67}