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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! # boticordrs
//!
//! Crate for [Boticord](https://boticord.top/) API
//! ## Usage
//!
//! Add this to your `Cargo.toml`
//! ```toml
//! [dependencies]
//! boticordrs = "0.1.3"
//! ```
//!
//! ## Example
//!
//! ```no_run
//! use boticordrs::{BoticordClient};
//! use boticordrs::types::{BotStats};
//!
//! #[tokio::main]
//! async fn main() {
//!     let client = BoticordClient::new("your token".to_string(), 2).expect("failed client");
//!
//!     let stats = BotStats {servers: 2514, shards: 3, users: 338250};
//!
//!     match client.post_bot_stats(stats).await {
//!         Ok(_) => {
//!             println!("Well Done!")
//!         },
//!         Err(e) => eprintln!("{}", e),
//!     }
//! }
//! ```
#![doc(html_root_url = "https://docs.rs/boticordrs/0.1.3")]

use reqwest::header::AUTHORIZATION;
use reqwest::{Client as ReqwestClient, Response};
use reqwest::{Method};

macro_rules! api_url {
    ($e:expr) => {
        concat!("https://api.boticord.top/v", $e)
    };
    ($e:expr, $($rest:tt)*) => {
        format!(api_url!($e), $($rest)*)
    };
}

pub mod types;
mod errors;

use types::*;
pub use errors::BoticordError;

/// You can use it to make it much easier to use the Boticord API.
#[derive(Clone)]
pub struct BoticordClient {
    client: ReqwestClient,
    token: String,
    version: u64
}

impl BoticordClient {
    /// Constructs a new Client.
    ///
    /// In BotiCord API v2 there are some changes with token.
    /// [Read more here](https://docs.boticord.top/topics/v1vsv2/)
    ///
    /// # Arguments
    ///
    /// * `token` - Your BotiCord token
    /// * `version` - Version of BotiCord API.
    ///
    pub fn new(token: String, version: u64) -> Result<Self, BoticordError> {
        let client = ReqwestClient::builder().build().map_err(errors::from)?;
        Ok(BoticordClient { client, token, version })
    }

    /// Constructs a new Client with ReqwestClient specified by user.
    ///
    /// In BotiCord API v2 there are some changes with token.
    /// [Read more here](https://docs.boticord.top/topics/v1vsv2/)
    ///
    /// # Arguments
    ///
    /// * `client` - Your custom ReqwestClient
    /// * `token` - Your BotiCord token
    /// * `version` - Version of BotiCord API
    ///
    pub fn new_with_client(client: ReqwestClient, token: String, version: u64) -> Self {
        BoticordClient { client, token, version }
    }

    /// Get information about a specific bot.
    ///
    /// # Arguments
    ///
    /// * `bot` - Id of bot.
    ///
    pub async fn get_bot_info(&self, bot: String) -> Result<Bot, BoticordError> {
        let url = api_url!("{}/bot/{}", &self.version, bot);
        get(self, url).await
    }

    /// Get information about a specific server.
    ///
    /// # Arguments
    ///
    /// * `server` - Id of server.
    ///
    pub async fn get_server_info(&self, server: String) -> Result<Server, BoticordError> {
        let url = api_url!("{}/server/{}", &self.version, server);
        get(self, url).await
    }

    /// Get information about a specific user.
    ///
    /// # Arguments
    ///
    /// * `user` - Id of user.
    ///
    pub async fn get_user_info(&self, user: String) -> Result<UserInformation, BoticordError> {
        let url = api_url!("{}/profile/{}", &self.version, user);
        get(self, url).await
    }

    /// Get Vec of bot's comments.
    ///
    /// # Arguments
    ///
    /// * `bot` - Id of bot.
    ///
    pub async fn get_bot_comments(&self, bot: String) -> Result<Vec<SingleComment>, BoticordError> {
        let url = api_url!("{}/bot/{}/comments", &self.version, bot);
        get(self, url).await
    }

    /// Get Vec of server's comments.
    ///
    /// # Arguments
    ///
    /// * `server` - Id of server.
    ///
    pub async fn get_server_comments(&self,
                                     server: String
    ) -> Result<Vec<SingleComment>, BoticordError> {
        let url = api_url!("{}/server/{}/comments", &self.version, server);
        get(self, url).await
    }

    /// Get Vec of user's comments.
    ///
    /// # Arguments
    ///
    /// * `user` - Id of user.
    ///
    pub async fn get_user_comments(&self,
                                   user: String
    ) -> Result<UserComments, BoticordError> {
        let url = api_url!("{}/profile/{}/comments", &self.version, user);
        get(self, url).await
    }

