1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00

Create new api_data module

This commit is contained in:
Pierre HUBERT 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,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;

View File

@ -1,23 +1,12 @@
use serde::{Deserialize, Serialize}; use crate::api_data::login_success::LoginSuccess;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::http_request_handler::HttpRequestHandler;
use crate::helpers::account_helper; use crate::helpers::account_helper;
/// Account controller /// Account controller
/// ///
/// @author Pierre Hubert /// @author Pierre Hubert
#[derive(Serialize, Deserialize)]
struct LoginTokens {
token1: String,
token2: String,
}
#[derive(Serialize, Deserialize)]
struct LoginSuccess {
tokens: LoginTokens
}
/// Sign in user /// Sign in user
pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
let email = request.post_email("userMail")?; let email = request.post_email("userMail")?;
@ -31,17 +20,12 @@ pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
match token { match token {
Ok(t) => { Ok(t) => {
request.set_response(LoginSuccess { request.set_response(LoginSuccess::new(&t))
tokens: LoginTokens {
token1: t,
token2: "dummy_data".to_string()
} }
})
},
Err(e) => { Err(e) => {
// TODO : limit request // TODO : limit request
println!("Error on login: {}", e); println!("Error on login: {}", e);
request.forbidden("Invalid email address / password!".to_string()) request.forbidden("Invalid email address / password!".to_string())
}, }
} }
} }

View File

@ -2,7 +2,6 @@ use actix_web::{App, HttpResponse, HttpServer, web, http, FromRequest, HttpReque
use crate::controllers::routes::{get_routes, Route, RequestResult}; use crate::controllers::routes::{get_routes, Route, RequestResult};
use crate::data::config::Config; use crate::data::config::Config;
use crate::data::http_error::HttpError;
use crate::controllers::routes::Method::{GET, POST}; use crate::controllers::routes::Method::{GET, POST};
use crate::data::http_request_handler::{HttpRequestHandler, RequestValue}; use crate::data::http_request_handler::{HttpRequestHandler, RequestValue};
use actix_web::dev::{PayloadStream, Payload, Decompress}; use actix_web::dev::{PayloadStream, Payload, Decompress};
@ -13,6 +12,7 @@ use actix_web::error::{ErrorBadRequest};
use encoding_rs::UTF_8; use encoding_rs::UTF_8;
use std::collections::HashMap; use std::collections::HashMap;
use percent_encoding::{percent_decode_str}; use percent_encoding::{percent_decode_str};
use crate::api_data::http_error::HttpError;
/// Main server functions /// Main server functions

View File

@ -1,6 +1,5 @@
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use crate::controllers::routes::RequestResult; use crate::controllers::routes::RequestResult;
use crate::data::http_error::HttpError;
use std::error::Error; use std::error::Error;
use serde::Serialize; use serde::Serialize;
use crate::data::error::{ResultBoxError, ExecError}; use crate::data::error::{ResultBoxError, ExecError};
@ -10,6 +9,7 @@ use actix_web::http::{HeaderName, HeaderValue};
use std::str::FromStr; use std::str::FromStr;
use crate::data::config::conf; use crate::data::config::conf;
use crate::data::api_client::APIClient; use crate::data::api_client::APIClient;
use crate::api_data::http_error::HttpError;
/// Http request handler /// Http request handler
/// ///

View File

@ -1,7 +1,6 @@
pub mod error; pub mod error;
pub mod config; pub mod config;
pub mod http_error;
pub mod http_request_handler; pub mod http_request_handler;
pub mod api_client; pub mod api_client;

View File

@ -1,4 +1,6 @@
pub mod data; pub mod data;
pub mod helpers; pub mod helpers;
pub mod controllers; pub mod controllers;
pub mod api_data;
pub mod database_structure; pub mod database_structure;