use std::fmt;
use std::sync::Arc;
use crate::api::ApiRequest;
use crate::config::{Config, common_builder_methods, request_span};
use crate::credentials::Credentials;
use crate::error::{Error, TransportError};
use crate::operations::{operation_table, resource_handles};
use crate::transport::{HttpRequest, HttpResponse};
#[cfg(not(target_arch = "wasm32"))]
pub trait Transport: Send + Sync {
fn send(&self, request: HttpRequest<'_>) -> Result<HttpResponse, TransportError>;
}
#[cfg(target_arch = "wasm32")]
pub trait Transport {
fn send(&self, request: HttpRequest<'_>) -> Result<HttpResponse, TransportError>;
}
#[cfg(feature = "ureq")]
pub struct Client<T = UreqTransport> {
inner: Arc<Inner<T>>,
}
#[cfg(not(feature = "ureq"))]
pub struct Client<T> {
inner: Arc<Inner<T>>,
}
struct Inner<T> {
transport: T,
config: Config,
}
#[cfg(feature = "ureq")]
impl Client {
pub fn new(credentials: Credentials) -> Self {
Self::builder(credentials).build()
}
pub fn builder(credentials: Credentials) -> ClientBuilder {
ClientBuilder::new(credentials, UreqTransport::default())
}
}
impl<T: Transport> Client<T> {
pub fn with_transport(credentials: Credentials, transport: T) -> Self {
ClientBuilder::new(credentials, transport).build()
}
pub fn transport(&self) -> &T {
&self.inner.transport
}
pub fn execute<R: ApiRequest>(&self, request: &R) -> Result<R::Response, Error> {
let _span = request_span::<R>().entered();
let config = &self.inner.config;
let body = config.encode(request)?;
let response = self.inner.transport.send(config.http_request(body))?;
config.decode::<R>(response.status, &response.body)
}
}
impl<T> Clone for Client<T> {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl<T> fmt::Debug for Client<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("config", &self.inner.config)
.finish_non_exhaustive()
}
}
#[cfg(feature = "ureq")]
#[must_use]
pub struct ClientBuilder<T = UreqTransport> {
config: Config,
transport: T,
}
#[cfg(not(feature = "ureq"))]
#[must_use]
pub struct ClientBuilder<T> {
config: Config,
transport: T,
}
impl<T> ClientBuilder<T> {
pub fn new(credentials: Credentials, transport: T) -> Self {
Self {
config: Config::new(credentials),
transport,
}
}
common_builder_methods!();
}
impl<T: Transport> ClientBuilder<T> {
pub fn build(self) -> Client<T> {
Client {
inner: Arc::new(Inner {
transport: self.transport,
config: self.config,
}),
}
}
}
impl<T> fmt::Debug for ClientBuilder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientBuilder")
.field("config", &self.config)
.finish_non_exhaustive()
}
}
#[cfg(feature = "ureq")]
#[derive(Debug, Clone)]
pub struct UreqTransport {
agent: ureq::Agent,
}
#[cfg(feature = "ureq")]
const MAX_RESPONSE_BYTES: u64 = 100 * 1024 * 1024;
#[cfg(feature = "ureq")]
impl Default for UreqTransport {
fn default() -> Self {
Self {
agent: ureq::Agent::new_with_defaults(),
}
}
}
#[cfg(feature = "ureq")]
impl From<ureq::Agent> for UreqTransport {
fn from(agent: ureq::Agent) -> Self {
Self { agent }
}
}
#[cfg(feature = "ureq")]
impl Transport for UreqTransport {
fn send(&self, request: HttpRequest<'_>) -> Result<HttpResponse, TransportError> {
let error = |err: ureq::Error| {
if matches!(err, ureq::Error::Timeout(_)) {
TransportError::timeout(err)
} else {
TransportError::new(err)
}
};
let mut response = self
.agent
.post(request.url)
.header("Content-Type", request.content_type)
.config()
.http_status_as_error(false)
.timeout_global(Some(request.timeout))
.user_agent(request.user_agent)
.build()
.send(request.body)
.map_err(error)?;
let status = response.status().as_u16();
let body = response
.body_mut()
.with_config()
.limit(MAX_RESPONSE_BYTES)
.read_to_vec()
.map_err(error)?;
Ok(HttpResponse::new(status, body))
}
}
operation_table!(resource_handles, Client, Transport, blocking);