coman/core/
http_response.rs1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
5pub struct HttpResponse {
6 pub version: String,
8 pub status: u16,
10 pub status_text: String,
12 pub headers: HashMap<String, String>,
14 pub body: String,
16 pub elapsed_ms: u128,
20 pub url: String,
22}
23
24impl HttpResponse {
25 pub fn is_success(&self) -> bool {
27 (200..300).contains(&self.status)
28 }
29
30 pub fn is_redirect(&self) -> bool {
32 (300..400).contains(&self.status)
33 }
34
35 pub fn is_client_error(&self) -> bool {
37 (400..500).contains(&self.status)
38 }
39
40 pub fn is_server_error(&self) -> bool {
42 (500..600).contains(&self.status)
43 }
44
45 pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
47 serde_json::from_str(&self.body)
48 }
49}