reqwest_enum/
http.rs

1use reqwest::Method;
2
3#[derive(Debug, Default)]
4pub struct HTTPBody {
5    pub inner: reqwest::Body,
6}
7
8impl HTTPBody {
9    pub fn to_bytes(&self) -> Vec<u8> {
10        self.inner.as_bytes().unwrap_or_default().to_vec()
11    }
12}
13
14pub type HTTPResponse = reqwest::Response;
15
16pub enum HTTPMethod {
17    GET,
18    POST,
19    PUT,
20    DELETE,
21    PATCH,
22    OPTIONS,
23    HEAD,
24    CONNECT,
25}
26
27impl std::fmt::Display for HTTPMethod {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        let s = match self {
30            HTTPMethod::GET => "GET",
31            HTTPMethod::POST => "POST",
32            HTTPMethod::PUT => "PUT",
33            HTTPMethod::DELETE => "DELETE",
34            HTTPMethod::PATCH => "PATCH",
35            HTTPMethod::OPTIONS => "OPTIONS",
36            HTTPMethod::HEAD => "HEAD",
37            HTTPMethod::CONNECT => "CONNECT",
38        };
39        write!(f, "{}", s)
40    }
41}
42
43impl From<HTTPMethod> for Method {
44    fn from(val: HTTPMethod) -> Self {
45        match val {
46            HTTPMethod::GET => Method::GET,
47            HTTPMethod::POST => Method::POST,
48            HTTPMethod::PUT => Method::PUT,
49            HTTPMethod::DELETE => Method::DELETE,
50            HTTPMethod::PATCH => Method::PATCH,
51            HTTPMethod::OPTIONS => Method::OPTIONS,
52            HTTPMethod::HEAD => Method::HEAD,
53            HTTPMethod::CONNECT => Method::CONNECT,
54        }
55    }
56}
57
58impl HTTPBody {
59    pub fn from<T>(value: &T) -> Self
60    where
61        T: serde::Serialize,
62    {
63        let mut bytes: Vec<u8> = Vec::new();
64        serde_json::to_writer(&mut bytes, value).expect("serde_json serialize error");
65        Self {
66            inner: bytes.into(),
67        }
68    }
69
70    pub fn from_array<T>(array: &[T]) -> Self
71    where
72        T: serde::Serialize,
73    {
74        let mut bytes: Vec<u8> = Vec::new();
75        serde_json::to_writer(&mut bytes, array).expect("serde_json serialize error");
76        Self {
77            inner: bytes.into(),
78        }
79    }
80}
81
82pub enum AuthMethod {
83    // Basic(username, password)
84    Basic(&'static str, &'static str),
85    // Bearer(token)
86    Bearer(&'static str),
87}