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