use super::{Form, Method};
use crate::{
error::Error,
routing::{Path, Route},
};
use hyper::header::{HeaderMap, HeaderName, HeaderValue};
use serde::Serialize;
use std::borrow::Cow;
pub struct RequestBuilder(Request);
impl RequestBuilder {
#[must_use = "request has not been fully built"]
pub fn new(route: Route) -> Self {
Self(Request::from_route(route))
}
#[allow(clippy::missing_const_for_fn)]
#[must_use = "request information is not useful on its own and must be acted on"]
pub fn build(self) -> Request {
self.0
}
#[must_use = "request has not been fully built"]
pub fn body(mut self, body: Vec<u8>) -> Self {
self.0.body.replace(body);
self
}
#[must_use = "request has not been fully built"]
pub fn form(mut self, form: Form) -> Self {
self.0.form.replace(form);
self
}
#[must_use = "request has not been fully built"]
pub fn headers(mut self, iter: impl Iterator<Item = (HeaderName, HeaderValue)>) -> Self {
self.0.headers.replace(iter.collect());
self
}
#[must_use = "request has not been fully built"]
pub fn json(self, to: &impl Serialize) -> Result<Self, Error> {
let bytes = crate::json::to_vec(to).map_err(Error::json)?;
Ok(self.body(bytes))
}
pub const fn use_authorization_token(mut self, use_authorization_token: bool) -> Self {
self.0.use_authorization_token = use_authorization_token;
self
}
}
#[derive(Debug)]
pub struct Request {
pub body: Option<Vec<u8>>,
pub form: Option<Form>,
pub headers: Option<HeaderMap<HeaderValue>>,
pub method: Method,
pub path: Path,
pub path_str: Cow<'static, str>,
pub(crate) use_authorization_token: bool,
}
impl Request {
#[deprecated(since = "0.4.0", note = "Use `Request::builder` instead")]
pub fn new(
body: Option<Vec<u8>>,
headers: Option<HeaderMap<HeaderValue>>,
route: Route,
) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body,
form: None,
headers,
method,
path,
path_str,
use_authorization_token: true,
}
}
pub fn builder(route: Route) -> RequestBuilder {
RequestBuilder::new(route)
}
pub fn from_route(route: Route) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: None,
form: None,
headers: None,
method,
path,
path_str,
use_authorization_token: true,
}
}
pub const fn use_authorization_token(&self) -> bool {
self.use_authorization_token
}
}
impl From<Route> for Request {
fn from(route: Route) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: None,
form: None,
headers: None,
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(Vec<u8>, Route)> for Request {
fn from((body, route): (Vec<u8>, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: Some(body),
form: None,
headers: None,
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(Form, Route)> for Request {
fn from((form, route): (Form, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: None,
form: Some(form),
headers: None,
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(Vec<u8>, Form, Route)> for Request {
fn from((body, form, route): (Vec<u8>, Form, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: Some(body),
form: Some(form),
headers: None,
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(HeaderMap<HeaderValue>, Route)> for Request {
fn from((headers, route): (HeaderMap<HeaderValue>, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: None,
form: None,
headers: Some(headers),
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(Vec<u8>, HeaderMap<HeaderValue>, Route)> for Request {
fn from((body, headers, route): (Vec<u8>, HeaderMap<HeaderValue>, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: Some(body),
form: None,
headers: Some(headers),
method,
path,
path_str,
use_authorization_token: true,
}
}
}
impl From<(Form, HeaderMap<HeaderValue>, Route)> for Request {
fn from((form, headers, route): (Form, HeaderMap<HeaderValue>, Route)) -> Self {
let (method, path, path_str) = route.into_parts();
Self {
body: None,
form: Some(form),
headers: Some(headers),
method,
path,
path_str,
use_authorization_token: true,
}
}
}