1use std::sync::Arc;
2use reqwest::Client;
3use serde::Serialize;
4
5pub struct Http {
6 client: Arc<Client>,
7 token: String,
8}
9
10impl Http {
11 pub fn new(token: String) -> Self {
12 Http {
13 client: Arc::new(Client::new()),
14 token,
15 }
16 }
17
18 pub async fn get<T: for<'de> serde::Deserialize<'de>>(&self, endpoint: &str) -> Result<T, reqwest::Error> {
19 let url = format!("https://discord.com/api/v10{}", endpoint);
20 let res = self.client
21 .get(&url)
22 .header("authorization", format!("Bot {}", self.token))
23 .send()
24 .await?
25 .json::<T>()
26 .await?;
27 Ok(res)
28 }
29
30 pub async fn post<T: for<'de> serde::Deserialize<'de>, J: Serialize + ?Sized>(&self, endpoint: &str, body: &J) -> Result<T, reqwest::Error> {
31 let url = format!("https://discord.com/api/v10{}", endpoint);
32 let res = self.client
33 .post(&url)
34 .header("authorization", format!("Bot {}", self.token))
35 .json(body)
36 .send()
37 .await?
38 .json::<T>()
39 .await?;
40 Ok(res)
41 }
42
43 pub async fn patch<T: for<'de> serde::Deserialize<'de>, J: Serialize + ?Sized>(&self, endpoint: &str, body: &J) -> Result<T, reqwest::Error> {
44 let url = format!("https://discord.com/api/v10{}", endpoint);
45 let res = self.client
46 .patch(&url)
47 .header("authorization", format!("Bot {}", self.token))
48 .json(body)
49 .send()
50 .await?
51 .json::<T>()
52 .await?;
53 Ok(res)
54 }
55
56 pub async fn delete<T: for<'de> serde::Deserialize<'de>>(&self, endpoint: &str) -> Result<T, reqwest::Error> {
57 let url = format!("https://discord.com/api/v10{}", endpoint);
58 let res = self.client
59 .delete(&url)
60 .header("authorization", format!("Bot {}", self.token))
61 .send()
62 .await?
63 .json::<T>()
64 .await?;
65 Ok(res)
66 }
67}