use super::*;
impl Default for HttpClientBuilder {
#[inline]
fn default() -> Self {
let mut config: RequestConfig = RequestConfig::default();
config.set_keep_alive(true);
Self {
base_url: None,
default_headers: HashMap::new(),
config,
root_cert: None,
}
}
}
impl HttpClientBuilder {
#[inline]
pub fn new() -> Self {
Self::default()
}
pub fn base_url<T>(mut self, url: T) -> Self
where
T: Into<String>,
{
let Self { base_url, .. } = &mut self;
*base_url = Some(url.into());
self
}
pub fn default_header<K, V>(mut self, key: K, value: V) -> Self
where
K: AsRef<str>,
V: AsRef<str>,
{
let Self {
default_headers, ..
} = &mut self;
default_headers.insert(key.as_ref().to_ascii_lowercase(), value.as_ref().to_owned());
self
}
pub fn bearer_auth<T>(self, token: T) -> Self
where
T: AsRef<str>,
{
let value: String = format!("{BEARER_PREFIX}{}", token.as_ref());
self.default_header(HEADER_AUTHORIZATION, value)
}
pub fn basic_auth<U, P>(self, username: U, password: P) -> Self
where
U: AsRef<str>,
P: AsRef<str>,
{
let raw: String = format!("{}:{}", username.as_ref(), password.as_ref());
let encoded: String = crate::utils::base64_encode(raw.as_bytes());
let value: String = format!("{BASIC_PREFIX}{encoded}");
self.default_header(HEADER_AUTHORIZATION, value)
}
pub fn timeout(mut self, ms: u64) -> Self {
let Self { config, .. } = &mut self;
config.set_timeout(ms);
self
}
pub fn connect_timeout(mut self, ms: u64) -> Self {
let Self { config, .. } = &mut self;
config.set_connect_timeout(ms);
self
}
pub fn retry(mut self, max_retries: usize) -> Self {
let Self { config, .. } = &mut self;
config.set_max_retries(max_retries);
self
}
pub fn keep_alive(mut self, enabled: bool) -> Self {
let Self { config, .. } = &mut self;
config.set_keep_alive(enabled);
self
}
pub fn http1_1_only(mut self) -> Self {
let Self { config, .. } = &mut self;
config.set_http_version(HttpVersion::Http1_1);
self
}
pub fn http2_only(mut self) -> Self {
let Self { config, .. } = &mut self;
config.set_http_version(HttpVersion::Http2);
self
}
pub fn redirect(mut self, max_redirects: usize) -> Self {
let Self { config, .. } = &mut self;
config.set_redirect(true);
config.set_max_redirect_times(max_redirects);
self
}
pub fn no_redirect(mut self) -> Self {
let Self { config, .. } = &mut self;
config.set_redirect(false);
self
}
pub fn decode(mut self, enabled: bool) -> Self {
let Self { config, .. } = &mut self;
config.set_decode(enabled);
self
}
pub fn proxy(mut self, proxy: Proxy) -> Self {
let Self { config, .. } = &mut self;
config.set_proxy(Some(proxy));
self
}
pub fn root_cert_pem(mut self, pem_bytes: &[u8]) -> Result<Self, RequestError> {
let Self { root_cert, .. } = &mut self;
let store: &mut RootCertStore = root_cert.get_or_insert_with(|| RootCertStore {
roots: TLS_SERVER_ROOTS.to_vec(),
});
let _: usize = add_pem_certs_to_store(store, pem_bytes)?;
Ok(self)
}
pub fn build(self) -> HttpClient {
let Self {
base_url,
default_headers,
config,
root_cert,
} = self;
HttpClient {
inner: Arc::new(HttpClientInner {
base_url,
default_headers,
config,
root_cert,
}),
}
}
}
impl Default for HttpClient {
#[inline]
fn default() -> Self {
HttpClientBuilder::new().build()
}
}
impl HttpClient {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn builder() -> HttpClientBuilder {
HttpClientBuilder::new()
}
fn resolve_target(&self, path_or_url: &str) -> String {
let Self { inner } = self;
if path_or_url.contains(SCHEME_SEPARATOR) {
return path_or_url.to_owned();
}
let Some(ref base) = inner.base_url else {
return path_or_url.to_owned();
};
let base_trimmed: &str = base.trim_end_matches('/');
if path_or_url.is_empty() {
return base_trimmed.to_owned();
}
if path_or_url.starts_with('/') || path_or_url.starts_with('?') {
let mut out: String = String::with_capacity(base_trimmed.len() + path_or_url.len());
out.push_str(base_trimmed);
out.push_str(path_or_url);
return out;
}
let mut out: String = String::with_capacity(base_trimmed.len() + 1 + path_or_url.len());
out.push_str(base_trimmed);
out.push('/');
out.push_str(path_or_url);
out
}
pub fn request<T>(&self, method: Method, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
let Self { inner } = self;
let resolved_url: String = self.resolve_target(path_or_url.as_ref());
let headers: HashMap<String, String> = if inner.default_headers.is_empty() {
HashMap::new()
} else {
inner.default_headers.clone()
};
let tmp: Tmp = Tmp {
visit_url: HashSet::new(),
root_cert: inner.root_cert.clone(),
};
RequestBuilder {
request: HttpRequest {
method,
url: resolved_url,
headers,
body: Body::empty(),
config: inner.config.clone(),
tmp,
},
}
}
#[inline]
pub fn get<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Get, path_or_url)
}
#[inline]
pub fn post<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Post, path_or_url)
}
#[inline]
pub fn put<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Put, path_or_url)
}
#[inline]
pub fn delete<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Delete, path_or_url)
}
#[inline]
pub fn patch<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Patch, path_or_url)
}
#[inline]
pub fn head<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Head, path_or_url)
}
#[inline]
pub fn options<T>(&self, path_or_url: T) -> RequestBuilder
where
T: AsRef<str>,
{
self.request(Method::Options, path_or_url)
}
}