coc-rs 0.8.4

A Rust crate wrapper around the Clash of Clans public API
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use std::sync::{
    atomic::{AtomicBool, AtomicUsize, Ordering},
    Arc,
};

use async_recursion::async_recursion;
use dashmap::DashMap;
use parking_lot::Mutex;
use reqwest::{RequestBuilder, Url};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[cfg(feature = "cos")]
use reqwest::header::{HeaderMap, HeaderValue};

use crate::{
    credentials::{Credential, Credentials},
    dev::{self, CLIENT},
    error::APIError,
    models::{
        clan, clan_capital, clan_search, gold_pass, labels, leagues, location, paging, player,
        rankings, season, war, war_log,
    },
    util::LogicLong,
};

#[derive(Clone, Debug, Default)]
pub struct Client {
    ready: Arc<AtomicBool>,
    pub(crate) accounts: Arc<DashMap<Credential, dev::APIAccount>>,

    account_index: Arc<AtomicUsize>,
    key_index: Arc<AtomicUsize>,

    ip_address: Arc<Mutex<String>>,

    #[cfg(feature = "cos")]
    pub(crate) is_cos_logged_in: Arc<AtomicBool>,
}

impl Client {
    const BASE_URL: &'static str = "https://api.clashofclans.com/v1";

    /// Returns a [`Client`]
    ///
    /// # Errors
    ///
    /// This function will return an error if the credentials are invalid
    pub async fn new(credentials: Credentials) -> anyhow::Result<Self> {
        let client = Self {
            ready: Arc::new(AtomicBool::new(false)),

            accounts: Arc::new(DashMap::new()),

            account_index: Arc::new(AtomicUsize::new(0)),
            key_index: Arc::new(AtomicUsize::new(0)),

            ip_address: Arc::new(Mutex::new(String::new())),

            #[cfg(feature = "cos")]
            is_cos_logged_in: Arc::new(AtomicBool::new(false)),
        };

        client.init(credentials).await?;
        client.ready.store(true, Ordering::Relaxed);
        Ok(client)
    }

    /// Called when the client is created to initialize every credential.
    async fn init(&self, credentials: Credentials) -> anyhow::Result<()> {
        let tasks = credentials.0.into_iter().map(dev::APIAccount::login);

        let accounts =
            futures::future::join_all(tasks).await.into_iter().collect::<Result<Vec<_>, _>>()?;

        *self.ip_address.lock() = accounts[0].1.clone();

        for (account, _) in accounts {
            self.accounts.insert(account.credential.clone(), account);
        }

        Ok(())
    }

    /// Called when an IP address change is detected
    pub(crate) async fn reinit(&self) -> anyhow::Result<()> {
        #[cfg(feature = "tracing")]
        tracing::debug!("reinitializing client");

        self.ready.store(false, Ordering::Relaxed);

        let accounts = self.accounts.iter().map(|account| account.clone()).collect::<Vec<_>>();

        for mut account in accounts {
            account.re_login().await?;

            // update the account in the DashMap
            self.accounts.insert(account.credential.clone(), account);
        }

        self.ready.store(true, Ordering::Relaxed);

        Ok(())
    }

    /// Here you can create a client yourself and load them here later (for example .env parsing)
    ///
    /// # Errors
    ///
    /// This function will return an error if the credentials are invalid
    ///
    /// # Example
    /// ```no_run
    /// use coc_rs::{api::Client, credentials::Credentials};
    ///
    /// #[tokio::main]
    /// async fn main() -> anyhow::Result<()> {
    ///     let client = Client::new(None);
    ///     let credentials = Credentials::builder()
    ///         .add_credential("email", "password")
    ///         .add_credential("email2", "password2")
    ///         .build();
    ///     client.load(credentials).await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn load(&self, credentials: Credentials) -> anyhow::Result<()> {
        #[cfg(feature = "tracing")]
        tracing::trace!(credentials = ?credentials, "Loading credentials");

