raspa/src/request.rs

98 lines
2.5 KiB
Rust

use http::Method;
use url::ParseError;
use crate::into_url::IntoUrl;
use crate::response;
#[derive(Clone)]
pub enum ParameterType {
Number(isize),
Decimal(f64),
Boolean(bool),
Text(String),
}
pub trait RequestBase {
fn new<T: IntoUrl>(url: T) -> Result<Self, ParseError>
where
Self: Sized;
fn launch(self) -> response::Response;
}
#[derive(Debug)]
pub struct Request {
pub url: url::Url,
method: Method,
params: Vec<(String, String)>,
}
impl RequestBase for Request {
fn new<T: IntoUrl>(url: T) -> Result<Request, ParseError> {
url.into_url().map(|url_parsed| Request {
url: url_parsed,
method: Method::GET,
params: Vec::new(),
})
}
fn launch(mut self) -> response::Response {
let client = reqwest::blocking::Client::new();
for (key, value) in &self.params {
self.url.query_pairs_mut().append_pair(key, value);
}
let resp = (match self.method {
Method::GET => client.get(self.url.as_str()),
Method::POST => client.post(self.url.as_str()),
Method::PUT => client.put(self.url.as_str()),
Method::PATCH => client.patch(self.url.as_str()),
Method::DELETE => client.delete(self.url.as_str()),
_ => unimplemented!(),
})
.send()
.unwrap();
let status = resp.status();
let text = resp.text().unwrap();
let url = self.url;
response::Response {
text: text,
status_code: status,
url: url,
}
}
}
impl std::string::ToString for ParameterType {
fn to_string(&self) -> String {
match self {
ParameterType::Number(n) => n.to_string(),
ParameterType::Decimal(n) => n.to_string(),
ParameterType::Boolean(n) => n.to_string(),
ParameterType::Text(t) => t.clone(),
}
}
}
impl Request {
pub fn method(mut self, method: Method) -> Self {
self.method = method;
self
}
pub fn add_params<S: AsRef<str>, T: ToString>(mut self, params: Vec<(S, T)>) -> Self {
self.params = params
.iter()
.map(|item| (item.0.as_ref().to_string(), item.1.to_string()))
.collect();
self
}
pub fn add_param<S: AsRef<str>, T: ToString>(&mut self, key: S, value: T) {
self.params
.push((key.as_ref().to_string(), value.to_string()));
}
}