feat: Add gRPC interceptors (#232)
This change introduces proper gRPC interceptors that are avilable regardless of the transport used. Each codegen service now produces an additional method called `with_interceptor` that accepts a `Interceptor`. All examples have been updated to use this new style and interop has a custom `tower::Service` middleware to echo the headers. There is also a new `interceptor` example that shows basic usage. BREAKING CHANGE: removed `interceptor_fn` and `intercep_headers_fn` from `transport` in favor of using `tonic::Interceptor`.
This commit is contained in:
@@ -3,8 +3,8 @@ pub mod api {
|
||||
}
|
||||
|
||||
use api::{publisher_client::PublisherClient, ListTopicsRequest};
|
||||
use http::header::HeaderValue;
|
||||
use tonic::{
|
||||
metadata::MetadataValue,
|
||||
transport::{Certificate, Channel, ClientTlsConfig},
|
||||
Request,
|
||||
};
|
||||
@@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.ok_or("Expected a project name as the first argument.".to_string())?;
|
||||
|
||||
let bearer_token = format!("Bearer {}", token);
|
||||
let header_value = HeaderValue::from_str(&bearer_token)?;
|
||||
let header_value = MetadataValue::from_str(&bearer_token)?;
|
||||
|
||||
let certs = tokio::fs::read("examples/data/gcp/roots.pem").await?;
|
||||
|
||||
@@ -32,14 +32,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.domain_name("pubsub.googleapis.com");
|
||||
|
||||
let channel = Channel::from_static(ENDPOINT)
|
||||
.intercept_headers(move |headers| {
|
||||
headers.insert("authorization", header_value.clone());
|
||||
})
|
||||
.tls_config(tls_config)
|
||||
.connect()
|
||||
.await?;
|
||||
|
||||
let mut service = PublisherClient::new(channel);
|
||||
let mut service = PublisherClient::with_interceptor(channel, move |mut req: Request<()>| {
|
||||
req.metadata_mut()
|
||||
.insert("authorization", header_value.clone());
|
||||
Ok(req)
|
||||
});
|
||||
|
||||
let response = service
|
||||
.list_topics(Request::new(ListTopicsRequest {
|
||||
|
||||
Reference in New Issue
Block a user