Skip to main content

torbox_user_rs/
lib.rs

1use torbox_core_rs::{
2    api::ApiResponse,
3    client::{Endpoint, TorboxClient},
4    data::user::{
5        DeviceCodeAuth, ReferralData, SearchEngineData, SessionToken, SubscriptionData,
6        TransactionData, UserProfile,
7    },
8    error::ApiError,
9};
10
11use crate::{
12    body::{RefreshApiTokenBody, SearchEngineControlBody, SearchEngineEditBody},
13    endpoint::{
14        AddReferalPostEp, DeviceCodeAuthGetEp, GetConfirmationCodeGetEp, GetUserDataGetEp,
15        ReferralDataGetEp, RefreshApiTokenPostEp, SearchEngineControlPostEp, SearchEngineDataGetEp,
16        SearchEngineEditPostEp, SubscriptionDataGetEp, TransactionDataGetEp,
17    },
18    query::{AddReferralQuery, DeviceCodeAuthQuery, SearchEngineDataQuery, UserDataQuery},
19};
20
21pub mod body;
22pub mod endpoint;
23pub mod query;
24pub mod tests;
25pub mod types;
26
27/// Main interface for TorBox User operations
28///
29/// Provides methods for all general API calls including:
30/// - Retrieving user data
31/// - Adding referal links
32/// - Refreshing APIs
33#[cfg_attr(feature = "specta", derive(specta::Type))]
34pub struct UserApi<'a> {
35    client: &'a TorboxClient,
36}
37
38impl<'a> UserApi<'a> {
39    pub fn new(client: &'a TorboxClient) -> Self {
40        Self { client }
41    }
42
43    /// Not done
44    pub async fn refresh_api_token(
45        &self,
46        token: String,
47        expires_at: u64,
48    ) -> Result<ApiResponse<String>, ApiError> {
49        Endpoint::<RefreshApiTokenPostEp>::new(self.client)
50            .call_json(RefreshApiTokenBody {
51                session_token: SessionToken { token, expires_at },
52            })
53            .await
54    }
55
56    ///
57    /// ## Overview
58    ///  Gets a users account data and information.
59    ///
60    pub async fn get_data(
61        &self,
62        enable_settings: bool,
63    ) -> Result<ApiResponse<UserProfile>, ApiError> {
64        Endpoint::<GetUserDataGetEp>::new(self.client)
65            .call_query(UserDataQuery {
66                settings: enable_settings,
67            })
68            .await
69    }
70
71    pub async fn add_referral_code(&self, referral: String) -> Result<ApiResponse<()>, ApiError> {
72        Endpoint::<AddReferalPostEp>::new(self.client)
73            .call_query(AddReferralQuery { referral })
74            .await
75    }
76
77    pub async fn get_confirmation_code(&self) -> Result<ApiResponse<()>, ApiError> {
78        Endpoint::<GetConfirmationCodeGetEp>::new(self.client)
79            .call_query(())
80            .await
81    }
82
83    pub async fn start_device_code_auth(
84        &self,
85        app_name: String,
86    ) -> Result<ApiResponse<DeviceCodeAuth>, ApiError> {
87        Endpoint::<DeviceCodeAuthGetEp>::new(self.client)
88            .call_query(DeviceCodeAuthQuery { app: app_name })
89            .await
90    }
91
92    pub async fn get_referral_data(&self) -> Result<ApiResponse<ReferralData>, ApiError> {
93        Endpoint::<ReferralDataGetEp>::new(self.client)
94            .call_query(())
95            .await
96    }
97
98    pub async fn get_subscription(&self) -> Result<ApiResponse<SubscriptionData>, ApiError> {
99        Endpoint::<SubscriptionDataGetEp>::new(self.client)
100            .call_query(())
101            .await
102    }
103
104    pub async fn get_transactions(&self) -> Result<ApiResponse<Vec<TransactionData>>, ApiError> {
105        Endpoint::<TransactionDataGetEp>::new(self.client)
106            .call_query(())
107            .await
108    }
109
110    pub async fn get_search_engines(
111        &self,
112        id: u32,
113    ) -> Result<ApiResponse<Vec<SearchEngineData>>, ApiError> {
114        Endpoint::<SearchEngineDataGetEp>::new(self.client)
115            .call_query(SearchEngineDataQuery { id })
116            .await
117    }
118
119    pub async fn control_search_engines(
120        &self,
121        body: SearchEngineControlBody,
122    ) -> Result<ApiResponse<()>, ApiError> {
123        Endpoint::<SearchEngineControlPostEp>::new(self.client)
124            .call_json(body)
125            .await
126    }
127
128    pub async fn edit_search_engines(
129        &self,
130        body: SearchEngineEditBody,
131    ) -> Result<ApiResponse<SearchEngineData>, ApiError> {
132        Endpoint::<SearchEngineEditPostEp>::new(self.client)
133            .call_json(body)
134            .await
135    }
136}