#![allow(clippy::multiple_crate_versions)]
#[cfg(all(
feature = "native-tls",
feature = "rustls",
not(feature = "default-backend")
))]
compile_error!(
"Features `native-tls` and `rustls` are mutually exclusive. \
Please enable only one TLS backend, or use `default-backend` for automatic platform selection."
);
#[cfg(all(
not(target_arch = "wasm32"),
any(feature = "native-tls", feature = "rustls"),
not(any(feature = "hyper-backend", feature = "ws")),
any(
all(target_vendor = "apple", feature = "apple-backend"),
feature = "curl-backend"
)
))]
compile_error!(
"The `native-tls` and `rustls` features apply to `hyper-backend` and native websocket support. \
Your current HTTP backend (apple-backend or curl-backend) has its own TLS implementation, so \
these TLS features are only valid here when websocket support is also enabled."
);
pub mod backend;
use backend::DefaultBackend;
pub use cache::Cache;
pub use client::Client;
pub use http_kit::*;
pub use oauth2::OAuth2ClientCredentials;
pub mod auth;
pub mod cache;
pub mod cookie;
pub mod error;
pub mod oauth2;
pub mod timeout;
mod client;
pub mod redirect;
pub mod retry;
pub use error::Error;
mod ext;
pub mod multipart;
#[cfg(all(not(target_arch = "wasm32"), feature = "proxy"))]
pub mod proxy;
#[cfg(feature = "ws")]
pub mod websocket;
pub use ext::ResponseExt;
#[cfg(all(not(target_arch = "wasm32"), feature = "proxy"))]
pub use proxy::{Proxy, ProxyBuilder};
pub use timeout::Timeout;
#[derive(Debug)]
pub struct DefaultClient {
inner: redirect::FollowRedirect<DefaultBackend>,
}
impl DefaultClient {
#[must_use]
pub fn new() -> Self {
Self {
inner: DefaultBackend::default().follow_redirect(),
}
}
#[must_use]
pub fn disable_redirect(self) -> DefaultBackend {
self.inner.disable_redirect()
}
#[must_use]
pub fn raw() -> DefaultBackend {
DefaultBackend::default()
}
}
impl Default for DefaultClient {
fn default() -> Self {
Self::new()
}
}
impl Endpoint for DefaultClient {
type Error = Error;
async fn respond(&mut self, request: &mut Request) -> Result<Response, Self::Error> {
self.inner.respond(request).await.map_err(Into::into)
}
}
impl Client for DefaultClient {}
#[must_use]
pub fn client() -> DefaultClient {
DefaultClient::new()
}
#[must_use]
pub fn raw_client() -> DefaultBackend {
DefaultClient::raw()
}
#[cfg(all(
not(target_arch = "wasm32"),
feature = "curl-backend",
not(all(target_vendor = "apple", feature = "apple-backend")),
not(feature = "hyper-backend")
))]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn client_with_proxy(proxy: Proxy) -> DefaultClient {
DefaultClient {
inner: DefaultBackend::with_proxy(proxy).follow_redirect(),
}
}
#[cfg(all(
not(target_arch = "wasm32"),
feature = "curl-backend",
not(all(target_vendor = "apple", feature = "apple-backend")),
not(feature = "hyper-backend")
))]
impl DefaultClient {
#[must_use]
pub fn proxy(self, proxy: Proxy) -> Self {
client_with_proxy(proxy)
}
}
pub async fn get<U>(uri: U) -> Result<Response, Error>
where
U: TryInto<Uri>,
U::Error: core::fmt::Display,
{
let mut client = client();
client.method(Method::GET, uri)?.await
}
pub async fn post<U>(uri: U) -> Result<Response, Error>
where
U: TryInto<Uri>,
U::Error: core::fmt::Display,
{
let mut client = client();
client.method(Method::POST, uri)?.await
}
pub async fn put<U>(uri: U) -> Result<Response, Error>
where
U: TryInto<Uri>,
U::Error: core::fmt::Display,
{
let mut client = client();
client.method(Method::PUT, uri)?.await
}
pub async fn delete<U>(uri: U) -> Result<Response, Error>
where
U: TryInto<Uri>,
U::Error: core::fmt::Display,
{
let mut client = client();
client.method(Method::DELETE, uri)?.await
}