feat: Add TryFrom implementations for MetadataValue (#990)
Co-authored-by: Adam Chalmers <[email protected]>
This commit is contained in:
co-authored by
Adam Chalmers
parent
2112ecc426
commit
edc5a0d88d
@@ -9,7 +9,7 @@ use tonic::{metadata::MetadataValue, transport::Channel, Request};
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let channel = Channel::from_static("http://[::1]:50051").connect().await?;
|
||||
|
||||
let token = MetadataValue::from_str("Bearer some-auth-token")?;
|
||||
let token: MetadataValue<_> = "Bearer some-auth-token".parse()?;
|
||||
|
||||
let mut client = EchoClient::with_interceptor(channel, move |mut req: Request<()>| {
|
||||
req.metadata_mut().insert("authorization", token.clone());
|
||||
|
||||
@@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
fn check_auth(req: Request<()>) -> Result<Request<()>, Status> {
|
||||
let token = MetadataValue::from_str("Bearer some-secret-token").unwrap();
|
||||
let token: MetadataValue<_> = "Bearer some-secret-token".parse().unwrap();
|
||||
|
||||
match req.metadata().get("authorization") {
|
||||
Some(t) if token == t => Ok(req),
|
||||
|
||||
@@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.ok_or_else(|| "Expected a project name as the first argument.".to_string())?;
|
||||
|
||||
let bearer_token = format!("Bearer {}", token);
|
||||
let header_value = MetadataValue::from_str(&bearer_token)?;
|
||||
let header_value: MetadataValue<_> = bearer_token.parse()?;
|
||||
|
||||
let certs = tokio::fs::read("examples/data/gcp/roots.pem").await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user