        self.ready.store(false, Ordering::Relaxed);
        self.init(credentials).await?;
        self.ready.store(true, Ordering::Relaxed);
        Ok(())
    }

    /// This is purely for diagnostics, it's not used anywhere else.
    ///
    /// # Example
    /// ```no_run
    /// use coc_rs::Client;
    ///
    /// let credentials = Credentials::builder()
    ///     .add_credential("email", "password")
    ///     .add_credential("email2", "password2")
    ///     .build();
    /// let client = Client::new(credentials);
    /// client.debug_keys().await;
    /// ```
    #[cfg(feature = "tracing")]
    pub fn debug_keys(&self) {
        self.accounts.iter().for_each(|account| {
            account.keys.keys.iter().for_each(|key| {
                tracing::debug!(key = %key.key, key.id=%key.id, key.name=%key.name);
            });
        });
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                       HTTP Methods                       │
    //         ╰──────────────────────────────────────────────────────────╯

    pub(crate) fn get<U: reqwest::IntoUrl>(
        &self,
        url: U,
    ) -> Result<reqwest::RequestBuilder, APIError> {
        if !self.ready.load(Ordering::Relaxed) {
            return Err(APIError::ClientNotReady);
        }
        Ok(CLIENT.get(url).bearer_auth(self.get_next_key()))
    }

    pub(crate) fn post<U: reqwest::IntoUrl, T: Into<reqwest::Body>>(
        &self,
        url: U,
        body: T,
    ) -> Result<reqwest::RequestBuilder, APIError> {
        if !self.ready.load(Ordering::Relaxed) {
            return Err(APIError::ClientNotReady);
        }
        Ok(CLIENT.post(url).bearer_auth(self.get_next_key()).body(body))
    }

    /// To allow usage without a client being ready
    #[cfg(feature = "cos")]
    pub(crate) fn cos_get<U: reqwest::IntoUrl>(
        &self,
        url: U,
    ) -> Result<reqwest::RequestBuilder, APIError> {
        let mut headers = HeaderMap::new();
        headers.insert("authority", HeaderValue::from_str("api.clashofstats.com")?);
        headers.insert("method", HeaderValue::from_str("GET")?);
        headers.insert("scheme", HeaderValue::from_str("https")?);
        headers.insert(
            "accept",
            HeaderValue::from_str("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")?,
        );
        headers.insert(
            "accept-language",
            HeaderValue::from_str("en-US,en;q=0.9,zh-CN;q=0.8,z;q=0.7")?,
        );
        headers.insert(
            "sec-ch-ua",
            HeaderValue::from_str(
                "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
            )?,
        );
        headers.insert("sec-ch-ua-platform", HeaderValue::from_str("\"Windows\"")?);
        headers.insert("upgrade-insecure-requests", HeaderValue::from_str("1")?);
        headers.insert(
            "user-agent",
            HeaderValue::from_str("Mozilla/5.0 (X11; Windows x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")?,
        );

        Ok(CLIENT.get(url).headers(headers))
    }

    /// To allow usage without a client being ready
    #[cfg(feature = "cos")]
    pub(crate) fn cos_post<U: reqwest::IntoUrl, T: Into<reqwest::Body>>(
        &self,
        url: U,
        body: T,
    ) -> Result<reqwest::RequestBuilder, APIError> {
        let mut headers = HeaderMap::new();
        headers.insert("authority", HeaderValue::from_str("api.clashofstats.com")?);
        headers.insert("method", HeaderValue::from_str("POST")?);
        headers.insert("scheme", HeaderValue::from_str("https")?);
        headers.insert("accept", HeaderValue::from_str("application/json, text/plain, */*")?);
        headers.insert("accept-encoding", HeaderValue::from_str("gzip, deflate, br")?);
        headers.insert(
            "accept-language",
            HeaderValue::from_str("en-US,en;q=0.9,zh-CN;q=0.8,z;q=0.7")?,
        );
        headers.insert("content-type", HeaderValue::from_str("application/json;charset=UTF-8")?);
        headers.insert(
            "sec-ch-ua",
            HeaderValue::from_str(
                "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
            )?,
        );
        headers.insert("sec-ch-ua-platform", HeaderValue::from_str("\"Windows\"")?);
        headers.insert("upgrade-insecure-requests", HeaderValue::from_str("1")?);
        headers.insert(
            "user-agent",
            HeaderValue::from_str("Mozilla/5.0 (X11; Windows x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")?,
        );

        Ok(CLIENT.post(url).body(body).headers(headers))
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                       Clan Methods                       │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clan_warlog(
        &self,
        clan_tag: &str,
    ) -> Result<APIResponse<war_log::WarLog>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan_warlog({})", clan_tag);
        let clan_tag = clan_tag.parse::<LogicLong>()?.to_string();
        let url = format!("{}/clans/{}/warlog", Self::BASE_URL, urlencoding::encode(&clan_tag));
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clans(
        &self,
        options: clan_search::ClanSearchOptions,
    ) -> Result<APIResponse<clan::Clan>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clans({})", options);
        let url = Url::parse_with_params(&format!("{}/clans", Self::BASE_URL), options.items)?;
        self.parse_json(self.get(url.to_string()), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_current_war(&self, clan_tag: &str) -> Result<war::War, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_current_war({})", clan_tag);
        let clan_tag = clan_tag.parse::<LogicLong>()?.to_string();
        let url = format!("{}/clans/{}/currentwar", Self::BASE_URL, urlencoding::encode(&clan_tag));
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clan(&self, clan_tag: &str) -> Result<clan::Clan, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan({})", clan_tag);
        let clan_tag = clan_tag.parse::<LogicLong>()?.to_string();
        let url = format!("{}/clans/{}", Self::BASE_URL, urlencoding::encode(&clan_tag));
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clan_members(
        &self,
        clan_tag: &str,
    ) -> Result<APIResponse<clan::ClanMember>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan_members({})", clan_tag);
        let clan_tag = clan_tag.parse::<LogicLong>()?.to_string();
        let url = format!("{}/clans/{}/members", Self::BASE_URL, urlencoding::encode(&clan_tag));
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clan_capital_raid_seasons(
        &self,
        clan_tag: &str,
    ) -> Result<APIResponse<clan_capital::ClanCapitalRaidSeason>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan_capital_raid_seasons({})", clan_tag);
        let clan_tag = clan_tag.parse::<LogicLong>()?.to_string();
        let url = format!(
            "{}/clans/{}/capitalraidseasons",
            Self::BASE_URL,
            urlencoding::encode(&clan_tag)
        );
        self.parse_json(self.get(url), false).await
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                      Player Methods                      │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_player(&self, player_tag: &str) -> Result<player::Player, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_player({})", player_tag);
        let player_tag = player_tag.parse::<LogicLong>()?.to_string();
        let url = format!("{}/players/{}", Self::BASE_URL, urlencoding::encode(&player_tag));
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn verify_player_token(
        &self,
        player_tag: &str,
        token: &str,
    ) -> Result<player::PlayerToken, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("verify_player_token({}, {})", player_tag, token);
        let player_tag = player_tag.parse::<LogicLong>()?.to_string();
        let url =
            format!("{}/players/{}/verifytoken", Self::BASE_URL, urlencoding::encode(&player_tag));
        let token = format!("{{\"token\":\"{token}\"}}");
        self.parse_json(self.post(url, token), false).await
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                      League Methods                      │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_leagues(&self) -> Result<APIResponse<leagues::League>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_leagues()");
        let url = format!("{}/leagues", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_league_season_rankings(
        &self,
        league_id: leagues::LeagueKind,
        season_id: season::Season,
        paging: paging::Paging,
    ) -> Result<APIResponse<rankings::PlayerRanking>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_league_season_rankings({}, {}, {})", league_id, season_id, paging);
        if league_id != leagues::LeagueKind::LegendLeague {
            return Err(APIError::InvalidParameters(
                "This league does not have seasons, only League::LegendLeague has seasons"
                    .to_string(),
            ));
        }
        let mut url =
            format!("{}/leagues/{}/seasons/{season_id}", Self::BASE_URL, league_id as i32);
        if paging.is_some() {
            url = Url::parse_with_params(&url, paging.to_vec())?.to_string();
        }
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_league(
        &self,
        league_id: leagues::LeagueKind,
    ) -> Result<leagues::League, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_league({})", league_id);
        let url = format!("{}/leagues/{}", Self::BASE_URL, league_id as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_league_seasons(
        &self,
        league_id: leagues::LeagueKind,
    ) -> Result<APIResponse<season::Season>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_league_seasons({})", league_id);
        if league_id != leagues::LeagueKind::LegendLeague {
            return Err(APIError::InvalidParameters(
                "This league does not have seasons, only League::LegendLeague has seasons"
                    .to_string(),
            ));
        }
        let url = format!("{}/leagues/{}/seasons", Self::BASE_URL, league_id as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_war_league(
        &self,
        war_league: leagues::WarLeagueKind,
    ) -> Result<leagues::WarLeague, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_war_league({})", war_league);
        let url = format!("{}/warleagues/{}", Self::BASE_URL, war_league as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_war_leagues(&self) -> Result<APIResponse<leagues::WarLeague>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_war_leagues()");
        let url = format!("{}/warleagues", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                     Location Methods                     │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_clan_rankings(
        &self,
        location: location::Local,
    ) -> Result<APIResponse<rankings::ClanRanking>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan_rankings({})", location);
        let url = format!("{}/locations/{}/rankings/clans", Self::BASE_URL, location as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_player_rankings(
        &self,
        location: location::Local,
    ) -> Result<APIResponse<rankings::PlayerRanking>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_player_rankings({})", location);
        let url = format!("{}/locations/{}/rankings/players", Self::BASE_URL, location as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_versus_clan_rankings(
        &self,
        location: location::Local,
    ) -> Result<APIResponse<rankings::ClanRanking>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_versus_clan_rankings({})", location);
        let url = format!("{}/locations/{}/rankings/clans-versus", Self::BASE_URL, location as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_versus_player_rankings(
        &self,
        location: location::Local,
    ) -> Result<APIResponse<rankings::PlayerVersusRanking>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_versus_player_rankings({})", location);
        let url =
            format!("{}/locations/{}/rankings/players-versus", Self::BASE_URL, location as i32);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_locations(&self) -> Result<APIResponse<location::Location>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_locations()");
        let url = format!("{}/locations", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_location(
        &self,
        location: location::Local,
    ) -> Result<location::Location, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_location({})", location);
        let url = format!("{}/locations/{}", Self::BASE_URL, location as i32);
        self.parse_json(self.get(url), false).await
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                     Gold Pass Method                     │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails
    pub async fn get_goldpass(&self) -> Result<gold_pass::GoldPass, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_goldpass()");
        let url = format!("{}/goldpass/seasons/current", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    //         ╭──────────────────────────────────────────────────────────╮
    //         │                      Label Methods                       │
    //         ╰──────────────────────────────────────────────────────────╯

    /// # Errors
    ///
    /// This function will return an error if the request fails.
    pub async fn get_player_labels(&self) -> Result<APIResponse<labels::PlayerLabel>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_player_labels()");
        let url = format!("{}/labels/players", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    /// # Errors
    ///
    /// This function will return an error if the request fails.
    pub async fn get_clan_labels(&self) -> Result<APIResponse<labels::ClanLabel>, APIError> {
        #[cfg(feature = "tracing")]
        tracing::trace!("get_clan_labels()");
        let url = format!("{}/labels/clans", Self::BASE_URL);
        self.parse_json(self.get(url), false).await
    }

    /// Runs the future that implements `Send` and parses the reqwest response into an
    /// `APIResponse`.
    ///
    /// # Panics
    ///
    /// Panics if the JSON parsing fails for some odd reason. This is a bug and should be reported.
    ///
    /// # Errors
    ///
    /// This function will return an error if the request fails.
    #[async_recursion]
    pub(crate) async fn parse_json<T: DeserializeOwned>(
        &self,
        rb: Result<RequestBuilder, APIError>,
        is_retry_and_not_cos: bool,
    ) -> Result<T, APIError> {
        match rb {
            Ok(rb) => {
                let cloned_rb = rb.try_clone();
                match rb.send().await {
                    Ok(resp) => {
                        match resp.status() {
                            reqwest::StatusCode::OK => {
                                let text = resp.text().await?;
                                Ok(serde_json::from_str(&text).unwrap_or_else(|e| panic!("Failure parsing json (please file a bug on the GitHub): {text}\nError: {e}")))
                            }
                            // 400
                            reqwest::StatusCode::BAD_REQUEST => Err(APIError::BadParameters),
                            // 403 - likely means the IP address has changed, let's reinit the
                            // client then and try this again
                            reqwest::StatusCode::FORBIDDEN => {
                                if is_retry_and_not_cos {
                                    #[cfg(feature = "tracing")]
                                    tracing::debug!("403 Forbidden, but already retried, try checking your credentials?");
                                    Err(APIError::AccessDenied)
                                } else {
                                    if let Err(e) = self.reinit().await {
                                        return Err(APIError::LoginFailed(e.to_string()));
                                    }
                                    if let Some(rb) = cloned_rb {
                                        self.parse_json(Ok(rb), true).await
                                    } else {
                                        Err(APIError::AccessDenied)
                                    }
                                }
                            }
                            // 404
                            reqwest::StatusCode::NOT_FOUND => Err(APIError::NotFound),
                            // 429
                            reqwest::StatusCode::TOO_MANY_REQUESTS => {
                                Err(APIError::RequestThrottled)
                            }
                            // 500
                            reqwest::StatusCode::INTERNAL_SERVER_ERROR => {
                                Err(APIError::UnknownError)
                            }
                            // 503
                            reqwest::StatusCode::SERVICE_UNAVAILABLE => {
                                Err(APIError::InMaintenance)
                            }
                            // edge cases
                            _ => {
                                let status = resp.status();
                                #[cfg(feature = "tracing")]
                                tracing::debug!("Unknown status code: {}", status);
                                Err(APIError::BadResponse(resp.text().await?, status))
                            }
                        }
                    }
                    Err(e) => Err(APIError::RequestFailed(e)),
                }
            }
            Err(e) => Err(e),
        }
    }

    fn get_next_key(&self) -> String {
        // increment key_token_index, unless it would be larger than the account's token size (10),
        // then reset to 0 and increment key_account_index

        let mut account_index = self.account_index.load(Ordering::Relaxed);
        let mut key_index = self.key_index.load(Ordering::Relaxed);

        let accounts = self.accounts.iter().collect::<Vec<_>>();
        let size_of_keys = accounts[account_index].keys.len().min(10);

        // if we're at the end of this account's keys..
        if key_index == size_of_keys - 1 {
            // reset token index anyways
            key_index = 0;
            // ..and at the end of the accounts
            if account_index == (accounts.len() - 1) {
                // then we've reached end of accounts, go back to first account
                account_index = 0;
            } else {
                // otherwise, just increment account index
                account_index += 1;
            }
        } else {
            // otherwise, just increment token index
            key_index += 1;
        }

        let token = accounts
            .get(account_index)
            .unwrap_or_else(|| {
                #[cfg(feature = "tracing")]
                tracing::warn!("No account found at index {account_index}");
                panic!("No account found at index {account_index}")
            })
            .keys
            .keys
            .get(key_index)
            .unwrap_or_else(|| {
                #[cfg(feature = "tracing")]
                tracing::warn!("No key found at index {key_index}");
                panic!("No key found at index {key_index}");
            })
            .clone();

        self.account_index.store(account_index, Ordering::Relaxed);
        self.key_index.store(key_index, Ordering::Relaxed);

        token.key
    }
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct APIResponse<T> {
    pub items: Vec<T>,
    pub paging: paging::Paging,
}