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

Handle pre-flight requests

This commit is contained in:
2021-03-14 14:25:11 +01:00
parent c29d8b1997
commit eb0c7aec6a
5 changed files with 57 additions and 30 deletions

View File

@ -1,23 +1,34 @@
use crate::constants::database_tables_names::CLIENTS_TABLE;
use crate::data::api_client::APIClient;
use crate::data::error::ResultBoxError;
use crate::data::error::{Res, ResultBoxError};
use crate::helpers::database;
use crate::helpers::database::QueryInfo;
/// API helper
///
/// @author Pierre Hubert
/// Get information about a client
/// Get information about a client, based on its token
pub fn get_client(name: &str) -> ResultBoxError<APIClient> {
QueryInfo::new(CLIENTS_TABLE)
.cond("name", name)
.query_row(|res| {
Ok(APIClient {
id: res.get_u64("id")?,
name: res.get_str("name")?,
domain: res.get_optional_str("domain")?,
comment: res.get_optional_str("comment")?,
default_expiration_time: res.get_u64("default_expiration_time")?,
})
})
.query_row(db_to_client)
}
/// Get information about a client, based on its origin
pub fn get_by_origin(name: &str) -> ResultBoxError<APIClient> {
QueryInfo::new(CLIENTS_TABLE)
.cond("domain", name)
.query_row(db_to_client)
}
fn db_to_client(res: &database::RowResult) -> Res<APIClient> {
Ok(APIClient {
id: res.get_u64("id")?,
name: res.get_str("name")?,
domain: res.get_optional_str("domain")?,
comment: res.get_optional_str("comment")?,
default_expiration_time: res.get_u64("default_expiration_time")?,
})
}