mirror of
https://github.com/CCC-MF/bwhc-kafka-rest-proxy.git
synced 2025-04-19 11:06:51 +00:00
refactor: add app response type to build axum response from
This commit is contained in:
parent
c99ee8cdfb
commit
e225cb73e4
62
src/main.rs
62
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<Body>, 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<MtbFile>,
|
||||
) -> 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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user