micro_http_async/
json_response.rs

1use crate::Response;
2
3/// # JSONResponse
4///
5/// Helpful tool for converting a serde_json json string into a response.
6///
7/// It will take in the response code, and create a response string with the headers required.
8///
9/// See the examples for usage
10pub struct JSONResponse;
11
12impl JSONResponse {
13    /// This function takes the response code and data (the json) to write, and returns a string that can be used
14    /// as a response.
15    pub async fn construct_response(response_code: Response, data: String) -> String {
16        // This should be changed over to support all response types
17        let header_code = match response_code {
18            Response::Ok => {
19                format!("HTTP/1.1 {} {}\r\n\r\n", 200, "OK")
20            }
21            Response::Redirect => {
22                format!("HTTP/1.1 {} {}\r\n\r\n", 301, "MOVED PERMANENTLY")
23            }
24            Response::ClientErr => {
25                format!("HTTP/1.1 {} {}\r\n\r\n", 404, "NOT FOUND")
26            }
27            Response::ServerErr => {
28                format!("HTTP/1.1 {} {}\r\n\r\n", 500, "INTERNEL SERVER ERROR")
29            }
30        };
31        let file = format!("{}{}", header_code, data);
32        return file;
33    }
34}