use crate::BASE_URL;
#[derive(Clone)]
pub struct ClientConfig {
pub(crate) base_url: String,
pub(crate) token: Option<String>,
pub(crate) username: Option<String>,
pub(crate) password: Option<String>,
pub(crate) http_client: Option<reqwest::Client>,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
base_url: BASE_URL.to_string(),
token: None,
username: None,
password: None,
http_client: None,
}
}
}
impl ClientConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
pub fn with_token(mut self, token: impl Into<String>) -> Self {
self.token = Some(token.into());
self
}
pub fn with_basic_auth(
mut self,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
self.username = Some(username.into());
self.password = Some(password.into());
self
}
pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
self.http_client = Some(client);
self
}
pub fn validate(&self) -> Result<(), crate::Error> {
match (&self.username, &self.password, &self.token) {
(None, None, None) => Err(crate::Error::InvalidConfig(
"missing auth credentials".to_string(),
)),
(Some(_), None, None) | (Some(_), None, Some(_)) => Err(crate::Error::InvalidConfig(
"password is required when username is set".to_string(),
)),
(None, Some(_), None) | (None, Some(_), Some(_)) => Err(crate::Error::InvalidConfig(
"username is required when password is set".to_string(),
)),
_ => Ok(()),
}
}
}