1use crate::types::HttpResonse;
2use hyper::{header, Body, Response, StatusCode};
3use serde_json::Value;
4
5pub fn build_response(
6 body_text: String,
7 status_code: StatusCode,
8 content_type: &str,
9) -> anyhow::Result<HttpResonse> {
10 let mut r = Response::builder()
11 .status(status_code)
12 .body(Body::from(body_text))?;
13 r.headers_mut()
14 .insert(header::CONTENT_TYPE, content_type.parse()?);
15 Ok(r)
16}
17
18pub fn response_json(body_text: String, status_code: StatusCode) -> anyhow::Result<HttpResonse> {
19 build_response(body_text, status_code, "application/json")
20}
21
22pub fn ok_json(v: Value) -> anyhow::Result<HttpResonse> {
23 ret_json(StatusCode::OK, v)
24}
25
26pub fn ret_json(status_code: StatusCode, v: Value) -> anyhow::Result<HttpResonse> {
27 response_json(v.to_string(), status_code)
28}
29
30pub fn internal_server_error(error: anyhow::Error) -> anyhow::Result<HttpResonse> {
31 build_response(
32 format!("Error: {}", error.to_string()),
33 StatusCode::INTERNAL_SERVER_ERROR,
34 "text/html",
35 )
36}
37
38pub fn internal_server_error_force(error: anyhow::Error) -> HttpResonse {
39 return internal_server_error(error).unwrap();
40}