wae_https/response/
mod.rs1use axum::{
6 body::Body,
7 http::{Response, StatusCode, header},
8 response::IntoResponse,
9};
10use serde::Serialize;
11
12use crate::ApiResponse;
13
14pub struct JsonResponse;
16
17impl JsonResponse {
18 pub fn success<T: Serialize>(data: T) -> Response<Body> {
20 ApiResponse::success(data).into_response()
21 }
22
23 pub fn error(code: impl Into<String>, message: impl Into<String>) -> Response<Body> {
25 ApiResponse::<()>::error(code, message).into_response()
26 }
27
28 pub fn not_found(message: impl Into<String>) -> Response<Body> {
30 let body = ApiResponse::<()>::error("NOT_FOUND", message);
31 Response::builder()
32 .status(StatusCode::NOT_FOUND)
33 .header(header::CONTENT_TYPE, "application/json")
34 .body(Body::from(serde_json::to_string(&body).unwrap_or_default()))
35 .unwrap()
36 }
37
38 pub fn internal_error(message: impl Into<String>) -> Response<Body> {
40 let body = ApiResponse::<()>::error("INTERNAL_ERROR", message);
41 Response::builder()
42 .status(StatusCode::INTERNAL_SERVER_ERROR)
43 .header(header::CONTENT_TYPE, "application/json")
44 .body(Body::from(serde_json::to_string(&body).unwrap_or_default()))
45 .unwrap()
46 }
47}