use std::borrow::Cow;
use hyper::Body;
use hyper::Headers;
use hyper::Method;
use hyper::Request;
use hyper::Uri;
use hyper::header::Raw;
use simplist::HttpContent;
use simplist::HttpMethod;
use simplist::Url;
#[derive(Debug)]
pub struct HttpRequest {
url: Url,
underlying: Request<Body>,
}
impl HttpRequest {
pub fn new() -> HttpRequest {
let url = Url::default();
let underlying = Request::new(Method::Get, Uri::default());
HttpRequest { url, underlying }
}
pub fn with(url: Url, method: HttpMethod) -> HttpRequest {
HttpRequest::new()
.with_url (url)
.with_method(method)
}
pub fn url(&self) -> &Url {
&self.url
}
pub fn method(&self) -> HttpMethod {
let copy = self.underlying.method().clone();
Into::into(copy)
}
pub fn path(&self) -> &str {
self.underlying.path()
}
pub fn query(&self) -> Option<&str> {
self.underlying.query()
}
pub fn headers(&self) -> &Headers {
self.underlying.headers()
}
pub fn headers_mut(&mut self) -> &mut Headers {
self.underlying.headers_mut()
}
pub fn with_method(mut self, method: HttpMethod) -> HttpRequest {
self.underlying.set_method(method.into());
self
}
pub fn with_url(mut self, url: Url) -> HttpRequest {
self.url = url;
self
}
pub fn with_body<TBody>(mut self, body: Option<TBody>) -> HttpRequest
where TBody: Into<HttpContent> {
let content: HttpContent = body.into();
let content: Body = content.into();
self.underlying.set_body(content);
self
}
pub fn with_header<TKey, TValue>(mut self, key: TKey, value: TValue) -> HttpRequest
where TKey: Into<Cow<'static, str>>,
TValue: Into<Raw>, {
{
let headers = self.underlying.headers_mut();
let key = key.into();
headers.remove_raw(&key);
headers.append_raw(key, value);
}
self
}
}
impl Into<Request<Body>> for HttpRequest {
fn into(self) -> Request<Body> {
let mut request = self.underlying;
request.set_uri(self.url.into());
request
}
}