Skip to main content

alpaca_http/
request.rs

1use reqwest::{Method, header::HeaderMap};
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum RequestBody {
5    Empty,
6    Json(serde_json::Value),
7    Text(String),
8    Bytes(Vec<u8>),
9}
10
11#[derive(Debug, Clone)]
12pub struct RequestParts {
13    operation: Option<String>,
14    method: Method,
15    path: String,
16    query: Vec<(String, String)>,
17    headers: HeaderMap,
18    body: RequestBody,
19}
20
21impl RequestParts {
22    #[must_use]
23    pub fn new(method: Method, path: impl Into<String>) -> Self {
24        Self {
25            operation: None,
26            method,
27            path: path.into(),
28            query: Vec::new(),
29            headers: HeaderMap::new(),
30            body: RequestBody::Empty,
31        }
32    }
33
34    #[must_use]
35    pub fn with_operation(mut self, operation: impl Into<String>) -> Self {
36        self.operation = Some(operation.into());
37        self
38    }
39
40    #[must_use]
41    pub fn with_query<I>(mut self, query: I) -> Self
42    where
43        I: IntoIterator<Item = (String, String)>,
44    {
45        self.query = query.into_iter().collect();
46        self
47    }
48
49    #[must_use]
50    pub fn with_headers(mut self, headers: HeaderMap) -> Self {
51        self.headers = headers;
52        self
53    }
54
55    #[must_use]
56    pub fn with_json_body(mut self, body: serde_json::Value) -> Self {
57        self.body = RequestBody::Json(body);
58        self
59    }
60
61    #[must_use]
62    pub fn with_text_body(mut self, body: impl Into<String>) -> Self {
63        self.body = RequestBody::Text(body.into());
64        self
65    }
66
67    #[must_use]
68    pub fn with_bytes_body(mut self, body: Vec<u8>) -> Self {
69        self.body = RequestBody::Bytes(body);
70        self
71    }
72
73    #[must_use]
74    pub fn operation(&self) -> Option<&str> {
75        self.operation.as_deref()
76    }
77
78    #[must_use]
79    pub fn method(&self) -> Method {
80        self.method.clone()
81    }
82
83    #[must_use]
84    pub fn path(&self) -> &str {
85        &self.path
86    }
87
88    #[must_use]
89    pub fn query(&self) -> &[(String, String)] {
90        &self.query
91    }
92
93    #[must_use]
94    pub fn headers(&self) -> &HeaderMap {
95        &self.headers
96    }
97
98    #[must_use]
99    pub fn body(&self) -> &RequestBody {
100        &self.body
101    }
102}
103
104#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
105pub struct NoContent;