tonic
tonic copied to clipboard
feat(transport): Add disable user-agent
fix https://github.com/hyperium/tonic/issues/970
disable default feature can without any user-agent
in your Cargo.toml
tonic = { version = "0.13.1", default-features = false, features = [
"channel",
"codegen",
"prost",
] }
if want set, use with_interceptor like
use bytes::Bytes;
use tonic::{
service::{Interceptor, interceptor::InterceptedService},
transport::{Channel, Endpoint, Error},
};
use crate::api_client::ApiClient;
pub async fn conn<F>(
addr: impl Into<Bytes>,
interceptor: F,
) -> Result<ApiClient<InterceptedService<Channel, F>>, Error>
where
F: Interceptor,
{
let endpoint = Endpoint::from_shared(addr.into())?;
let channel = endpoint.connect().await?;
let client = ApiClient::with_interceptor(channel, interceptor);
Ok(client)
}
let mut client = conn("12.3.2.3:1111", |mut req: tonic::Request<()>| {
req.metadata_mut().insert("user-agent", "xxxx".try_into().unwrap());
Ok(req)
});