Skip to content

Commit

Permalink
Merge pull request #83 from Dstack-TEE/json-prpc
Browse files Browse the repository at this point in the history
prpc: Choose json codec in prpc clients
  • Loading branch information
kvinwang authored Dec 26, 2024
2 parents f37d550 + 9f7be1e commit ab33cbc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ rcgen = { version = "0.13.1", features = ["pem"] }
x509-parser = "0.16.0"

# RPC/Protocol
prpc = "0.4.0"
prpc-build = "0.4.1"
prpc = "0.5.0"
prpc-build = "0.5.1"

# Development/Testing
bindgen = "0.70.1"
Expand Down
1 change: 1 addition & 0 deletions http-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hyperlocal.workspace = true
log.workspace = true
pin-project-lite = "0.2.15"
prpc = { workspace = true, optional = true }
serde.workspace = true
tokio.workspace = true
tokio-vsock.workspace = true
tower-service = "0.3.3"
Expand Down
20 changes: 16 additions & 4 deletions http-client/src/prpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use prpc::client::{Error, RequestClient};
use anyhow::Context;
use prpc::{
client::{Error, RequestClient},
serde_json, Message,
};
use serde::{de::DeserializeOwned, Serialize};

pub struct PrpcClient {
base_url: String,
Expand All @@ -11,11 +16,18 @@ impl PrpcClient {
}

impl RequestClient for PrpcClient {
async fn request(&self, path: &str, body: Vec<u8>) -> Result<Vec<u8>, Error> {
let (status, body) = super::http_request("POST", &self.base_url, path, &body).await?;
async fn request<T, R>(&self, path: &str, body: T) -> Result<R, Error>
where
T: Message + Serialize,
R: Message + DeserializeOwned,
{
let body = serde_json::to_vec(&body).context("Failed to serialize body")?;
let path = format!("{path}?json");
let (status, body) = super::http_request("POST", &self.base_url, &path, &body).await?;
if status != 200 {
return Err(Error::RpcError(format!("Invalid status code: {status}")));
}
Ok(body)
let response = serde_json::from_slice(&body).context("Failed to deserialize response")?;
Ok(response)
}
}
1 change: 1 addition & 0 deletions ra-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reqwest = { workspace = true, default-features = false, features = ["rustls-tls"
ra-tls.workspace = true
bon.workspace = true
rocket-vsock-listener = { workspace = true, optional = true }
serde.workspace = true

[features]
default = ["rocket", "client"]
Expand Down
13 changes: 10 additions & 3 deletions ra-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use prpc::{
Message,
};
use reqwest::{Certificate, Client, Identity};
use serde::{de::DeserializeOwned, Serialize};

pub struct RaClient {
remote_uri: String,
Expand Down Expand Up @@ -48,8 +49,13 @@ impl RaClient {
}

impl RequestClient for RaClient {
async fn request(&self, path: &str, body: Vec<u8>) -> Result<Vec<u8>, Error> {
let url = format!("{}/{}", self.remote_uri, path);
async fn request<T, R>(&self, path: &str, body: T) -> Result<R, Error>
where
T: Message + Serialize,
R: Message + DeserializeOwned,
{
let body = serde_json::to_vec(&body).context("Failed to serialize body")?;
let url = format!("{}/{}?json", self.remote_uri, path);
let response = self
.client
.post(url)
Expand All @@ -72,6 +78,7 @@ impl RequestClient for RaClient {
.await
.map_err(|err| Error::RpcError(format!("failed to read response: {:?}", err)))?
.to_vec();
Ok(body)
let response = serde_json::from_slice(&body).context("Failed to deserialize response")?;
Ok(response)
}
}

0 comments on commit ab33cbc

Please sign in to comment.