pnwkit_core/
request.rs

1use async_trait::async_trait;
2use std::fmt::Debug;
3
4#[derive(Debug, Clone)]
5pub struct Request {
6    pub method: Method,
7    pub url: String,
8    pub body: Option<String>,
9    pub headers: Option<Headers>,
10    pub content_type: Option<ContentType>,
11}
12
13impl Request {
14    pub fn new(
15        method: Method,
16        url: String,
17        body: Option<String>,
18        headers: Option<Headers>,
19        content_type: Option<ContentType>,
20    ) -> Self {
21        Self {
22            method,
23            url,
24            body,
25            headers,
26            content_type,
27        }
28    }
29}
30
31#[derive(Debug, Clone)]
32pub enum Method {
33    Get,
34    Post,
35    Patch,
36    Put,
37    Delete,
38}
39
40#[derive(Debug, Default, Clone)]
41pub struct Headers {
42    pub authorization: String,
43    pub x_api_key: Option<String>,
44    pub x_bot_key: Option<String>,
45    pub user_agent: String,
46}
47
48impl Headers {
49    pub fn new() -> Self {
50        Self::default()
51    }
52
53    pub(crate) fn set_authorization(&mut self, authorization: String) {
54        self.authorization = authorization;
55    }
56
57    pub(crate) fn set_x_api_key(&mut self, x_api_key: String) {
58        self.x_api_key = Some(x_api_key);
59    }
60
61    pub(crate) fn set_x_bot_key(&mut self, x_bot_key: String) {
62        self.x_bot_key = Some(x_bot_key);
63    }
64
65    pub(crate) fn set_user_agent(&mut self, user_agent: String) {
66        self.user_agent = user_agent;
67    }
68}
69
70#[derive(Clone, Debug)]
71pub enum ContentType {
72    Json,
73    Form,
74}
75
76impl Default for ContentType {
77    fn default() -> Self {
78        Self::Json
79    }
80}
81
82impl ToString for ContentType {
83    fn to_string(&self) -> String {
84        match self {
85            Self::Json => "application/json".to_string(),
86            Self::Form => "application/x-www-form-urlencoded".to_string(),
87        }
88    }
89}
90
91#[derive(Debug, Clone)]
92pub struct Response {
93    pub(crate) status: u16,
94    pub(crate) body: String,
95    pub(crate) x_ratelimit_reset: Option<u64>,
96}
97
98impl Response {
99    pub fn new(status: u16, body: String, x_ratelimit_reset: Option<u64>) -> Self {
100        Self {
101            status,
102            body,
103            x_ratelimit_reset,
104        }
105    }
106}
107
108pub type ResponseResult = Result<Response, String>;
109
110#[async_trait]
111pub trait Client: Debug + Send + Sync + 'static {
112    #[cfg(any(feature = "async", feature = "subscriptions"))]
113    async fn request(&self, request: &Request) -> ResponseResult;
114
115    #[cfg(feature = "sync")]
116    fn request_sync(&self, request: &Request) -> ResponseResult;
117}