use bytes::Bytes;
use http::Method;
use crate::body::{NativeResponseBody, RequestBody};
use crate::client::Client;
use crate::error::Result;
use crate::headers::Headers;
use crate::redirect::RedirectPolicy;
use crate::timeout::Timeout;
use crate::transport_hints::{NativeRequestOptions, TransportHints};
pub struct NativeRequestBuilder {
client: Option<Client>,
method: Method,
uri: http::Uri,
headers: Headers,
body: RequestBody,
version: http::Version,
timeout: Option<Timeout>,
transport_hints: TransportHints,
retry_disabled: bool,
redirect: Option<RedirectPolicy>,
error: Option<crate::Error>,
}
impl std::fmt::Debug for NativeRequestBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let scheme = self.uri.scheme_str().unwrap_or("");
let authority = self
.uri
.authority()
.map_or("", http::uri::Authority::as_str);
let path = self.uri.path();
f.debug_struct("NativeRequestBuilder")
.field("method", &self.method)
.field("uri", &format!("{scheme}://{authority}{path}"))
.field("headers", &self.headers)
.field("body", &self.body)
.field("version", &self.version)
.field("timeout", &self.timeout)
.field("transport_hints", &self.transport_hints)
.field("retry_disabled", &self.retry_disabled)
.field("redirect", &self.redirect)
.finish_non_exhaustive()
}
}
impl NativeRequestBuilder {
pub(crate) fn new(client: Client, method: Method, uri: http::Uri) -> Self {
Self {
client: Some(client),
method,
uri,
headers: Headers::new(),
body: RequestBody::default(),
version: http::Version::HTTP_11,
timeout: None,
transport_hints: TransportHints::default(),
retry_disabled: false,
redirect: None,
error: None,
}
}
#[must_use]
pub fn header(mut self, name: &str, value: &str) -> Self {
if let Err(error) = self.headers.insert(name, value) {
if self.error.is_none() {
self.error = Some(error);
}
}
self
}
#[must_use]
pub fn headers(mut self, headers: Headers) -> Self {
self.headers = headers;
self
}
#[must_use]
pub fn body(mut self, body: impl Into<RequestBody>) -> Self {
self.body = body.into();
self
}
#[must_use]
pub fn bytes(self, data: impl Into<Bytes>) -> Self {
self.body(RequestBody::Bytes(data.into()))
}
#[must_use]
pub fn version(mut self, version: http::Version) -> Self {
self.version = version;
self
}
#[must_use]
pub fn timeout(mut self, timeout: Timeout) -> Self {
self.timeout = Some(timeout);
self
}
#[must_use]
pub fn transport_hints(mut self, hints: TransportHints) -> Self {
self.transport_hints = hints;
self
}
#[must_use]
pub fn without_retry(mut self) -> Self {
self.retry_disabled = true;
self
}
#[must_use]
pub fn redirect_policy(mut self, policy: RedirectPolicy) -> Self {
self.redirect = Some(policy);
self
}
pub fn build(self) -> Result<http::Request<RequestBody>> {
if let Some(error) = self.error {
return Err(error);
}
let mut request = http::Request::new(self.body);
*request.uri_mut() = self.uri;
*request.method_mut() = self.method;
*request.version_mut() = self.version;
*request.headers_mut() = self.headers.into_inner();
Ok(request)
}
pub async fn send(self) -> Result<http::Response<NativeResponseBody>> {
let client = self.client.clone().ok_or_else(|| {
crate::Error::RequestBuild("no client associated with request builder".into())
})?;
let timeout = self.timeout;
let transport_hints = self.transport_hints.clone();
let request = self.build()?;
let options = NativeRequestOptions {
timeout,
transport_hints,
};
client.execute_http_body(request, options).await
}
}