From 242b72014705475596042fa4ce191d9cf57d49a6 Mon Sep 17 00:00:00 2001 From: Ivan Subotic <400790+subotic@users.noreply.github.com> Date: Thu, 28 Sep 2023 13:26:04 +0200 Subject: [PATCH] feat: add api axum based handlers (ongoing) --- Cargo.toml | 1 + src/dsp_meta/api/project_metadata_handler.rs | 9 ++-- src/dsp_meta/errors.rs | 47 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ebed5d81..10ef016e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ documentation = "https://github.com/dasch-swiss/dsp-ingest" readme = "README.md" description = "The DSP Metadata Command Line Tool providing transformation, validation and serving of research project's metadata." authors = ["DaSCH - Swiss National Data and Service Center for the Humanities"] +default-run = "dsp_meta_server" [lib] name = "dsp_meta" diff --git a/src/dsp_meta/api/project_metadata_handler.rs b/src/dsp_meta/api/project_metadata_handler.rs index a750fcb9..32a3c3f8 100644 --- a/src/dsp_meta/api/project_metadata_handler.rs +++ b/src/dsp_meta/api/project_metadata_handler.rs @@ -35,18 +35,17 @@ pub async fn get_all_project_metadata(State(state): State>) -> Jso } pub async fn store_project_metadata( - body: String, State(state): State>, + body: String, ) -> Result<(), DspMetaError> { trace!("entered store_project_metadata"); + let service = &state.project_metadata_service; + let hcl_body = hcl::from_str(body.as_str())?; let project_metadata = ProjectMetadata::try_from(&hcl_body)?; - let store_result = state - .project_metadata_service - .store(&project_metadata.project.shortcode, &project_metadata); - store_result + service.store(&project_metadata.project.shortcode, &project_metadata) } // basic handler that responds with a static string diff --git a/src/dsp_meta/errors.rs b/src/dsp_meta/errors.rs index bc38f714..f1ebd471 100644 --- a/src/dsp_meta/errors.rs +++ b/src/dsp_meta/errors.rs @@ -1,5 +1,7 @@ use std::io; +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; use serde::Serialize; #[derive(Debug, Serialize)] @@ -25,3 +27,48 @@ impl From for DspMetaError { DspMetaError::ParseHcl(error.to_string()) } } + +/// Convert `DspMetaError` into a response. +/// TODO: Add correct status codes and error messages. +impl IntoResponse for DspMetaError { + fn into_response(self) -> Response { + match self { + DspMetaError::IO(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::ParseHcl(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::UnknownAttribute(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::ParseVersion(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::ParseProject(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::ParseDataset(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::CreateValueObject(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Something went wrong: {}", err), + ) + .into_response(), + DspMetaError::NotFound => (StatusCode::NOT_FOUND, "Not Found").into_response(), + } + } +}