pub struct ClientInterceptor {
    tenant: String,
    requestor: String,
}
Expand description

Defines the interceptor data for any gRPC client.

The interceptor exists for the sole purpose of introducing tenant and requestor data within a given request, that is going to be sent by the client connected to a service.

Example

Consider the following dummy service that allows a remote procedure call to a ping function.

// example.proto

syntax = "proto3";
import "google/protobuf/empty.proto";

package Example;

service Example {
  rpc ping(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}

Imagine that this single procedure implementation requires that tenant and requestor data are embedded within a request for this to work. For example:

// example.rs
tonic::include_proto!("example");

#[derive(Clone)]
pub struct ExampleService;

#[tonic::async_trait]
impl Example for ExampleService {
    async fn ping(&self, req: Request<()>) -> Result<(), Status> {
        let tenant = metadata::get_value(req.metadata(), "tenant")
            .ok_or_else(|| Status::failed_precondition("Missing tenant data"))?;
        let requestor = metadata::get_value(req.metadata(), "requestor")
            .ok_or_else(|| Status::failed_precondition("Missing requestor data"))?;
        println!("Tenant: {}, requestor: {}", tenant, requestor);
        Ok(Response::new(()))
    }
}

One may want to use this structure to send data as needed.

// example.rs
fn test_request() {
    use tonic::transport::Channel;
    let channel = Channel::from_static("http://localhost:1234").connect().await?;
    let client = test_client::TestClient::with_interceptor(
        channel,
        ClientInterceptor::new("company", "admin"),
    );
    let response = client.ping(Request::new(())).await?;
}

Fields

tenant: String

Tenant related to the database access while on a request.

requestor: String

Requestor of the operation, usually the login of a user.

Implementations

Creates a new ClientInterceptor from given tenant and requestor names. The tenant is closely related to the database to be manipulated, while the requestor usually is the login of the user performing the manipulation.

Trait Implementations

Intercept a request before it is sent, optionally cancelling it.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Wrap the input message T in a tonic::Request
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more