Skip to content

Commit

Permalink
lsps0: client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Sep 28, 2023
1 parent b34e28e commit 3fb0e5b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
67 changes: 67 additions & 0 deletions libs/sdk-core/src/lsps0/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use serde::{Deserialize, Serialize};
use std::{sync::Arc, time::Duration};
use tokio::sync::mpsc;

use super::{error::Error, transport::Transport};

#[derive(Debug, Serialize)]
pub struct ListProtocolsRequest {}

#[derive(Debug, Deserialize)]
pub struct ListProtocolsResponse {
pub protocols: Vec<i32>,
}

pub struct Client {
transport: Arc<Transport>,
peer_id: Vec<u8>,
timeout: Duration,
}

impl Client {
#[allow(dead_code)]
pub fn new(transport: Arc<Transport>, peer_id: Vec<u8>, timeout: Duration) -> Self {
Self {
transport,
peer_id,
timeout,
}
}

#[allow(dead_code)]
pub async fn call<TRequest, TResponse>(
&self,
method: String,
req: TRequest,
) -> Result<TResponse, Error>
where
TRequest: serde::Serialize,
TResponse: serde::de::DeserializeOwned,
{
self.transport
.request_response(method, self.peer_id.clone(), &req, self.timeout)
.await
}

#[allow(dead_code)]
pub async fn stream_notifications<TNotification>(
&self,
method: String,
) -> Result<mpsc::Receiver<TNotification>, Error>
where
TNotification: serde::de::DeserializeOwned + std::marker::Send + 'static,
{
self.transport
.stream_notifications(method, self.peer_id.clone())
.await
}

#[allow(dead_code)]
pub async fn list_protocols(&self) -> Result<ListProtocolsResponse, Error> {
self.call(
String::from("lsps0.list_protocols"),
ListProtocolsRequest {},
)
.await
}
}
4 changes: 4 additions & 0 deletions libs/sdk-core/src/lsps0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pub(crate) mod client;
pub(crate) mod error;
pub(crate) mod jsonrpc;
pub(crate) mod transport;

#[allow(unused_imports)]
pub(crate) use client::Client;

#[allow(unused_imports)]
pub(crate) use transport::Transport;

Expand Down
2 changes: 0 additions & 2 deletions libs/sdk-core/src/lsps0/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ impl Transport {
}
}

#[allow(dead_code)]
pub async fn request_response<TRequest, TResponse>(
&self,
method: String,
Expand Down Expand Up @@ -271,7 +270,6 @@ impl Transport {
}
}

#[allow(dead_code)]
pub async fn stream_notifications<TNotification>(
&self,
method: String,
Expand Down

0 comments on commit 3fb0e5b

Please sign in to comment.