use bytes::Bytes;
use http::{HeaderMap, Method, header::InvalidHeaderValue};
use url::Url;
#[derive(Debug, Clone)]
pub struct Request {
pub method: Method,
pub url: Url,
pub headers: HeaderMap,
pub body: Option<Bytes>,
}
#[cfg(feature = "reqwest")]
impl From<reqwest::Request> for Request {
fn from(r: reqwest::Request) -> Self {
Self {
method: r.method().clone(),
url: r.url().clone(),
headers: r.headers().clone(),
body: r
.body()
.and_then(|b| b.as_bytes().map(Bytes::copy_from_slice)),
}
}
}
impl Request {
pub fn get(url: Url) -> Self {
Self::new(Method::GET, url)
}
pub fn post(url: Url) -> Self {
Self::new(Method::POST, url)
}
pub fn new(method: Method, url: Url) -> Self {
Self {
method,
url,
headers: HeaderMap::new(),
body: None,
}
}
pub fn header(mut self, name: impl http::header::IntoHeaderName, value: &str) -> Self {
let value = value
.parse()
.expect("header value contains invalid characters");
self.headers.insert(name, value);
self
}
pub fn try_header(
mut self,
name: impl http::header::IntoHeaderName,
value: &str,
) -> Result<Self, InvalidHeaderValue> {
let value = value.parse()?;
self.headers.insert(name, value);
Ok(self)
}
pub fn body(mut self, body: impl Into<Bytes>) -> Self {
self.body = Some(body.into());
self
}
}