From e225cb73e420f9b2477f3eb262e5f0c7fe08dd08 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Tue, 2 Apr 2024 09:09:59 +0200 Subject: [PATCH] refactor: add app response type to build axum response from --- src/main.rs | 62 ++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 974793b..06b530e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ +use axum::{Extension, Json, Router}; use axum::body::Body; use axum::extract::Path; -use axum::http::header::AUTHORIZATION; use axum::http::{Request, StatusCode}; +use axum::http::header::AUTHORIZATION; use axum::middleware::{from_fn, Next}; -use axum::response::Response; +use axum::response::{IntoResponse, Response}; use axum::routing::{delete, post}; -use axum::{Extension, Json, Router}; use bwhc_dto::MtbFile; use clap::Parser; use lazy_static::lazy_static; @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; #[cfg(debug_assertions)] use tower_http::trace::TraceLayer; +use crate::AppResponse::{Accepted, InternalServerError, Unauthorized}; use crate::cli::Cli; use crate::sender::MtbFileSender; @@ -26,6 +27,26 @@ struct RecordKey { patient_id: String, } +enum AppResponse<'a> { + Accepted(&'a str), + Unauthorized, + InternalServerError, +} + +impl IntoResponse for AppResponse<'_> { + fn into_response(self) -> Response { + match self { + Accepted(request_id) => Response::builder() + .status(StatusCode::ACCEPTED) + .header("X-Request-Id", request_id), + Unauthorized => Response::builder().status(StatusCode::UNAUTHORIZED), + InternalServerError => Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR), + } + .body(Body::empty()) + .expect("response built") + } +} + lazy_static! { static ref CONFIG: Cli = Cli::parse(); } @@ -67,10 +88,7 @@ async fn check_basic_auth(request: Request, next: Next) -> Response { return next.run(request).await; } } - Response::builder() - .status(StatusCode::UNAUTHORIZED) - .body(Body::empty()) - .expect("response built") + Unauthorized.into_response() } async fn handle_delete( @@ -80,8 +98,8 @@ async fn handle_delete( let delete_mtb_file = MtbFile::new_with_consent_rejected(&patient_id); match sender.send(delete_mtb_file).await { - Ok(request_id) => success_response(&request_id), - _ => error_response(), + Ok(request_id) => Accepted(&request_id).into_response(), + _ => InternalServerError.into_response(), } } @@ -90,43 +108,29 @@ async fn handle_post( Json(mtb_file): Json, ) -> Response { match sender.send(mtb_file).await { - Ok(request_id) => success_response(&request_id), - _ => error_response(), + Ok(request_id) => Accepted(&request_id).into_response(), + _ => InternalServerError.into_response(), } } -fn success_response(request_id: &str) -> Response { - Response::builder() - .status(StatusCode::ACCEPTED) - .header("X-Request-Id", request_id) - .body(Body::empty()) - .expect("response built") -} - -fn error_response() -> Response { - Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(Body::empty()) - .expect("response built") -} - #[cfg(test)] mod tests { use axum::http::StatusCode; + use axum::response::IntoResponse; use uuid::Uuid; - use crate::{error_response, success_response}; + use crate::AppResponse::{Accepted, InternalServerError}; #[test] fn should_return_success_response() { - let response = success_response(&Uuid::new_v4().to_string()); + let response = Accepted(&Uuid::new_v4().to_string()).into_response(); assert_eq!(response.status(), StatusCode::ACCEPTED); assert!(response.headers().contains_key("x-request-id")); } #[test] fn should_return_error_response() { - let response = error_response(); + let response = InternalServerError.into_response(); assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR); assert_eq!(response.headers().contains_key("x-request-id"), false); }