mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-25 23:09:22 +00:00
Get information about API client
This commit is contained in:
parent
b18f6f4190
commit
665aa1683c
11
src/data/api_client.rs
Normal file
11
src/data/api_client.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/// API client information
|
||||||
|
///
|
||||||
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct APIClient {
|
||||||
|
pub id: u32,
|
||||||
|
pub name: String,
|
||||||
|
pub token: String,
|
||||||
|
pub domain: Option<String>,
|
||||||
|
}
|
@ -5,6 +5,7 @@ use std::error::Error;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use crate::data::error::{ResultBoxError, ExecError};
|
use crate::data::error::{ResultBoxError, ExecError};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use crate::helpers::api_helper;
|
||||||
|
|
||||||
/// Http request handler
|
/// Http request handler
|
||||||
///
|
///
|
||||||
@ -82,18 +83,23 @@ impl HttpRequestHandler {
|
|||||||
Err(Box::new(ExecError::new(&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 {
|
||||||
|
Ok(e) => Ok(e),
|
||||||
|
Err(err) => {
|
||||||
|
println!("Error leading to bad request: {}", err);
|
||||||
|
self.bad_request(msg.to_string())?;
|
||||||
|
unreachable!()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the path of the request
|
/// Get the path of the request
|
||||||
pub fn request_path(&self) -> String {
|
pub fn request_path(&self) -> String {
|
||||||
self.request.path().to_string()
|
self.request.path().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check login tokens
|
|
||||||
pub fn check_client_token(&mut self) -> Result<(), Box<dyn Error>> {
|
|
||||||
println!("me = {}", self.post_string("me")?);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if a POST parameter was present in the request or not
|
/// Check if a POST parameter was present in the request or not
|
||||||
pub fn has_post_parameter(&self, name: &str) -> bool {
|
pub fn has_post_parameter(&self, name: &str) -> bool {
|
||||||
self.body.contains_key(name)
|
self.body.contains_key(name)
|
||||||
@ -116,7 +122,22 @@ impl HttpRequestHandler {
|
|||||||
Some(s) => Ok(s.to_string()),
|
Some(s) => Ok(s.to_string()),
|
||||||
None => {
|
None => {
|
||||||
Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err())
|
Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err())
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check login tokens
|
||||||
|
pub fn check_client_token(&mut self) -> Result<(), Box<dyn Error>> {
|
||||||
|
let api_name = self.post_string("serviceName")?;
|
||||||
|
let api_token = self.post_string("serviceToken")?;
|
||||||
|
|
||||||
|
let client = self.ok_or_bad_request(
|
||||||
|
api_helper::get_client(&api_name, &api_token),
|
||||||
|
"Client not recognized!"
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// TODO : continue here
|
||||||
|
println!("{:#?}", client);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
@ -3,3 +3,4 @@ pub mod config;
|
|||||||
|
|
||||||
pub mod http_error;
|
pub mod http_error;
|
||||||
pub mod http_request_handler;
|
pub mod http_request_handler;
|
||||||
|
pub mod api_client;
|
6
src/database_structure.rs
Normal file
6
src/database_structure.rs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
/// Database structure information
|
||||||
|
///
|
||||||
|
/// @author Pierre Hubert
|
||||||
|
|
||||||
|
/// API services tokens table
|
||||||
|
pub const SERVICES_TABLES : &str = "comunic_api_services_tokens";
|
27
src/helpers/api_helper.rs
Normal file
27
src/helpers/api_helper.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use crate::data::api_client::APIClient;
|
||||||
|
use crate::helpers::database;
|
||||||
|
use crate::helpers::database::QueryInfo;
|
||||||
|
use crate::database_structure::SERVICES_TABLES;
|
||||||
|
use crate::data::error::ResultBoxError;
|
||||||
|
|
||||||
|
/// API helper
|
||||||
|
///
|
||||||
|
/// @author Pierre Hubert
|
||||||
|
|
||||||
|
/// Get information about a client
|
||||||
|
pub fn get_client(name: &str, token: &str) -> ResultBoxError<APIClient> {
|
||||||
|
database::query_row(
|
||||||
|
QueryInfo::new(SERVICES_TABLES)
|
||||||
|
.cond("service_name", name)
|
||||||
|
.cond("token", token),
|
||||||
|
|
||||||
|
|res| {
|
||||||
|
Ok(APIClient {
|
||||||
|
id: res.get_int64("id")? as u32,
|
||||||
|
name: res.get_str("service_name")?,
|
||||||
|
token: res.get_str("token")?,
|
||||||
|
domain: res.get_optional_str("domain")?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
@ -125,6 +125,14 @@ impl<'a> RowResult<'a> {
|
|||||||
Some(s) => Ok(s)
|
Some(s) => Ok(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get an optional string
|
||||||
|
pub fn get_optional_str(&self, name: &str) -> Result<Option<String>, ExecError> {
|
||||||
|
match self.find_col(name) {
|
||||||
|
Ok(col) => Ok(self.row.get(col)),
|
||||||
|
Err(_) => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1 +1,3 @@
|
|||||||
pub mod database;
|
pub mod database;
|
||||||
|
|
||||||
|
pub mod api_helper;
|
@ -1,3 +1,4 @@
|
|||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod controllers;
|
pub mod controllers;
|
||||||
|
pub mod database_structure;
|
Loading…
Reference in New Issue
Block a user