Skip to content

Commit

Permalink
feat: add api axum based handlers (ongoing)
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Sep 28, 2023
1 parent 72c9c49 commit 242b720
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 4 additions & 5 deletions src/dsp_meta/api/project_metadata_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ pub async fn get_all_project_metadata(State(state): State<Arc<AppState>>) -> Jso
}

pub async fn store_project_metadata(
body: String,
State(state): State<Arc<AppState>>,
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
Expand Down
47 changes: 47 additions & 0 deletions src/dsp_meta/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::Serialize;

#[derive(Debug, Serialize)]
Expand All @@ -25,3 +27,48 @@ impl From<hcl::Error> 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(),
}
}
}

0 comments on commit 242b720

Please sign in to comment.