1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 17:05:16 +00:00

Start to read POST request arguments

This commit is contained in:
2020-05-23 09:37:21 +02:00
parent fcc38afe42
commit 7c18f19674
6 changed files with 175 additions and 32 deletions

View File

@ -6,6 +6,10 @@ use std::error;
///
/// @author Pierre Hubert
/// Simple result type
pub type ResultExecError<E> = Result<E, ExecError>;
#[derive(Debug, Clone)]
pub struct ExecError(pub String);
@ -17,6 +21,10 @@ impl ExecError {
pub fn boxed_new(msg: &str) -> Box<ExecError> {
Box::new(ExecError(msg.to_string()))
}
pub fn boxed_string(msg: String) -> Box<ExecError> {
Box::new(ExecError(msg))
}
}
impl fmt::Display for ExecError {

View File

@ -4,11 +4,27 @@ use crate::data::http_error::HttpError;
use std::convert::TryFrom;
use std::error::Error;
use serde::Serialize;
use crate::data::error::{ResultExecError, ExecError};
use std::collections::HashMap;
/// Http request handler
///
/// @author Pierre Hubert
/// 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)
}
}
}
#[derive(Serialize)]
struct SuccessMessage {
success: String
@ -16,16 +32,17 @@ struct SuccessMessage {
pub struct HttpRequestHandler {
request: web::HttpRequest,
response: Option<web::HttpResponse>
body: HashMap<String, RequestValue>,
response: Option<web::HttpResponse>,
}
impl HttpRequestHandler {
/// Construct a new request handler
pub fn new(req: HttpRequest) -> HttpRequestHandler {
pub fn new(req: HttpRequest, body: HashMap<String, RequestValue>) -> HttpRequestHandler {
HttpRequestHandler {
request: req,
response: None
body,
response: None,
}
}
@ -53,4 +70,34 @@ impl HttpRequestHandler {
HttpError::internal_error("Internal server error.")));
Err(Box::try_from(actix_web::error::ErrorInternalServerError(error)).unwrap())
}
/// 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())
}
}
}