fundamentum_edge_mcu_http_client/models/http_request.rs
1use core::str;
2
3use serde::Deserialize;
4use strum::AsRefStr;
5
6/// HTTP request fields.
7///
8/// ## Example
9///
10/// ```
11/// use fundamentum_edge_mcu_http_client::models::{HttpBody, HttpMethod, HttpRequest};
12///
13/// let request = HttpRequest {
14/// method: HttpMethod::POST,
15/// domain_name: "devices.fundamentum-iot.com",
16/// port: 443,
17/// user_agent: "Fundamentum/EdgeMCU/0.1.0/rust",
18/// base_path: Some("/api/v3"),
19/// path: "/provision",
20/// accept: "application/json",
21/// body: Some(HttpBody {
22/// content_type: "application/json",
23/// body: r#"{"key", "value"}"#.as_bytes(),
24/// }),
25/// };
26/// ```
27#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
28pub struct HttpRequest<'a> {
29 /// HTTP Method
30 pub method: HttpMethod,
31 /// Domain name of the remote server
32 pub domain_name: &'a str,
33 /// Server TCP port
34 pub port: u16,
35 /// User Agent of the HTTP client
36 pub user_agent: &'a str,
37 /// Base common path
38 pub base_path: Option<&'a str>,
39 /// Path on the remote server
40 pub path: &'a str,
41 /// Content types supported / accepted by the client for the response.
42 ///
43 /// ### Note
44 ///
45 /// All `Accept` headers must stand on a single line as noted by the [MDN
46 /// documentation][mdn-doc].
47 ///
48 /// [mdn-doc]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept#syntax
49 pub accept: &'a str,
50 /// Request's body
51 pub body: Option<HttpBody<'a>>,
52}
53
54/// HTTP request methods.
55#[derive(AsRefStr)]
56#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
57#[cfg_attr(feature = "log", derive(defmt::Format))]
58pub enum HttpMethod {
59 /// GET
60 #[strum(serialize = "GET")]
61 GET,
62 /// PUT
63 #[strum(serialize = "PUT")]
64 PUT,
65 /// POST
66 #[strum(serialize = "POST")]
67 POST,
68 /// DELETE
69 #[strum(serialize = "DELETE")]
70 DELETE,
71}
72
73/// HTTP status code
74#[derive(Deserialize)]
75#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
76#[cfg_attr(feature = "log", derive(defmt::Format))]
77pub struct StatusCode(pub u16);
78
79/// Body of an HTTP request
80#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
81pub struct HttpBody<'a> {
82 /// `Content-Type` of the request's body
83 pub content_type: &'a str,
84 /// Request's body
85 pub body: &'a [u8],
86}