use std::collections::HashMap;
use std::time::Duration;
#[derive(Debug, Clone, Default)]
pub struct APIContextOptions {
pub(crate) base_url: Option<String>,
pub(crate) extra_http_headers: HashMap<String, String>,
pub(crate) http_credentials: Option<HttpCredentials>,
pub(crate) ignore_https_errors: bool,
pub(crate) proxy: Option<ProxyConfig>,
pub(crate) timeout: Option<Duration>,
pub(crate) user_agent: Option<String>,
}
#[derive(Debug, Clone)]
pub struct HttpCredentials {
pub username: String,
pub password: String,
pub origin: Option<String>,
pub send: Option<CredentialSend>,
}
impl HttpCredentials {
pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
Self {
username: username.into(),
password: password.into(),
origin: None,
send: None,
}
}
#[must_use]
pub fn origin(mut self, origin: impl Into<String>) -> Self {
self.origin = Some(origin.into());
self
}
#[must_use]
pub fn send(mut self, send: CredentialSend) -> Self {
self.send = Some(send);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialSend {
Unauthorized,
Always,
}
#[derive(Debug, Clone)]
pub struct ProxyConfig {
pub server: String,
pub username: Option<String>,
pub password: Option<String>,
pub bypass: Option<String>,
}
impl ProxyConfig {
pub fn new(server: impl Into<String>) -> Self {
Self {
server: server.into(),
username: None,
password: None,
bypass: None,
}
}
#[must_use]
pub fn credentials(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
self.username = Some(username.into());
self.password = Some(password.into());
self
}
#[must_use]
pub fn bypass(mut self, bypass: impl Into<String>) -> Self {
self.bypass = Some(bypass.into());
self
}
}
impl APIContextOptions {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn base_url(mut self, url: impl Into<String>) -> Self {
self.base_url = Some(url.into());
self
}
#[must_use]
pub fn extra_http_headers(
mut self,
headers: impl IntoIterator<Item = (String, String)>,
) -> Self {
self.extra_http_headers = headers.into_iter().collect();
self
}
#[must_use]
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.extra_http_headers.insert(name.into(), value.into());
self
}
#[must_use]
pub fn http_credentials(mut self, credentials: HttpCredentials) -> Self {
self.http_credentials = Some(credentials);
self
}
#[must_use]
pub fn ignore_https_errors(mut self, ignore: bool) -> Self {
self.ignore_https_errors = ignore;
self
}
#[must_use]
pub fn proxy(mut self, proxy: ProxyConfig) -> Self {
self.proxy = Some(proxy);
self
}
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
#[must_use]
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = Some(user_agent.into());
self
}
}
#[cfg(test)]
mod tests;