use http::{header::HeaderName, HeaderMap, HeaderValue};
use reqwest::Client;
use serde::Serialize;
use crate::request::{Method, Request, RequestResult};
pub struct Context {
host: &'static str,
port: u16,
}
impl Context {
pub const fn new() -> Context {
Context {
host: "http://localhost",
port: 80,
}
}
pub const fn with_host(self, host: &'static str) -> Context {
let port = self.port;
Context { host, port }
}
pub const fn with_port(self, port: u16) -> Context {
let host = self.host;
Context { host, port }
}
pub async fn run<I>(&self, request: Request<I>) -> RequestResult
where
I: Serialize,
{
let client = reqwest::Client::new();
let create_request = match request.method {
Method::Get => Client::get,
Method::Post => Client::post,
};
let url = format!("{}:{}{}", self.host, self.port, request.url);
let headers = request
.header
.into_iter()
.map(|(k, v)| {
(
k.parse::<HeaderName>()
.expect("Header name conversion failed"),
v.parse::<HeaderValue>()
.expect("Header value conversion failed"),
)
})
.collect::<HeaderMap<HeaderValue>>();
let response = create_request(&client, url)
.headers(headers)
.json(&request.body)
.send()
.await
.expect("Request failed");
RequestResult { response }
}
}