use async_trait::async_trait;
use reqwest::{Client, RequestBuilder, Url};
use static_assertions::assert_impl_all;
use super::{AuthType, EndpointFilters, Error, ErrorKind};
#[derive(Clone, Debug)]
pub struct BasicAuth {
endpoint: Url,
username: String,
password: String,
}
assert_impl_all!(BasicAuth: Send, Sync);
impl BasicAuth {
pub fn new<U, S1, S2>(endpoint: U, username: S1, password: S2) -> Result<BasicAuth, Error>
where
U: AsRef<str>,
S1: Into<String>,
S2: Into<String>,
{
let endpoint = Url::parse(endpoint.as_ref())
.map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?;
Ok(BasicAuth {
endpoint,
username: username.into(),
password: password.into(),
})
}
}
#[async_trait]
impl AuthType for BasicAuth {
async fn authenticate(
&self,
_client: &Client,
request: RequestBuilder,
) -> Result<RequestBuilder, Error> {
Ok(request.basic_auth(&self.username, Some(&self.password)))
}
async fn get_endpoint(
&self,
_client: &Client,
_service_type: &str,
_filters: &EndpointFilters,
) -> Result<Url, Error> {
Ok(self.endpoint.clone())
}
async fn refresh(&self, _client: &Client) -> Result<(), Error> {
Ok(())
}
}