1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
pub mod blocking; pub mod model; const BASE_URL: &str = "https://koreanbots.dev/api/v2"; use std::collections::HashMap; use model::UserInfo; use reqwest::{ header::HeaderMap, header::{AUTHORIZATION, CONTENT_TYPE}, Url, }; use crate::model::{ response::{Data, Response, ResponseUpdate}, Bot, VoteCheck, WidgetQuery, WidgetType, }; pub struct Client { authorization: &'static str, } impl Client { pub fn new(authorization: &'static str) -> Self { Self { authorization } } pub async fn get_bot(&self, bot_id: &str) -> Response<Bot> { let mut url = String::from("bots/"); url.push_str(bot_id); let fetch = Client::_fetch("GET", &url, None, None, None); let json: Response<Bot> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn search_bot(&self, search: &str, page: Option<usize>) -> Response<Data<Bot>> { let mut query = HashMap::new(); query.insert("query", search.to_string()); if let Some(page) = page { query.insert("page", page.to_string()); } else { query.insert("page", "1".to_string()); } let fetch = Client::_fetch("GET", "search/bots", None, Some(query), None); let json: Response<Data<Bot>> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn get_votes_list(&self, page: Option<usize>) -> Response<Data<Bot>> { let mut query = HashMap::new(); if let Some(page) = page { query.insert("page", page.to_string()); } else { query.insert("page", "1".to_string()); } let fetch = Client::_fetch("GET", "list/bots/votes", None, Some(query), None); let json: Response<Data<Bot>> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn get_new_bot_list(&self) -> Response<Data<Bot>> { let fetch = Client::_fetch("GET", "list/bots/new", None, None, None); let json: Response<Data<Bot>> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn get_vote_check(&self, bot_id: &str, user_id: &str) -> Response<VoteCheck> { let mut header = HeaderMap::new(); header.insert(AUTHORIZATION, self.authorization.parse().unwrap()); header.insert(CONTENT_TYPE, "application/json".parse().unwrap()); let mut query = HashMap::new(); query.insert("userID", user_id.to_string()); let mut url = String::from("bots/"); url.push_str(bot_id); url.push_str("/vote"); let fetch = Client::_fetch("GET", &url, Some(header), Some(query), None); let json: Response<VoteCheck> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn update( &self, bot_id: &str, servers: Option<usize>, shards: Option<usize>, ) -> ResponseUpdate { let mut header = HeaderMap::new(); header.insert(AUTHORIZATION, self.authorization.parse().unwrap()); header.insert(CONTENT_TYPE, "application/json".parse().unwrap()); let mut json = HashMap::new(); if let Some(servers) = servers { json.insert("servers", servers.to_string()); } else if let Some(shards) = shards { json.insert("shards", shards.to_string()); } let mut url = String::from("bots/"); url.push_str(bot_id); url.push_str("/stats"); let fetch = Client::_fetch("POST", &url, Some(header), None, Some(json)); let json: ResponseUpdate = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn get_user(&self, user_id: &str) -> Response<UserInfo> { let mut url = String::from("users/"); url.push_str(user_id); let fetch = Client::_fetch("GET", &url, None, None, None); let json: Response<UserInfo> = serde_json::from_str(&fetch.await.text().await.unwrap()).unwrap(); json } pub async fn get_bot_widget( &self, bot_id: &str, widget_type: WidgetType, widget_query: Option<WidgetQuery>, ) -> String { let query = { if let Some(widget_query) = widget_query { let mut query: HashMap<&str, String> = HashMap::new(); query.insert("style", widget_query.to_style().to_string()); query.insert("icon", widget_query.to_icon().to_string()); query.insert("scale", widget_query.to_scale().to_string()); Some(query) } else { None } }; let mut url = String::from("widget/bots/"); url.push_str(&widget_type.to_string()); url.push('/'); url.push_str(bot_id); url.push_str(".svg"); let fetch = Client::_fetch("GET", &url, None, query, None); fetch.await.url().to_string() } async fn _fetch( method: &str, path: &str, headers: Option<HeaderMap>, query: Option<HashMap<&str, String>>, json: Option<HashMap<&str, String>>, ) -> reqwest::Response { let client = reqwest::Client::new(); let url = { let path_url = format!("{}/{}", BASE_URL, path); if let Some(query) = query { Url::parse_with_params(&path_url, query) .unwrap() .to_string() } else { path_url } }; match method { "GET" => { if let Some(headers) = headers { if let Some(json) = json { client .get(url) .headers(headers) .json(&json) .send() .await .unwrap() } else { client.get(url).headers(headers).send().await.unwrap() } } else if let Some(json) = json { client.get(url).json(&json).send().await.unwrap() } else { client.get(url).send().await.unwrap() } } "POST" => { if let Some(headers) = headers { if let Some(json) = json { client .post(url) .headers(headers) .json(&json) .send() .await .unwrap() } else { client.post(url).headers(headers).send().await.unwrap() } } else if let Some(json) = json { client.post(url).json(&json).send().await.unwrap() } else { client.post(url).send().await.unwrap() } } _ => panic!(""), } } }