reqores/client/
client_request.rs

1use serde::de::DeserializeOwned;
2
3use crate::HttpMethod;
4
5use super::ClientResponse;
6
7/// Collections for common headers
8pub mod headers {
9    /// Header entry for setting "Content-Type" with "application/json; charset=UTF-8"
10    pub fn content_type_json_utf8() -> (String, String) {
11        (
12            "Content-Type".to_string(),
13            "application/json; charset=UTF-8".to_string(),
14        )
15    }
16}
17
18/// A client request to send
19pub trait ClientRequest {
20    /// The type for response.
21    type Response: DeserializeOwned;
22
23    /// The headers to send.
24    /// 
25    /// By default, it will set "Content-Type" to "application/json; charset=UTF-8".
26    fn headers(&self) -> Vec<(String, String)> {
27        vec![headers::content_type_json_utf8()]
28    }
29
30    /// The URL endpoint.
31    fn url(&self) -> String;
32
33    /// The request body to send.
34    fn body(&self) -> Option<String> {
35        None
36    }
37
38    /// The HTTP method to use.
39    fn method(&self) -> HttpMethod;
40
41    /// The way to deserialize the response.
42    /// 
43    /// By default, it will use [`serde_json::from_slice`] for deserializing response body.
44    fn deserialize(&self, response: &dyn ClientResponse) -> Result<Self::Response, String> {
45        serde_json::from_slice(response.body()).map_err(|e| e.to_string())
46    }
47}