animebytes_rs/
lib.rs

1//! # animebytes-rs
2//!
3//! `animebytes-rs` is a crate that provides access to animebytes-tv tracker
4//! api. You need to provide your torrent password to access most of the
5//! endpoints. Prometheus endpoints are not provided however in the future there
6//! is a chance that they could be added if requested.
7
8#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::perf)]
9#![allow(clippy::module_name_repetitions, clippy::no_effect_underscore_binding)]
10use serde::de::DeserializeOwned;
11
12pub mod errors;
13pub mod models;
14
15pub struct Client {
16    torrent_pass: String,
17    username: String,
18    http_client: reqwest::Client,
19}
20
21impl Client {
22    /// Create a new `Client`, providing a torrent password, and username.
23    /// Current checking if credentials are valid is not done on client
24    /// creation.
25    /// # Errors
26    /// This method fails if the http client backend could not be created.
27    pub fn new(torrent_pass: &str, username: &str) -> Result<Self, errors::Error> {
28        Ok(Self {
29            torrent_pass: torrent_pass.into(),
30            username: username.into(),
31            http_client: reqwest::ClientBuilder::new().build()?,
32        })
33    }
34
35    async fn get<T: DeserializeOwned>(&self, params: &str) -> Result<T, errors::Error> {
36        Ok(self.http_client.get(params).send().await?.json().await?)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    #[tokio::test]
43    async fn test_anime_search() {
44        let client = crate::Client::new(&std::env::var("AB_KEY").unwrap(), &std::env::var("AB_USER").unwrap()).unwrap();
45
46        let dto = client.search_anime("sword art online").await.unwrap();
47
48        assert!(dto.groups.iter().any(|x| x.id == 12053));
49    }
50
51    #[tokio::test]
52    async fn test_status() {
53        let client = crate::Client::new(&std::env::var("AB_KEY").unwrap(), &std::env::var("AB_USER").unwrap()).unwrap();
54
55        let dto = client.status().await.unwrap();
56
57        assert!(dto.success);
58    }
59
60    #[tokio::test]
61    async fn test_stats() {
62        let client = crate::Client::new(&std::env::var("AB_KEY").unwrap(), &std::env::var("AB_USER").unwrap()).unwrap();
63
64        let dto = client.stats().await.unwrap();
65
66        assert!(dto.success);
67    }
68}