use crate::utils::del_vec::del_vec;
use crate::utils::get_vec::get_vec;
use crate::utils::set_vec::set_vec;
use serde::Serialize;
use serde_json::Error;
#[derive(Clone, Debug)]
pub struct Response {
pub(crate) header: Vec<(String, String)>,
pub body: String,
pub status: usize,
pub content_type: String,
}
impl Response {
pub async fn json(&mut self, value: impl Serialize) {
let value: Result<String, Error> = serde_json::to_string(&value);
match value {
Ok(s) => self.body = s,
Err(e) => {
self.body = "{}".to_owned();
println!("[Error] Fail to serialize json data:\n{}", e);
}
}
self.content_type = "application/json".to_owned();
}
pub async fn get_header(&self, key: &str) -> Option<String> {
get_vec(&self.header, key.to_owned()).await
}
pub async fn set_header(&mut self, key: &str, value: &str) {
self.header = set_vec(&self.header, key.to_owned(), value.to_owned()).await;
}
pub async fn del_header(&mut self, key: &str) {
self.header = del_vec(&self.header, key.to_owned()).await;
}
}