2020-05-21 13:28:07 +00:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
|
|
|
use crate::controllers::routes::RequestResult;
|
|
|
|
use crate::data::http_error::HttpError;
|
|
|
|
use std::error::Error;
|
|
|
|
use serde::Serialize;
|
2020-05-23 08:19:15 +00:00
|
|
|
use crate::data::error::{ResultBoxError, ExecError};
|
2020-05-23 07:37:21 +00:00
|
|
|
use std::collections::HashMap;
|
2020-05-23 09:00:53 +00:00
|
|
|
use crate::helpers::api_helper;
|
2020-05-21 13:28:07 +00:00
|
|
|
|
|
|
|
/// Http request handler
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
2020-05-23 07:37:21 +00:00
|
|
|
/// Single request body value
|
|
|
|
pub struct RequestValue {
|
|
|
|
pub string: Option<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestValue {
|
|
|
|
/// Build a string value
|
|
|
|
pub fn string(s: String) -> RequestValue {
|
|
|
|
RequestValue {
|
|
|
|
string: Some(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:28:07 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct SuccessMessage {
|
|
|
|
success: String
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct HttpRequestHandler {
|
|
|
|
request: web::HttpRequest,
|
2020-05-23 07:37:21 +00:00
|
|
|
body: HashMap<String, RequestValue>,
|
|
|
|
response: Option<web::HttpResponse>,
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpRequestHandler {
|
|
|
|
/// Construct a new request handler
|
2020-05-23 07:37:21 +00:00
|
|
|
pub fn new(req: HttpRequest, body: HashMap<String, RequestValue>) -> HttpRequestHandler {
|
2020-05-21 13:28:07 +00:00
|
|
|
HttpRequestHandler {
|
|
|
|
request: req,
|
2020-05-23 07:37:21 +00:00
|
|
|
body,
|
|
|
|
response: None,
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a response has been set for this request
|
|
|
|
pub fn has_response(&self) -> bool {
|
|
|
|
self.response.is_some()
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:54:13 +00:00
|
|
|
/// Get the response status code, eg. 200 or 404
|
|
|
|
pub fn response_status_code(&self) -> u16 {
|
|
|
|
self.response.as_ref().unwrap().status().as_u16()
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:28:07 +00:00
|
|
|
/// 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<dyn Error>) -> RequestResult {
|
|
|
|
self.response = Some(HttpResponse::InternalServerError().json(
|
|
|
|
HttpError::internal_error("Internal server error.")));
|
2020-05-23 08:19:15 +00:00
|
|
|
Err(error)
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|
2020-05-23 07:37:21 +00:00
|
|
|
|
2020-05-23 08:14:21 +00:00
|
|
|
/// Bad request
|
|
|
|
pub fn bad_request(&mut self, message: String) -> RequestResult {
|
|
|
|
self.response = Some(HttpResponse::BadRequest().json(
|
|
|
|
HttpError::bad_request(&message)));
|
2020-05-23 08:19:15 +00:00
|
|
|
Err(Box::new(ExecError::new(&message)))
|
2020-05-23 08:14:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:00:53 +00:00
|
|
|
/// If result is not OK, return a bad request
|
|
|
|
pub fn ok_or_bad_request<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
|
|
|
|
match res {
|
|
|
|
Ok(e) => Ok(e),
|
|
|
|
Err(err) => {
|
|
|
|
println!("Error leading to bad request: {}", err);
|
|
|
|
self.bad_request(msg.to_string())?;
|
|
|
|
unreachable!()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:54:13 +00:00
|
|
|
/// Get the path of the request
|
|
|
|
pub fn request_path(&self) -> String {
|
|
|
|
self.request.path().to_string()
|
|
|
|
}
|
2020-05-23 07:37:21 +00:00
|
|
|
|
|
|
|
/// 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
|
2020-05-23 08:14:21 +00:00
|
|
|
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())
|
2020-05-23 07:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a post string
|
2020-05-23 08:19:15 +00:00
|
|
|
pub fn post_string(&mut self, name: &str) -> ResultBoxError<String> {
|
2020-05-23 07:37:21 +00:00
|
|
|
let param = self.post_parameter(name)?;
|
|
|
|
|
|
|
|
match ¶m.string {
|
2020-05-23 08:19:15 +00:00
|
|
|
Some(s) => Ok(s.to_string()),
|
|
|
|
None => {
|
|
|
|
Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err())
|
2020-05-23 09:00:53 +00:00
|
|
|
}
|
2020-05-23 07:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 09:00:53 +00:00
|
|
|
|
|
|
|
/// Check login tokens
|
|
|
|
pub fn check_client_token(&mut self) -> Result<(), Box<dyn Error>> {
|
|
|
|
let api_name = self.post_string("serviceName")?;
|
|
|
|
let api_token = self.post_string("serviceToken")?;
|
|
|
|
|
|
|
|
let client = self.ok_or_bad_request(
|
|
|
|
api_helper::get_client(&api_name, &api_token),
|
|
|
|
"Client not recognized!"
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// TODO : continue here
|
|
|
|
println!("{:#?}", client);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-21 13:28:07 +00:00
|
|
|
}
|