Skip to content

Commit ab33cbc

Browse files
authored
Merge pull request #83 from Dstack-TEE/json-prpc
prpc: Choose json codec in prpc clients
2 parents f37d550 + 9f7be1e commit ab33cbc

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

Cargo.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ rcgen = { version = "0.13.1", features = ["pem"] }
131131
x509-parser = "0.16.0"
132132

133133
# RPC/Protocol
134-
prpc = "0.4.0"
135-
prpc-build = "0.4.1"
134+
prpc = "0.5.0"
135+
prpc-build = "0.5.1"
136136

137137
# Development/Testing
138138
bindgen = "0.70.1"

http-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ hyperlocal.workspace = true
1414
log.workspace = true
1515
pin-project-lite = "0.2.15"
1616
prpc = { workspace = true, optional = true }
17+
serde.workspace = true
1718
tokio.workspace = true
1819
tokio-vsock.workspace = true
1920
tower-service = "0.3.3"

http-client/src/prpc.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use prpc::client::{Error, RequestClient};
1+
use anyhow::Context;
2+
use prpc::{
3+
client::{Error, RequestClient},
4+
serde_json, Message,
5+
};
6+
use serde::{de::DeserializeOwned, Serialize};
27

38
pub struct PrpcClient {
49
base_url: String,
@@ -11,11 +16,18 @@ impl PrpcClient {
1116
}
1217

1318
impl RequestClient for PrpcClient {
14-
async fn request(&self, path: &str, body: Vec<u8>) -> Result<Vec<u8>, Error> {
15-
let (status, body) = super::http_request("POST", &self.base_url, path, &body).await?;
19+
async fn request<T, R>(&self, path: &str, body: T) -> Result<R, Error>
20+
where
21+
T: Message + Serialize,
22+
R: Message + DeserializeOwned,
23+
{
24+
let body = serde_json::to_vec(&body).context("Failed to serialize body")?;
25+
let path = format!("{path}?json");
26+
let (status, body) = super::http_request("POST", &self.base_url, &path, &body).await?;
1627
if status != 200 {
1728
return Err(Error::RpcError(format!("Invalid status code: {status}")));
1829
}
19-
Ok(body)
30+
let response = serde_json::from_slice(&body).context("Failed to deserialize response")?;
31+
Ok(response)
2032
}
2133
}

ra-rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ reqwest = { workspace = true, default-features = false, features = ["rustls-tls"
1616
ra-tls.workspace = true
1717
bon.workspace = true
1818
rocket-vsock-listener = { workspace = true, optional = true }
19+
serde.workspace = true
1920

2021
[features]
2122
default = ["rocket", "client"]

ra-rpc/src/client.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use prpc::{
77
Message,
88
};
99
use reqwest::{Certificate, Client, Identity};
10+
use serde::{de::DeserializeOwned, Serialize};
1011

1112
pub struct RaClient {
1213
remote_uri: String,
@@ -48,8 +49,13 @@ impl RaClient {
4849
}
4950

5051
impl RequestClient for RaClient {
51-
async fn request(&self, path: &str, body: Vec<u8>) -> Result<Vec<u8>, Error> {
52-
let url = format!("{}/{}", self.remote_uri, path);
52+
async fn request<T, R>(&self, path: &str, body: T) -> Result<R, Error>
53+
where
54+
T: Message + Serialize,
55+
R: Message + DeserializeOwned,
56+
{
57+
let body = serde_json::to_vec(&body).context("Failed to serialize body")?;
58+
let url = format!("{}/{}?json", self.remote_uri, path);
5359
let response = self
5460
.client
5561
.post(url)
@@ -72,6 +78,7 @@ impl RequestClient for RaClient {
7278
.await
7379
.map_err(|err| Error::RpcError(format!("failed to read response: {:?}", err)))?
7480
.to_vec();
75-
Ok(body)
81+
let response = serde_json::from_slice(&body).context("Failed to deserialize response")?;
82+
Ok(response)
7683
}
7784
}

0 commit comments

Comments
 (0)