#[cfg(not(target_arch = "wasm32"))]
mod native_local;
#[cfg(not(target_arch = "wasm32"))]
mod native_send;
#[cfg(all(test, not(target_arch = "wasm32"), feature = "tokio"))]
mod tests;
#[cfg(feature = "wasi-p2")]
mod wasi;
#[cfg(feature = "wasm")]
mod wasm;
use std::future::Future;
use std::time::Duration;
use bytes::Bytes;
use http::header::{HeaderMap, HeaderName, HeaderValue};
use http::{Method, StatusCode};
use crate::error::{Error, SendError};
pub trait HttpClient: Clone + 'static {
type RequestBuilder: RequestBuilderExt;
fn request(&self, method: Method, uri: &str) -> Result<Self::RequestBuilder, Error>;
fn get(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::GET, uri)
}
fn head(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::HEAD, uri)
}
fn post(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::POST, uri)
}
fn put(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::PUT, uri)
}
fn patch(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::PATCH, uri)
}
fn delete(&self, uri: &str) -> Result<Self::RequestBuilder, Error> {
self.request(Method::DELETE, uri)
}
}
pub trait RequestBuilderExt: Sized {
type Response: ResponseExt;
fn header(self, name: HeaderName, value: HeaderValue) -> Self;
fn headers(self, headers: HeaderMap) -> Self;
fn bearer_auth(self, token: &str) -> Self;
fn body(self, body: impl Into<Bytes>) -> Self;
fn timeout(self, duration: Duration) -> Self;
fn send(self) -> impl Future<Output = Result<Self::Response, SendError>>;
}
pub trait ResponseExt {
type ByteStream: ByteStreamExt;
fn status(&self) -> StatusCode;
fn headers(&self) -> &HeaderMap;
fn bytes(self) -> impl Future<Output = Result<Bytes, Error>>;
fn text(self) -> impl Future<Output = Result<String, Error>>;
#[cfg(feature = "json")]
fn json<T: serde::de::DeserializeOwned>(self) -> impl Future<Output = Result<T, Error>>;
fn into_bytes_stream(self) -> Self::ByteStream;
}
pub trait ByteStreamExt {
fn next(&mut self) -> impl Future<Output = Option<Result<Bytes, Error>>>;
}
#[cfg(not(target_arch = "wasm32"))]
pub use native_local::OwnedRequestBuilderLocal;
#[cfg(not(target_arch = "wasm32"))]
pub use native_send::OwnedRequestBuilderSend;
#[cfg(feature = "wasi-p2")]
pub use wasi::OwnedWasiRequestBuilder;
#[cfg(feature = "wasm")]
pub use wasm::OwnedWasmRequestBuilder;