reifydb_network/http/
builder.rs1use std::collections::HashMap;
5
6use super::HttpResponse;
7
8pub struct HttpResponseBuilder {
9 status_code: u16,
10 status_text: String,
11 headers: HashMap<String, String>,
12 body: Vec<u8>,
13}
14
15impl HttpResponseBuilder {
16 pub fn new() -> Self {
17 Self {
18 status_code: 200,
19 status_text: "OK".to_string(),
20 headers: HashMap::new(),
21 body: Vec::new(),
22 }
23 }
24
25 pub fn status(mut self, code: u16, text: &str) -> Self {
26 self.status_code = code;
27 self.status_text = text.to_string();
28 self
29 }
30
31 pub fn header(mut self, key: &str, value: &str) -> Self {
32 self.headers.insert(key.to_string(), value.to_string());
33 self
34 }
35
36 pub fn body(mut self, body: Vec<u8>) -> Self {
37 self.body = body;
38 self
39 }
40
41 pub fn json(mut self, json: &str) -> Self {
42 self.headers.insert("Content-Type".to_string(), "application/json".to_string());
43 self.body = json.as_bytes().to_vec();
44 self
45 }
46
47 pub fn html(mut self, html: &str) -> Self {
48 self.headers.insert("Content-Type".to_string(), "text/html; charset=utf-8".to_string());
49 self.body = html.as_bytes().to_vec();
50 self
51 }
52
53 pub fn build(self) -> HttpResponse {
54 HttpResponse {
55 status_code: self.status_code,
56 status_text: self.status_text,
57 headers: self.headers,
58 body: self.body,
59 }
60 }
61}
62
63impl Default for HttpResponseBuilder {
64 fn default() -> Self {
65 Self::new()
66 }
67}