-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters