1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 00:45:18 +00:00

Ready to implement password check

This commit is contained in:
2020-05-23 19:17:48 +02:00
parent 975c129f7c
commit d2035a6a3f
9 changed files with 130 additions and 8 deletions

View File

@ -35,8 +35,18 @@ impl HttpError {
}
}
/// Generate a 401 error
/// Generate a 400 error
pub fn bad_request(message: &str) -> HttpError {
HttpError {
error: InnerHTTPError {
code: 400,
message: message.to_string(),
}
}
}
/// Generate a 401 error
pub fn forbidden(message: &str) -> HttpError {
HttpError {
error: InnerHTTPError {
code: 401,

View File

@ -78,6 +78,12 @@ impl HttpRequestHandler {
Ok(response)
}
/// Set request response
pub fn set_response<T: Serialize>(&mut self, data: T) -> RequestResult {
self.response = Some(HttpResponse::Ok().json(data));
Ok(())
}
/// Success message
pub fn success(&mut self, message: &str) -> RequestResult {
self.response = Some(HttpResponse::Ok().json(SuccessMessage {
@ -86,20 +92,27 @@ impl HttpRequestHandler {
Ok(())
}
/// Internal error message
/// Internal error response (500)
pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
self.response = Some(HttpResponse::InternalServerError().json(
HttpError::internal_error("Internal server error.")));
Err(error)
}
/// Bad request
/// Bad request (400)
pub fn bad_request(&mut self, message: String) -> RequestResult {
self.response = Some(HttpResponse::BadRequest().json(
HttpError::bad_request(&message)));
Err(Box::new(ExecError::new(&message)))
}
/// Forbidden (401)
pub fn forbidden(&mut self, message: String) -> RequestResult {
self.response = Some(HttpResponse::Forbidden().json(
HttpError::forbidden(&message)));
Err(Box::new(ExecError::new(&message)))
}
/// 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 {
@ -117,6 +130,11 @@ impl HttpRequestHandler {
self.request.path().to_string()
}
/// Get information about the client which made the request
pub fn api_client(&self) -> &APIClient {
self.client.as_ref().unwrap()
}
/// 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)

View File

@ -3,4 +3,6 @@ pub mod config;
pub mod http_error;
pub mod http_request_handler;
pub mod api_client;
pub mod api_client;
pub mod user;

11
src/data/user.rs Normal file
View File

@ -0,0 +1,11 @@
/// User information
///
/// @author Pierre Hubert
#[derive(Debug)]
pub struct User {
pub id: i64,
pub email: String,
pub password: String,
pub first_name: String,
pub last_name: String,
}