telegram_bot_raw_ars/requests/_base/
http.rs

1use std::fmt;
2
3use bytes::Bytes;
4
5use crate::types::Text;
6use crate::url::telegram_api_url;
7
8#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9pub enum RequestUrl {
10    Method(&'static str),
11}
12
13impl RequestUrl {
14    pub fn method(method: &'static str) -> Self {
15        RequestUrl::Method(method)
16    }
17
18    pub fn url(&self, token: &str) -> String {
19        match self {
20            &RequestUrl::Method(method) => format!("{}bot{}/{}", telegram_api_url(), token, method),
21        }
22    }
23}
24
25#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
26pub enum Method {
27    Get,
28    Post,
29}
30
31#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
32pub enum MultipartValue {
33    Text(Text),
34    Path { path: Text, file_name: Option<Text> },
35    Data { file_name: Text, data: Bytes },
36}
37
38pub type Multipart = Vec<(&'static str, MultipartValue)>;
39
40#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
41pub enum Body {
42    Empty,
43    Multipart(Multipart),
44    Json(String),
45    #[doc(hidden)]
46    __Nonexhaustive,
47}
48
49impl fmt::Display for Body {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
51        match self {
52            Body::Empty => "<empty body>".fmt(f),
53            Body::Multipart(multipart) => write!(f, "{:?}", multipart),
54            Body::Json(s) => s.fmt(f),
55            Body::__Nonexhaustive => unreachable!(),
56        }
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
61pub struct HttpRequest {
62    pub url: RequestUrl,
63    pub method: Method,
64    pub body: Body,
65}
66
67impl HttpRequest {
68    pub fn name(&self) -> &'static str {
69        match self.url {
70            RequestUrl::Method(method) => method,
71        }
72    }
73}
74
75#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
76pub struct HttpResponse {
77    pub body: Option<Vec<u8>>,
78}