rust_integration_services/http/
http_request.rs

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