    /// Get Vec of user's bots.
    ///
    /// # Arguments
    ///
    /// * `user` - Id of user.
    ///
    pub async fn get_user_bots(&self,
                               user: String
    ) -> Result<Vec<SingleUserBot>, BoticordError> {
        let url = api_url!("{}/bots/{}", &self.version, user);
        get(self, url).await
    }

    /// Get Vec of shorted by current user links
    pub async fn get_my_shorted_links(&self) -> Result<Vec<ShortedLink>, BoticordError> {
        let url = api_url!("{}/links/get", &self.version,);
        post_with_response(self, url, Some(EmptyBody{})).await
    }

    /// Get Vec of shorted by current user links with the provided code
    ///
    /// # Arguments
    ///
    /// * `shortener_body` - Short information about a link, that we will search.
    ///
    pub async fn search_for_shorted_link(&self,
                                      shortener_body: ShortenerBody
    ) -> Result<Vec<ShortedLink>, BoticordError> {
        let url = api_url!("{}/links/get", &self.version,);
        post_with_response(self, url, Some(shortener_body)).await
    }

    /// Creates new shorted link
    ///
    /// # Arguments
    ///
    /// * `shortener_body` - Information about link we will create.
    ///
    pub async fn create_shorted_link(&self,
                                      shortener_body: ShortenerBody
    ) -> Result<ShortedLink, BoticordError> {
        let url = api_url!("{}/links/create", &self.version,);
        post_with_response(self, url, Some(shortener_body)).await
    }

    /// Deletes shorted link
    ///
    /// # Arguments
    ///
    /// * `shortener_body` - Information about link we will delete.
    ///
    pub async fn delete_shorted_link(&self,
                                     shortener_body: ShortenerBody
    ) -> Result<(), BoticordError> {
        let url = api_url!("{}/links/delete", &self.version);
        post(self, url, Some(shortener_body)).await
    }


    /// Post current bot's stats.
    /// # How to set BotStats? (example)
    ///
    /// # Arguments
    ///
    /// * `stats` - Stats that we will post
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use boticordrs::types::{BotStats};
    ///
    /// let stats = BotStats{servers: 2514, shards: 3, users: 338250};
    /// ```
    pub async fn post_bot_stats(&self, stats: BotStats) -> Result<(), BoticordError> {
        let url = api_url!("{}/stats", &self.version);
        post(self, url, Some(stats)).await
    }

    /// Post Server Stats Method.
    ///
    /// Remember, that only Boticord-Service Bots can do it in global,
    /// other will get an 403 error.
    /// (but it may works for custom bots, but you need a special API-token)
    ///
    ///
    /// # Arguments
    ///
    /// * `stats` - Stats that we will post
    ///
    pub async fn post_server_stats(&self, stats: ServerStats) -> Result<(), BoticordError> {
        let url = api_url!("{}/server ", &self.version);
        post(self, url, Some(stats)).await
    }
}

async fn request<T>(
    client: &BoticordClient,
    method: Method,
    url: String,
    data: Option<T>,
) -> Result<Response, BoticordError>
    where
        T: serde::Serialize + Sized,
{

    let mut req = client
        .client
        .request(method, &url)
        .header(AUTHORIZATION, &client.token);

    if let Some(data) = data {
        req = req.json(&data);
    }

    let resp = match req.send().await {
        Ok(resp) => resp,
        Err(e) => return Err(errors::from(e)),
    };
    match resp.status() {
        _ => resp.error_for_status().map_err(errors::from),
    }
}

async fn get<T>(client: &BoticordClient, url: String) -> Result<T, BoticordError>
    where
        T: serde::de::DeserializeOwned + Sized,
{
    let resp = request(client, Method::GET, url, None::<()>).await?;
    match resp.json().await {
        Ok(data) => Ok(data),
        Err(e) => Err(errors::from(e)),
    }
}


async fn post<T>(client: &BoticordClient,
                 url: String,
                 data: Option<T>) -> Result<(), BoticordError>
    where
        T: serde::Serialize + Sized,
{
    request(client, Method::POST, url, data).await?;
    Ok(())
}

async fn post_with_response<T, R>(client: &BoticordClient,
                    url: String,
                    data: Option<T>) -> Result<R, BoticordError>
    where
        T: serde::Serialize + Sized,
        R: serde::de::DeserializeOwned + Sized,
{
    let resp = request(client, Method::POST, url, data).await?;
    match resp.json().await {
        Ok(data) => Ok(data),
        Err(e) => Err(errors::from(e)),
    }
}