1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-07 11:22:36 +00:00
comunicapiv3/src/data/http_request_handler.rs

112 lines
3.0 KiB
Rust
Raw Normal View History

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::convert::TryFrom;
use std::error::Error;
use serde::Serialize;
2020-05-23 07:37:21 +00:00
use crate::data::error::{ResultExecError, ExecError};
use std::collections::HashMap;
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.")));
Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap())
}
2020-05-23 07:37:21 +00:00
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 login tokens
pub fn check_client_token(&mut self) -> ResultExecError<()> {
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(&self, name: &str) -> Result<&RequestValue, ExecError> {
self.body.get(name)
.ok_or(ExecError(format!("POST parameter {} not found in request!", name)))
}
/// Get a post string
pub fn post_string(&self, name: &str) -> Result<String, ExecError> {
let param = self.post_parameter(name)?;
match &param.string {
None => Err(ExecError(format!("{} is not a string!", name))),
Some(s) => Ok(s.to_string())
}
}
2020-05-21 13:28:07 +00:00
}