use actix_web::{web, HttpRequest, HttpResponse}; use crate::controllers::routes::RequestResult; use crate::data::http_error::HttpError; use std::convert::TryFrom; use std::error::Error; use serde::Serialize; use crate::data::error::{ResultBoxError}; use std::collections::HashMap; /// Http request handler /// /// @author Pierre Hubert /// Single request body value pub struct RequestValue { pub string: Option } impl RequestValue { /// Build a string value pub fn string(s: String) -> RequestValue { RequestValue { string: Some(s) } } } #[derive(Serialize)] struct SuccessMessage { success: String } pub struct HttpRequestHandler { request: web::HttpRequest, body: HashMap, response: Option, } impl HttpRequestHandler { /// Construct a new request handler pub fn new(req: HttpRequest, body: HashMap) -> HttpRequestHandler { HttpRequestHandler { request: req, body, response: None, } } /// Check if a response has been set for this request pub fn has_response(&self) -> bool { self.response.is_some() } /// Get the response status code, eg. 200 or 404 pub fn response_status_code(&self) -> u16 { self.response.as_ref().unwrap().status().as_u16() } /// Take the response from this struct pub fn response(self) -> HttpResponse { self.response.unwrap() } /// Success message pub fn success(&mut self, message: &str) -> RequestResult { self.response = Some(HttpResponse::Ok().json(SuccessMessage { success: message.to_string() })); Ok(()) } /// Internal error message pub fn internal_error(&mut self, error: Box) -> RequestResult { self.response = Some(HttpResponse::InternalServerError().json( HttpError::internal_error("Internal server error."))); Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap()) } /// Bad request pub fn bad_request(&mut self, message: String) -> RequestResult { self.response = Some(HttpResponse::BadRequest().json( HttpError::bad_request(&message))); Err(Box::try_from(actix_web::error::ErrorBadRequest(message)).unwrap()) } /// Get the path of the request pub fn request_path(&self) -> String { self.request.path().to_string() } /// Check login tokens pub fn check_client_token(&mut self) -> Result<(), Box> { println!("me = {}", self.post_string("me")?); Ok(()) } /// Check if a POST parameter was present in the request or not pub fn has_post_parameter(&self, name: &str) -> bool { self.body.contains_key(name) } /// Get a post parameter pub fn post_parameter(&mut self, name: &str) -> ResultBoxError<&RequestValue> { if !self.has_post_parameter(name) { self.bad_request(format!("POST parameter '{}' not found in request!", name))?; } Ok(self.body.get(name).unwrap()) } /// Get a post string pub fn post_string(&mut self, name: &str) -> Result> { let param = self.post_parameter(name)?; match ¶m.string { None => Err(Box::new(actix_web::error::ErrorBadRequest(format!("{} is not a string!", name)))), Some(s) => Ok(s.to_string()) } } }