Skip to content

Commit 0530b75

Browse files
committed
Add base rest client setup.
1 parent 6f56c2e commit 0530b75

4 files changed

Lines changed: 87 additions & 14 deletions

File tree

client/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
protos = { path = "../protos" }
8+
reqwest = { version = "0.11.13", default-features = false, features = ["rustls-tls"] }
9+
tokio = { version = "1.38.0", default-features = false }
10+
prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] }

client/src/client.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use prost::Message;
2+
3+
use crate::error::LdkNodeServerError;
4+
use reqwest::header::CONTENT_TYPE;
5+
use reqwest::Client;
6+
7+
const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";
8+
9+
/// Client to access a hosted instance of LDK Node Server.
10+
#[derive(Clone)]
11+
pub struct LdkNodeServerClient {
12+
base_url: String,
13+
client: Client,
14+
}
15+
16+
impl LdkNodeServerClient {
17+
/// Constructs a [`LdkNodeServerClient`] using `base_url` as the server endpoint.
18+
pub fn new(base_url: String) -> Self {
19+
Self { base_url, client: Client::new() }
20+
}
21+
22+
async fn post_request<Rq: Message, Rs: Message + Default>(
23+
&self, request: &Rq, url: &str,
24+
) -> Result<Rs, LdkNodeServerError> {
25+
let request_body = request.encode_to_vec();
26+
let response_raw = match self
27+
.client
28+
.post(url)
29+
.header(CONTENT_TYPE, APPLICATION_OCTET_STREAM)
30+
.body(request_body)
31+
.send()
32+
.await
33+
{
34+
Ok(response) => response,
35+
Err(e) => {
36+
return Err(LdkNodeServerError::InternalError(e.to_string()));
37+
},
38+
};
39+
let status = response_raw.status();
40+
let payload = response_raw.bytes().await?;
41+
42+
if status.is_success() {
43+
Ok(Rs::decode(&payload[..])?)
44+
} else {
45+
//TODO: Error handling and error response parsing.
46+
Err(LdkNodeServerError::InternalError("Unknown Error".to_string()))
47+
}
48+
}
49+
}

client/src/error.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use prost::DecodeError;
2+
3+
/// When there is an error in request to LDK Node Server, the response contains a relevant error code.
4+
#[derive(Debug)]
5+
pub enum LdkNodeServerError {
6+
/// There is an unknown error. (Placeholder until error handling is done.)
7+
InternalError(String),
8+
}
9+
10+
impl From<DecodeError> for LdkNodeServerError {
11+
fn from(err: DecodeError) -> Self {
12+
LdkNodeServerError::InternalError(err.to_string())
13+
}
14+
}
15+
16+
impl From<reqwest::Error> for LdkNodeServerError {
17+
fn from(err: reqwest::Error) -> Self {
18+
LdkNodeServerError::InternalError(err.to_string())
19+
}
20+
}

client/src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
pub fn add(left: usize, right: usize) -> usize {
2-
left + right
3-
}
4-
5-
#[cfg(test)]
6-
mod tests {
7-
use super::*;
8-
9-
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
13-
}
14-
}
1+
//! Client-side library to interact with LDK Node Server.
2+
3+
#![deny(rustdoc::broken_intra_doc_links)]
4+
#![deny(rustdoc::private_intra_doc_links)]
5+
#![deny(missing_docs)]
6+
7+
/// Implements a client ([`client::LdkNodeServerClient`]) to access a hosted instance of LDK Node Server.
8+
pub mod client;
9+
10+
/// Implements the error type ([`error::LdkNodeServerError`]) returned on interacting with [`client::LdkNodeServerClient`]
11+
pub mod error;
12+
13+
/// Request/Response structs required for interacting with the client.
14+
pub use protos;

0 commit comments

Comments
 (0)