Struct minerva_rpc::metadata::ClientInterceptor
source · [−]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
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for ClientInterceptor
impl Send for ClientInterceptor
impl Sync for ClientInterceptor
impl Unpin for ClientInterceptor
impl UnwindSafe for ClientInterceptor
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
sourcefn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T
in a tonic::Request