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

Create new api_data module

This commit is contained in:
2020-05-24 13:09:50 +02:00
parent d2035a6a3f
commit 5554197310
8 changed files with 46 additions and 24 deletions

View File

@ -0,0 +1,57 @@
use serde::Serialize;
/// HTTP request error
///
/// @author Pierre Hubert
#[derive(Serialize)]
pub struct InnerHTTPError {
pub code: u16,
pub message: String,
}
#[derive(Serialize)]
pub struct HttpError {
pub error: InnerHTTPError
}
impl HttpError {
/// Generate a 404 error
pub fn not_found(message: &str) -> HttpError {
HttpError {
error: InnerHTTPError {
code: 404,
message: message.to_string(),
}
}
}
/// Generate a 500 error
pub fn internal_error(message: &str) -> HttpError {
HttpError {
error: InnerHTTPError {
code: 500,
message: message.to_string(),
}
}
}
/// 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,
message: message.to_string(),
}
}
}
}

View File

@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};
/// Data sent in case of successful login
///
/// @author Pierre Hubert
#[derive(Serialize, Deserialize)]
struct LoginTokens {
token1: String,
token2: String,
}
#[derive(Serialize, Deserialize)]
pub struct LoginSuccess {
success: String,
tokens: LoginTokens,
}
impl LoginSuccess {
pub fn new(token: &str) -> LoginSuccess {
LoginSuccess {
success: "Login successful".to_string(),
tokens: LoginTokens {
token1: token.to_string(),
token2: "dummy_data".to_string(),
},
}
}
}

9
src/api_data/mod.rs Normal file
View File

@ -0,0 +1,9 @@
///! # Presentation
///! This crate contains all the serializable structures that can be sent through the API to the
///! Clients
///!
///! ## Author
///! Pierre Hubert
pub mod http_error;
pub mod login_success;