use crate::Content;
use anyhow::Result;
use http::Response;
use serde::{Deserialize, Serialize};
use serde_json;
use std::collections::HashMap;
use std::io::Stdin;
#[derive(Serialize, Deserialize)]
pub struct Input {
url: String,
method: String,
headers: HashMap<String, String>,
body: String,
kv: HashMap<String, String>,
#[serde(default)]
params: HashMap<String, String>,
}
impl Input {
pub fn new(reader: Stdin) -> Result<Self> {
serde_json::from_reader::<Stdin, Input>(reader).map_err(anyhow::Error::new)
}
pub fn to_http_request(&self) -> http::Request<String> {
let mut request = http::request::Builder::new()
.uri(&self.url)
.method(self.method.as_str());
for (key, value) in self.headers.iter() {
request = request.header(key, value);
}
request.body(self.body.to_string()).unwrap()
}
pub fn cache_data(&self) -> HashMap<String, String> {
self.kv.clone()
}
pub fn params(&self) -> &HashMap<String, String> {
&self.params
}
}
#[derive(Serialize, Deserialize)]
pub struct Output {
data: String,
headers: HashMap<String, String>,
status: u16,
kv: HashMap<String, String>,
base64: bool,
}
impl Output {
pub fn new(
data: &str,
status: u16,
headers: Option<HashMap<String, String>>,
kv: Option<HashMap<String, String>>,
base64: bool,
) -> Self {
Self {
data: data.to_string(),
status,
headers: headers.unwrap_or_default(),
kv: kv.unwrap_or_default(),
base64,
}
}
pub fn from_response(response: Response<Content>, cache: HashMap<String, String>) -> Self {
let mut headers = HashMap::new();
for (key, value) in response.headers().iter() {
headers.insert(
String::from(key.as_str()),
String::from(value.to_str().unwrap()),
);
}
let status = response.status().as_u16();
let content = response.into_body();
let body;
let base64;
match content {
Content::Base64(data) => {
body = data;
base64 = true;
}
Content::Text(data) => {
body = data;
base64 = false;
}
}
Self::new(&body, status, Some(headers), Some(cache), base64)
}
pub fn to_json(&self) -> Result<String> {
serde_json::to_string(&self).map_err(anyhow::Error::new)
}
}