Skip to content

Commit e453109

Browse files
committed
Add initial set of api methods to rest-client.
1 parent 0530b75 commit e453109

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

client/src/client.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
use prost::Message;
22

33
use crate::error::LdkNodeServerError;
4+
use protos::{
5+
Bolt11ReceiveRequest, Bolt11ReceiveResponse, Bolt11SendRequest, Bolt11SendResponse,
6+
Bolt12ReceiveRequest, Bolt12ReceiveResponse, Bolt12SendRequest, Bolt12SendResponse,
7+
CloseChannelRequest, CloseChannelResponse, ListChannelsRequest, ListChannelsResponse,
8+
OnchainReceiveRequest, OnchainReceiveResponse, OnchainSendRequest, OnchainSendResponse,
9+
OpenChannelRequest, OpenChannelResponse,
10+
};
411
use reqwest::header::CONTENT_TYPE;
512
use reqwest::Client;
613

714
const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";
815

16+
const ONCHAIN_RECEIVE_PATH: &str = "OnchainReceive";
17+
const ONCHAIN_SEND_PATH: &str = "OnchainSend";
18+
const BOLT11_RECEIVE_PATH: &str = "Bolt11Receive";
19+
const BOLT11_SEND_PATH: &str = "Bolt11Send";
20+
const BOLT12_RECEIVE_PATH: &str = "Bolt12Receive";
21+
const BOLT12_SEND_PATH: &str = "Bolt12Send";
22+
const OPEN_CHANNEL_PATH: &str = "OpenChannel";
23+
const CLOSE_CHANNEL_PATH: &str = "CloseChannel";
24+
const LIST_CHANNELS_PATH: &str = "ListChannels";
25+
926
/// Client to access a hosted instance of LDK Node Server.
1027
#[derive(Clone)]
1128
pub struct LdkNodeServerClient {
@@ -19,6 +36,87 @@ impl LdkNodeServerClient {
1936
Self { base_url, client: Client::new() }
2037
}
2138

39+
/// Retrieve a new on-chain funding address.
40+
/// For API contract/usage, refer to docs for [`OnchainReceiveRequest`] and [`OnchainReceiveResponse`].
41+
pub async fn onchain_receive(
42+
&self, request: OnchainReceiveRequest,
43+
) -> Result<OnchainReceiveResponse, LdkNodeServerError> {
44+
let url = format!("http://{}/{ONCHAIN_RECEIVE_PATH}", self.base_url);
45+
self.post_request(&request, &url).await
46+
}
47+
48+
/// Send an on-chain payment to the given address.
49+
/// For API contract/usage, refer to docs for [`OnchainSendRequest`] and [`OnchainSendResponse`].
50+
pub async fn onchain_send(
51+
&self, request: OnchainSendRequest,
52+
) -> Result<OnchainSendResponse, LdkNodeServerError> {
53+
let url = format!("http://{}/{ONCHAIN_SEND_PATH}", self.base_url);
54+
self.post_request(&request, &url).await
55+
}
56+
57+
/// Retrieve a new BOLT11 payable invoice.
58+
/// For API contract/usage, refer to docs for [`Bolt11ReceiveRequest`] and [`Bolt11ReceiveResponse`].
59+
pub async fn bolt11_receive(
60+
&self, request: Bolt11ReceiveRequest,
61+
) -> Result<Bolt11ReceiveResponse, LdkNodeServerError> {
62+
let url = format!("http://{}/{BOLT11_RECEIVE_PATH}", self.base_url);
63+
self.post_request(&request, &url).await
64+
}
65+
66+
/// Send a payment for a BOLT11 invoice.
67+
/// For API contract/usage, refer to docs for [`Bolt11SendRequest`] and [`Bolt11SendResponse`].
68+
pub async fn bolt11_send(
69+
&self, request: Bolt11SendRequest,
70+
) -> Result<Bolt11SendResponse, LdkNodeServerError> {
71+
let url = format!("http://{}/{BOLT11_SEND_PATH}", self.base_url);
72+
self.post_request(&request, &url).await
73+
}
74+
75+
/// Retrieve a new BOLT11 payable offer.
76+
/// For API contract/usage, refer to docs for [`Bolt12ReceiveRequest`] and [`Bolt12ReceiveResponse`].
77+
pub async fn bolt12_receive(
78+
&self, request: Bolt12ReceiveRequest,
79+
) -> Result<Bolt12ReceiveResponse, LdkNodeServerError> {
80+
let url = format!("http://{}/{BOLT12_RECEIVE_PATH}", self.base_url);
81+
self.post_request(&request, &url).await
82+
}
83+
84+
/// Send a payment for a BOLT12 offer.
85+
/// For API contract/usage, refer to docs for [`Bolt12SendRequest`] and [`Bolt12SendResponse`].
86+
pub async fn bolt12_send(
87+
&self, request: Bolt12SendRequest,
88+
) -> Result<Bolt12SendResponse, LdkNodeServerError> {
89+
let url = format!("http://{}/{BOLT12_SEND_PATH}", self.base_url);
90+
self.post_request(&request, &url).await
91+
}
92+
93+
/// Creates a new outbound channel.
94+
/// For API contract/usage, refer to docs for [`OpenChannelRequest`] and [`OpenChannelResponse`].
95+
pub async fn open_channel(
96+
&self, request: OpenChannelRequest,
97+
) -> Result<OpenChannelResponse, LdkNodeServerError> {
98+
let url = format!("http://{}/{OPEN_CHANNEL_PATH}", self.base_url);
99+
self.post_request(&request, &url).await
100+
}
101+
102+
/// Closes the channel specified by given request.
103+
/// For API contract/usage, refer to docs for [`CloseChannelRequest`] and [`CloseChannelResponse`].
104+
pub async fn close_channel(
105+
&self, request: CloseChannelRequest,
106+
) -> Result<CloseChannelResponse, LdkNodeServerError> {
107+
let url = format!("http://{}/{CLOSE_CHANNEL_PATH}", self.base_url);
108+
self.post_request(&request, &url).await
109+
}
110+
111+
/// Retrieves list of known channels.
112+
/// For API contract/usage, refer to docs for [`ListChannelsRequest`] and [`ListChannelsResponse`].
113+
pub async fn list_channels(
114+
&self, request: ListChannelsRequest,
115+
) -> Result<ListChannelsResponse, LdkNodeServerError> {
116+
let url = format!("http://{}/{LIST_CHANNELS_PATH}", self.base_url);
117+
self.post_request(&request, &url).await
118+
}
119+
22120
async fn post_request<Rq: Message, Rs: Message + Default>(
23121
&self, request: &Rq, url: &str,
24122
) -> Result<Rs, LdkNodeServerError> {

0 commit comments

Comments
 (0)