From c29455218f44a616ce8d6a879f777f09e9480f1a Mon Sep 17 00:00:00 2001 From: JasterV <49537445+JasterV@users.noreply.github.com> Date: Mon, 16 Jun 2025 01:10:21 +0200 Subject: [PATCH] Update slides --- codebook.toml | 1 + docs/learning_grpc.md | 47 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/codebook.toml b/codebook.toml index 012c54f..8cd3e40 100644 --- a/codebook.toml +++ b/codebook.toml @@ -1,5 +1,6 @@ words = [ "chrono", + "dyn", "idl", "prima", "prost", diff --git a/docs/learning_grpc.md b/docs/learning_grpc.md index 94f716a..a6ef4bc 100644 --- a/docs/learning_grpc.md +++ b/docs/learning_grpc.md @@ -437,7 +437,8 @@ note: We need to expose the generated code through our lib.rs --- -## Auto generated services + +## Filling the gaps ```rust pub trait PolicyManagementService { @@ -454,10 +455,44 @@ note: We get a trait generated from the Protobuf Service definition --- -## Building a server + +## Filling the gaps + +```rust +use es_policy_grpc::policy_service::v1::PolicyManagementService; +use es_policy_grpc::messages::withdraw_policy::request::v1::WithdrawPolicyRequest; +use es_policy_grpc::messages::withdraw_policy::response::v1::WithdrawPolicyResponse; +use tonic::{Request, Response, Status}; + +pub struct PolicyManagementServiceImpl { + application: Arc, +} + +impl PolicyManagementService for PolicyManagementServiceImpl { + async fn withdraw_policy( + &self, + request: Request, + ) -> Result, Status> { + let request = request.into_inner(); + + let policy_id = Uuid::parse_str(&request.policy_id).unwrap(); + let details = request.try_to_domain().unwrap(); + + self.application.cancel_policy(policy_id, details).await.unwrap() + + Ok(Response::new(WithdrawPolicyResponse { + policy_id: policy_id.to_string(), + })) + } + // .. +} + +``` + +--- +## Building the server ```rust -// main.rs use tonic::Server as GrpcServer; use es_policy_grpc::policy_service::v1::PolicyManagementServiceServer as PolicyManagementServerStub; @@ -484,10 +519,9 @@ Simple build of a Tonic Server. We will dive into how to add middleware later. Highlight the fact that at the end of the day the gRPC server will be listening to a TCP port like any other HTTP2 server. --- -## Building a client +## Building the client ```rust -// Auto-generated client stub use es_policy_grpc::policy_service::v1::PolicyManagementServiceClient as PolicyManagementClientStub; use tonic::{metadata::MetadataValue, Request}; use es_policy_grpc::messages::decline_renewal::request::v1::{ @@ -496,7 +530,8 @@ use es_policy_grpc::messages::decline_renewal::request::v1::{ CustomerDeclineRenewalReason }; -let mut client = PolicyManagementClientStub::connect("http://[::1]:50051").await?; +// Auto-generated client stub +let mut client = PolicyManagementClientStub::connect("http://localhost:50051").await?; let mut request = Request::new(DeclineRenewalRequest { policy_id: uuid::Uuid::new_v4(),