Implement all API according to swagger spec #27

Merged
blek merged 16 commits from delete-upload-api-methods into 0.2-dev 2023-12-14 11:56:46 +01:00
4 changed files with 66 additions and 5 deletions
Showing only changes of commit bf923244fa - Show all commits

View File

@ -1,7 +1,7 @@
use warp::{reply::Reply, reject::Rejection, Filter};
use serde::{Serialize, Deserialize};
use self::files::get_all::get_all_f;
use self::files::{get_all::get_all_f, delete::delete_f};
use super::state::SharedState;
@ -24,5 +24,6 @@ pub fn get_routes(state: SharedState) -> impl Filter<Extract = impl Reply, Error
warp::path!("api")
.and(warp::path::end())
.map(api_root)
.or(get_all_f(state))
.or(get_all_f(state.clone()))
.or(delete_f(state))
}

View File

@ -24,3 +24,4 @@ fn function_disabled_err() -> WithStatus<Json> {
}
pub mod get_all;
pub mod delete;

View File

@ -0,0 +1,57 @@
use std::collections::HashMap;
use warp::{reply::{Reply, json}, reject::Rejection, Filter, http::StatusCode};
use serde::{Serialize, Deserialize};
use crate::web::{state::SharedState, rejection::HttpReject, api::types::{ErrorMessage, Error}};
use super::{function_disabled_err, check_api_enabled};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DeleteFunctionPayload {
pub fid: String
}
pub async fn delete(state: SharedState, body: DeleteFunctionPayload) -> Result<Box<dyn Reply>, Rejection> {
if let Err(res) = check_api_enabled(&state) {
return Ok(Box::new(res));
}
if state.config.api.delete {
return Ok(Box::new(function_disabled_err()))
}
let id = body.fid;
let mut file = state.file_mgr.find_by_hash(id.clone())
.map_err(|x| HttpReject::StringError(x.to_string()))?;
if let None = file {
file = state.file_mgr.find_by_name(id)
.map_err(|x| HttpReject::StringError(x.to_string()))?;
}
if let None = file {
return Ok(
Box::new(
warp::reply::with_status(
json(
&ErrorMessage {
error: Error::APIError,
details: Some("No file with that ID was found.".into())
}
),
StatusCode::NOT_FOUND
)
)
)
}
Ok(Box::new(json(&HashMap::<(), ()>::new())))
}
pub fn delete_f(state: SharedState) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path!("api" / "files" / "delete")
.map(move || state.clone())
.and(warp::body::json())
.and_then(delete)
}

View File

@ -3,7 +3,8 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error {
APIDisabled,
APIFunctionDisabled
APIFunctionDisabled,
APIError
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -17,7 +18,8 @@ impl ErrorMessage {
ErrorMessage {
details: match error {
Error::APIDisabled => Some("API is disabled by the administrator. Please contact them for further details".into()),
Error::APIFunctionDisabled => Some("This API function is disabled by the administrator. Please contact them for further details.".into())
Error::APIFunctionDisabled => Some("This API function is disabled by the administrator. Please contact them for further details.".into()),
Error::APIError => Some("An error has occured while executing the API request".into())
},
error,
}