Victor Martinez
--- ## Ingredients to build a **gRPC API**
---
## EsPolicyGrpc
https://github.com/primait/es-policy-grpc
---
## Why a dedicated **repository**?
+ It is **not coupled** to a single project.
+ **Multiple projects** implement the same **gRPC** server.
+ We want **multiple domains** to use our gRPC libraries.
+ **Dedicated CI**, can grow without affecting directly an existing project.
---
## Project structure
https://github.com/primait/es-policy-grpc
---
## Project structure
https://github.com/primait/es-policy-grpc
---
## Defining a **request**
```proto
syntax = "proto3";
package es_policy_grpc.messages.issue_policy.request.v1;
import "es_policy_grpc/domain/v1/bundle.proto";
import "es_policy_grpc/domain/v1/coverage.proto";
import "es_policy_grpc/domain/v1/issuing_company.proto";
// ... etc
message IssuePolicyRequest {
google.protobuf.Timestamp requested_at = 1;
google.protobuf.Timestamp start_at = 2;
google.protobuf.Timestamp end_at = 3;
google.protobuf.Timestamp purchased_at = 4;
es_policy_grpc.domain.v1.TransactionInformation transaction = 5;
es_policy_grpc.domain.v1.IssuingCompany issuing_company = 6;
string quote_id = 7;
es_policy_grpc.domain.v1.QuoteSource quote_source = 8;
string application_id = 9;
string offer_id = 10;
es_policy_grpc.domain.v1.Price price = 11;
es_policy_grpc.domain.v1.Bundle bundle = 12;
repeated es_policy_grpc.domain.v1.ProductCover covers = 13;
es_policy_grpc.domain.v1.PolicyHolderInformation policy_holder_information = 14;
es_policy_grpc.domain.v1.VehicleInformation vehicle_information = 15;
es_policy_grpc.domain.v1.QuoteVersion quote_version = 16;
}
```
---
## Defining a **request**
```proto
syntax = "proto3";
package es_policy_grpc.domain.v1;
import "es_policy_grpc/domain/v1/price.proto";
import "google/protobuf/timestamp.proto";
message ProductCover {
CoverType cover_type = 1;
google.protobuf.Timestamp start_at = 2;
google.protobuf.Timestamp end_at = 3;
es_policy_grpc.domain.v1.Price price = 4;
optional RoadsideAssistanceTier roadside_assistance_tier = 5;
}
enum CoverType {
COVER_TYPE_UNSPECIFIED = 0;
COVER_TYPE_MANDATORY_THIRD_PARTY_LIABILITY = 1;
COVER_TYPE_VOLUNTARY_THIRD_PARTY_LIABILITY = 2;
COVER_TYPE_DRIVER_ACCIDENT = 3;
COVER_TYPE_WINDSHIELD = 4;
COVER_TYPE_THEFT = 5;
// ..etc
}
enum RoadsideAssistanceTier {
ROADSIDE_ASSISTANCE_TIER_UNSPECIFIED = 0;
ROADSIDE_ASSISTANCE_TIER_BASE = 1;
ROADSIDE_ASSISTANCE_TIER_PREMIUM = 2;
ROADSIDE_ASSISTANCE_TIER_PREMIUM_V2 = 3;
}
```
---
## Defining a **response**
```proto
syntax = "proto3";
package es_policy_grpc.messages.issue_policy.response.v1;
message IssuePolicyResponse {
string policy_id = 1;
}
```
---
## About **backwards** compatibility
---
### Required fields don't exist, everything has a default value
+ **Strings** => The empty string.
+ **Bytes** => Empty bytes.
+ **Bools** => False.
+ **Numeric** => Zero.
+ **Message** => Not set. Its exact value is language-dependent.
+ **Enums** => The first defined enum value, which must be 0.
https://protobuf.dev/programming-guides/proto3/#default
---
### Enums must have an "**Unspecified**" variant
```proto
enum BundleSlug {
BUNDLE_SLUG_UNSPECIFIED = 0;
BUNDLE_SLUG_TERCEROS_BASICO = 1;
BUNDLE_SLUG_TERCEROS_AMPLIADO = 2;
BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA = 3;
BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA_300 = 4;
BUNDLE_SLUG_TODO_RIESGO_CON_FRANQUICIA_500 = 5;
}
```
https://protobuf.dev/best-practices/dos-donts/#unspecified-enum
---
### Field tags must **never** be reused
And deleted fields must be marked as reserved
```proto
message AmendWithdrawalRequest {
string policy_id = 1;
google.protobuf.Timestamp requested_at = 2;
reserved 3;
reserved "interruption_at";
optional string description = 4;
oneof reason {
es_policy_grpc.messages.withdraw_policy.request.v1.CustomerWithdrawReason customer = 5;
}
}
```
https://protobuf.dev/best-practices/dos-donts#reuse-number
---
### Learn more about **Do's** and **Dont's**
https://protobuf.dev/best-practices/dos-donts
---
## Defining a **service**
```proto
syntax = "proto3";
package es_policy_grpc.service.v1;
import "es_policy_grpc/messages/issue_policy/request/v1/request.proto";
import "es_policy_grpc/messages/issue_policy/response/v1/response.proto";
service PolicyManagementService {
rpc IssuePolicy(es_policy_grpc.messages.issue_policy.request.v1.IssuePolicyRequest) returns (es_policy_grpc.messages.issue_policy.response.v1.IssuePolicyResponse);
}
```
---
---
### Build script
```rust
use std::io::Result;
fn main() -> Result<()> {
// List of proto files containing a message definition
let proto_files = &[
//Domain
"proto/es_policy_grpc/domain/v1/address.proto",
"proto/es_policy_grpc/domain/v1/bundle.proto",
// etc.
// Messages
"proto/es_policy_grpc/messages/issue_policy/request/v1/request.proto",
"proto/es_policy_grpc/messages/issue_policy/response/v1/response.proto",
// Services
"proto/es_policy_grpc/service/v1/service.proto",
];
// Name of the folder containing the proto definitions
let proto_folder = "proto";
tonic_prost_build::configure()
.protoc_arg("--experimental_allow_proto3_optional")
.compile_protos(proto_files, &[proto_folder])
.unwrap();
Ok(())
}
```
---
### Exposing the generated code
```rust
// src/lib.rs
pub mod domain {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.domain.v1.rs",));
}
}
pub mod messages {
pub mod issue_policy {
pub mod request {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.messages.issue_policy.request.v1.rs"));
}
}
pub mod response {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.messages.issue_policy.response.v1.rs"));
}
}
}
}
pub mod policy_service {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/es_policy_grpc.service.v1.rs"));
}
}
```
---
Data gathered from **October 29th** to **November 12th**
---
### Some metrics
Data gathered from **October 29th** to **November 12th**
---
# Thank you
:purple_heart: