Skip to main content

torbox_general_rs/
lib.rs

1use reqwest::Method;
2use torbox_core_rs::{
3    api::ApiResponse,
4    client::{Endpoint, TorboxClient},
5    data::general::{ChangelogJsonVersion, SpeedtestFile, TorboxGeneralStats},
6    error::ApiError,
7    network::constants::CONTENT_XML,
8};
9
10use crate::{
11    endpoint::{
12        GetChangelogJsonVersionsEp, GetChangelogRssFeedEp, GetSpeedtestFilesEp, GetStatsEp,
13        GetUpStatusEp,
14    },
15    query::SpeedTestQuery,
16    types::FileLength,
17};
18
19pub mod endpoint;
20pub mod query;
21pub mod tests;
22pub mod types;
23
24/// Main interface for TorBox general operations
25///
26/// Provides methods for all general API calls including:
27/// - Up status
28/// - Changelogs in both RSS and JSON
29/// - Test files
30#[cfg_attr(feature = "specta", derive(specta::Type))]
31pub struct GeneralApi<'a> {
32    client: &'a TorboxClient,
33}
34
35impl<'a> GeneralApi<'a> {
36    pub fn new(client: &'a TorboxClient) -> Self {
37        Self { client }
38    }
39
40    pub async fn get_up_status(&self) -> Result<ApiResponse<()>, ApiError> {
41        let tmp_client = self.client.with_base_url("https://api.torbox.app");
42        tmp_client.request_with_query(Method::GET, "", &()).await
43    }
44
45    pub async fn get_stats(&self) -> Result<ApiResponse<TorboxGeneralStats>, ApiError> {
46        Endpoint::<GetStatsEp>::new(self.client)
47            .call_query(())
48            .await
49    }
50
51    pub async fn get_changelog_rss_feed(&self) -> Result<String, ApiError> {
52        Endpoint::<GetChangelogRssFeedEp>::new(self.client)
53            .call_query_raw((), CONTENT_XML)
54            .await
55    }
56
57    pub async fn get_changelog_json_versions(
58        &self,
59    ) -> Result<ApiResponse<Vec<ChangelogJsonVersion>>, ApiError> {
60        Endpoint::<GetChangelogJsonVersionsEp>::new(self.client)
61            .call_query(())
62            .await
63    }
64
65    pub async fn get_speedtest_files(
66        &self,
67        test_length: Option<FileLength>,
68        region: Option<String>,
69    ) -> Result<ApiResponse<Vec<SpeedtestFile>>, ApiError> {
70        Endpoint::<GetSpeedtestFilesEp>::new(self.client)
71            .call_query(SpeedTestQuery {
72                test_length,
73                region,
74            })
75            .await
76    }
77}