ginmi 0.1.3

An asynchrounous gNMI client to interact with and manage network devices.
Documentation
use tonic::metadata::AsciiMetadataValue;
use tonic::service::Interceptor;
use tonic::{Request, Status};

#[derive(Debug, Clone)]
pub struct AuthInterceptor {
    username: AsciiMetadataValue,
    password: AsciiMetadataValue,
}

impl AuthInterceptor {
    pub fn new(username: Option<AsciiMetadataValue>, password: Option<AsciiMetadataValue>) -> Self {
        Self {
            username: username.unwrap_or(AsciiMetadataValue::from_static("")),
            password: password.unwrap_or(AsciiMetadataValue::from_static("")),
        }
    }
}

impl Interceptor for AuthInterceptor {
    fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
        request
            .metadata_mut()
            .insert("username", self.username.clone());
        request
            .metadata_mut()
            .insert("password", self.password.clone());
        Ok(request)
    }
}