diff --git a/Cargo.lock b/Cargo.lock index 3be1e594e..5f6624a9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1949,6 +1949,7 @@ dependencies = [ "log", "pin-project-lite", "prpc", + "serde", "tokio", "tokio-vsock", "tower-service", @@ -3420,9 +3421,9 @@ dependencies = [ [[package]] name = "prpc" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bf27f5c46f289f99f68086d3a24b58c9f4f66c3143a1780d5c0589c79b17c81" +checksum = "e36dbacc22fa64d2059cc8df1e929ab9100804ae2011548b6679f923c1172f42" dependencies = [ "anyhow", "async-trait", @@ -3439,9 +3440,9 @@ dependencies = [ [[package]] name = "prpc-build" -version = "0.4.1" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caf018aca01f6c5e7b0c312e484b29ebc63cf42404e4c35d105b1703e6350bfb" +checksum = "5034baf630735948c9df3cd637f77c2897413934ba1f201068d168080ab4e510" dependencies = [ "either", "fs-err", @@ -3544,6 +3545,7 @@ dependencies = [ "reqwest 0.12.9", "rocket", "rocket-vsock-listener", + "serde", "serde_json", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 596342bf5..d6deb266e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/http-client/Cargo.toml b/http-client/Cargo.toml index 45380481c..409f42821 100644 --- a/http-client/Cargo.toml +++ b/http-client/Cargo.toml @@ -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" diff --git a/http-client/src/prpc.rs b/http-client/src/prpc.rs index 59e0f2874..75daa3ab4 100644 --- a/http-client/src/prpc.rs +++ b/http-client/src/prpc.rs @@ -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, @@ -11,11 +16,18 @@ impl PrpcClient { } impl RequestClient for PrpcClient { - async fn request(&self, path: &str, body: Vec) -> Result, Error> { - let (status, body) = super::http_request("POST", &self.base_url, path, &body).await?; + async fn request(&self, path: &str, body: T) -> Result + 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) } } diff --git a/ra-rpc/Cargo.toml b/ra-rpc/Cargo.toml index 3bba01816..9e7aaf073 100644 --- a/ra-rpc/Cargo.toml +++ b/ra-rpc/Cargo.toml @@ -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"] diff --git a/ra-rpc/src/client.rs b/ra-rpc/src/client.rs index 3eb45b7b6..e4105c335 100644 --- a/ra-rpc/src/client.rs +++ b/ra-rpc/src/client.rs @@ -7,6 +7,7 @@ use prpc::{ Message, }; use reqwest::{Certificate, Client, Identity}; +use serde::{de::DeserializeOwned, Serialize}; pub struct RaClient { remote_uri: String, @@ -48,8 +49,13 @@ impl RaClient { } impl RequestClient for RaClient { - async fn request(&self, path: &str, body: Vec) -> Result, Error> { - let url = format!("{}/{}", self.remote_uri, path); + async fn request(&self, path: &str, body: T) -> Result + 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) @@ -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) } }