rust_integration_services/http/
http_request.rs

1use std::collections::HashMap;
2
3use crate::http::http_method::HttpMethod;
4
5#[derive(Debug, Clone)]
6pub struct HttpRequest {
7    pub method: HttpMethod,
8    pub path: String,
9    pub headers: HashMap<String, String>,
10    pub params: HashMap<String, String>,
11    pub body: Vec<u8>,
12}
13
14impl HttpRequest {
15    pub fn new() -> Self {
16        HttpRequest {
17            method: HttpMethod::Get,
18            path: String::from("/"),
19            headers: HashMap::new(),
20            params: HashMap::new(),
21            body: Vec::new(),
22        }
23    }
24
25    /// Creates a http request with the method `GET`
26    pub fn get() -> Self {
27        Self::new().method("GET")
28    }
29
30    /// Creates a http request with the method `POST`
31    pub fn post() -> Self {
32        Self::new().method("POST")
33    }
34
35    /// Sets the HTTP request method.
36    pub fn method<T: AsRef<str>>(mut self, method: T) -> Self {
37        self.method = HttpMethod::from_str(method.as_ref()).expect("Invalid HTTP method");
38        self
39    }
40
41    /// Sets the HTTP request path.
42    pub fn path<T: AsRef<str>>(mut self, path: T) -> Self {
43        self.path = path.as_ref().to_string();
44        self
45    }
46
47    /// Sets the HTTP request body.
48    pub fn body(mut self, body: &[u8]) -> Self {
49        self.body = body.to_vec();
50        self
51    }
52
53    /// Adds or updates a header in the HTTP request.
54    pub fn header<T: AsRef<str>>(mut self, key: T, value: T) -> Self {
55        self.headers.insert(key.as_ref().to_string(), value.as_ref().to_string());
56        self
57    }
58}