tonic::include_proto!("tekscope");
#[cfg(feature = "client-id-header")]
use tonic::{
Request, Status, codegen::InterceptedService, metadata::MetadataValue, service::Interceptor,
transport::Channel,
};
#[doc(hidden)]
#[derive(Clone)]
#[cfg(feature = "client-id-header")]
pub struct ClientIdInterceptor {
value: MetadataValue<tonic::metadata::Ascii>,
}
#[cfg(feature = "client-id-header")]
impl ClientIdInterceptor {
fn new(client_id: &str) -> Self {
let value = MetadataValue::try_from(client_id)
.unwrap_or_else(|_| MetadataValue::from_static("invalid-client-id"));
Self { value }
}
}
#[cfg(feature = "client-id-header")]
impl Interceptor for ClientIdInterceptor {
fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
request
.metadata_mut()
.insert("x-client-id", self.value.clone());
Ok(request)
}
}
#[doc(hidden)]
#[cfg(feature = "client-id-header")]
pub type ClientChannel = InterceptedService<Channel, ClientIdInterceptor>;
#[doc(hidden)]
#[cfg(not(feature = "client-id-header"))]
pub type ClientChannel = tonic::transport::Channel;
#[inline]
#[cfg(feature = "client-id-header")]
fn with_client_id(channel: Channel, client_id: &str) -> ClientChannel {
InterceptedService::new(channel, ClientIdInterceptor::new(client_id))
}
#[inline]
#[cfg(not(feature = "client-id-header"))]
fn with_client_id(channel: tonic::transport::Channel, _client_id: &str) -> ClientChannel {
channel
}
#[inline]
#[doc(hidden)]
pub fn new_connect_client(
channel: tonic::transport::Channel,
client_id: &str,
) -> connect_client::ConnectClient<ClientChannel> {
connect_client::ConnectClient::new(with_client_id(channel, client_id))
}
#[inline]
pub(super) fn new_native_data_client(
channel: tonic::transport::Channel,
client_id: &str,
) -> native_data_client::NativeDataClient<ClientChannel> {
native_data_client::NativeDataClient::new(with_client_id(channel, client_id))
}
#[inline]
#[allow(dead_code)]
pub(super) fn new_normalized_data_client(
channel: tonic::transport::Channel,
client_id: &str,
) -> normalized_data_client::NormalizedDataClient<ClientChannel> {
normalized_data_client::NormalizedDataClient::new(with_client_id(channel, client_id))
}