1use crate::client::GrpcClient;
4use crate::error::GrpcError;
5use bytes::Bytes;
6use serde::{de::DeserializeOwned, Serialize};
7use sova_core::Request;
8
9pub trait GrpcExt {
10 fn grpc(&self) -> GrpcBound;
11 fn try_grpc(&self) -> Option<GrpcBound>;
12}
13
14impl GrpcExt for Request {
15 fn grpc(&self) -> GrpcBound {
16 GrpcBound {
17 client: self.state::<GrpcClient>(),
18 }
19 }
20
21 fn try_grpc(&self) -> Option<GrpcBound> {
22 self.try_state::<GrpcClient>()
23 .map(|client| GrpcBound { client })
24 }
25}
26
27pub struct GrpcBound {
28 client: std::sync::Arc<GrpcClient>,
29}
30
31impl GrpcBound {
32 pub fn client(&self) -> &GrpcClient {
33 &self.client
34 }
35
36 pub async fn call<Req, Res>(&self, method: &str, req: &Req) -> Result<Res, GrpcError>
37 where
38 Req: Serialize,
39 Res: DeserializeOwned,
40 {
41 self.client.call(method, req).await
42 }
43
44 pub async fn call_raw(&self, method: &str, body: Bytes) -> Result<Bytes, GrpcError> {
45 self.client.call_raw(method, body).await
46 }
47}