Skip to content

Commit

Permalink
refactor: make MarzbanAPIClient have an inner MarzbanAPIClientRef, ma…
Browse files Browse the repository at this point in the history
…king it an arc internally allowing for cheap cloning.
  • Loading branch information
nulluser0 committed Dec 4, 2024
1 parent 7977913 commit e96bc4d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 59 deletions.
14 changes: 7 additions & 7 deletions src/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl MarzbanAPIClient {
&self,
auth: &BodyAdminTokenApiAdminTokenPost,
) -> Result<Token, ApiError> {
let url = format!("{}/api/admin/token", self.base_url);
let url = format!("{}/api/admin/token", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -65,7 +65,7 @@ impl MarzbanAPIClient {
auth: &BodyAdminTokenApiAdminTokenPost,
) -> Result<(), ApiError> {
let token = self.admin_token(auth).await?;
let mut token_lock = self.token.write().await;
let mut token_lock = self.inner.token.write().await;
*token_lock = Some(token.access_token);
Ok(())
}
Expand All @@ -74,7 +74,7 @@ impl MarzbanAPIClient {
///
/// Retrieve the current authenticated admin.
pub async fn get_current_admin(&self) -> Result<Admin, ApiError> {
let url = format!("{}/api/admin", self.base_url);
let url = format!("{}/api/admin", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -101,7 +101,7 @@ impl MarzbanAPIClient {
///
/// Create a new admin if the current admin has sudo privileges.
pub async fn create_admin(&self, body: &AdminCreate) -> Result<Admin, ApiError> {
let url = format!("{}/api/admin", self.base_url);
let url = format!("{}/api/admin", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -146,7 +146,7 @@ impl MarzbanAPIClient {
admin_username: &str,
body: &AdminModify,
) -> Result<Admin, ApiError> {
let url = format!("{}/api/admin/{}", self.base_url, admin_username);
let url = format!("{}/api/admin/{}", self.inner.base_url, admin_username);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down Expand Up @@ -185,7 +185,7 @@ impl MarzbanAPIClient {
///
/// Remove an admin from the database.
pub async fn delete_admin(&self, admin_username: &str) -> Result<Admin, ApiError> {
let url = format!("{}/api/admin/{}", self.base_url, admin_username);
let url = format!("{}/api/admin/{}", self.inner.base_url, admin_username);
let response = self
.prepare_authorized_request(reqwest::Method::DELETE, &url)
.await
Expand Down Expand Up @@ -234,7 +234,7 @@ impl MarzbanAPIClient {
limit: Option<&i32>,
username: Option<&str>,
) -> Result<Vec<Admin>, ApiError> {
let url = format!("{}/api/admins/", self.base_url);
let url = format!("{}/api/admins/", self.inner.base_url);
let mut params = Vec::new();
if let Some(value) = offset {
params.push(("offset", value.to_string()))
Expand Down
8 changes: 4 additions & 4 deletions src/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl MarzbanAPIClient {
///
/// Retrieve core statistics such as version and uptime.
pub async fn get_core_stats(&self) -> Result<CoreStats, ApiError> {
let url = format!("{}/api/core", self.base_url);
let url = format!("{}/api/core", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -40,7 +40,7 @@ impl MarzbanAPIClient {
///
/// Restart the core and all connected nodes.
pub async fn restart_core(&self) -> Result<String, ApiError> {
let url = format!("{}/api/core", self.base_url);
let url = format!("{}/api/core", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand All @@ -64,7 +64,7 @@ impl MarzbanAPIClient {
///
/// Get the current core configuration.
pub async fn get_core_config(&self) -> Result<String, ApiError> {
let url = format!("{}/api/core/config", self.base_url);
let url = format!("{}/api/core/config", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -91,7 +91,7 @@ impl MarzbanAPIClient {
///
/// Modify the core configuration and restart the core.
pub async fn modify_core_config(&self, config_as_json: &str) -> Result<String, ApiError> {
let url = format!("{}/api/core/config", self.base_url);
let url = format!("{}/api/core/config", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down
2 changes: 1 addition & 1 deletion src/api/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl MarzbanAPIClient {
/// Base URL of the Marzban panel.
pub async fn base_url(&self) -> Result<String, ApiError> {
let response = self
.prepare_authorized_request(reqwest::Method::GET, &self.base_url)
.prepare_authorized_request(reqwest::Method::GET, &self.inner.base_url)
.await
.send()
.await?;
Expand Down
16 changes: 8 additions & 8 deletions src/api/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl MarzbanAPIClient {
///
/// Retrieve the current node settings, including TLS certificate.
pub async fn get_node_settings(&self) -> Result<NodeSettings, ApiError> {
let url = format!("{}/api/node/settings", self.base_url);
let url = format!("{}/api/node/settings", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -39,7 +39,7 @@ impl MarzbanAPIClient {
///
/// Add a new node to the database and optionally add it as a host.
pub async fn add_node(&self, body: &NodeCreate) -> Result<NodeResponse, ApiError> {
let url = format!("{}/api/node", self.base_url);
let url = format!("{}/api/node", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -70,7 +70,7 @@ impl MarzbanAPIClient {
///
/// Retrieve details of a specific node by its ID.
pub async fn get_node(&self, node_id: &i32) -> Result<NodeResponse, ApiError> {
let url = format!("{}/api/node/{}", self.base_url, node_id);
let url = format!("{}/api/node/{}", self.inner.base_url, node_id);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down Expand Up @@ -105,7 +105,7 @@ impl MarzbanAPIClient {
node_id: &i32,
body: &NodeModify,
) -> Result<NodeResponse, ApiError> {
let url = format!("{}/api/node/{}", self.base_url, node_id);
let url = format!("{}/api/node/{}", self.inner.base_url, node_id);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down Expand Up @@ -137,7 +137,7 @@ impl MarzbanAPIClient {
///
/// Delete a node and remove it from xray in the background.
pub async fn remove_node(&self, node_id: &i32) -> Result<String, ApiError> {
let url = format!("{}/api/node/{}", self.base_url, node_id);
let url = format!("{}/api/node/{}", self.inner.base_url, node_id);
let response = self
.prepare_authorized_request(reqwest::Method::DELETE, &url)
.await
Expand Down Expand Up @@ -165,7 +165,7 @@ impl MarzbanAPIClient {
///
/// Retrieve a list of all nodes. Accessible only to sudo admins.
pub async fn get_nodes(&self) -> Result<Vec<NodeResponse>, ApiError> {
let url = format!("{}/api/nodes", self.base_url);
let url = format!("{}/api/nodes", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -188,7 +188,7 @@ impl MarzbanAPIClient {
///
/// Trigger a reconnection for the specified node. Only accessible to sudo admins.
pub async fn reconnect_node(&self, node_id: &i32) -> Result<String, ApiError> {
let url = format!("{}/api/node/{}/reconnect", self.base_url, node_id);
let url = format!("{}/api/node/{}/reconnect", self.inner.base_url, node_id);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -225,7 +225,7 @@ impl MarzbanAPIClient {
start: Option<&str>,
end: Option<&str>,
) -> Result<NodesUsageResponse, ApiError> {
let url = format!("{}/api/nodes/usage", self.base_url);
let url = format!("{}/api/nodes/usage", self.inner.base_url);
let mut params = Vec::new();
if let Some(value) = start {
params.push(("start", value))
Expand Down
8 changes: 4 additions & 4 deletions src/api/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl MarzbanAPIClient {
/// For example, if the user agent is Clash, the response will be a Clash subscription link.
/// If the user agent is a browser, the response will be a web page.
pub async fn user_subscription(&self, user_token: &str) -> Result<String, ApiError> {
let url = format!("{}/sub/{}", self.base_url, user_token);
let url = format!("{}/sub/{}", self.inner.base_url, user_token);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -47,7 +47,7 @@ impl MarzbanAPIClient {
///
/// Retrieves detailed information about the user's subscription.
pub async fn user_subscription_info(&self, user_token: &str) -> Result<UserResponse, ApiError> {
let url = format!("{}/sub/{}/info", self.base_url, user_token);
let url = format!("{}/sub/{}/info", self.inner.base_url, user_token);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down Expand Up @@ -84,7 +84,7 @@ impl MarzbanAPIClient {
start: Option<&str>,
end: Option<&str>,
) -> Result<UserUsagesResponse, ApiError> {
let url = format!("{}/sub/{}/usage", self.base_url, user_token);
let url = format!("{}/sub/{}/usage", self.inner.base_url, user_token);
let mut params = Vec::new();
if let Some(value) = start {
params.push(("start", value))
Expand Down Expand Up @@ -124,7 +124,7 @@ impl MarzbanAPIClient {
user_token: &str,
client_type: &ClientTypes,
) -> Result<String, ApiError> {
let url = format!("{}/sub/{}/{}", self.base_url, user_token, client_type);
let url = format!("{}/sub/{}/{}", self.inner.base_url, user_token, client_type);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down
8 changes: 4 additions & 4 deletions src/api/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl MarzbanAPIClient {
///
/// Fetch system stats including memory, CPU, and user metrics.
pub async fn get_system_stats(&self) -> Result<SystemStats, ApiError> {
let url = format!("{}/api/system", self.base_url);
let url = format!("{}/api/system", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -46,7 +46,7 @@ impl MarzbanAPIClient {
///
/// Retrieve inbound configurations grouped by protocol.
pub async fn get_inbounds(&self) -> Result<HashMap<ProxyTypes, Vec<ProxyInbound>>, ApiError> {
let url = format!("{}/api/inbounds", self.base_url);
let url = format!("{}/api/inbounds", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand All @@ -73,7 +73,7 @@ impl MarzbanAPIClient {
///
/// Get a list of proxy hosts grouped by inbound tag.
pub async fn get_hosts(&self) -> Result<HashMap<ProxyTypes, Vec<ProxyHost>>, ApiError> {
let url = format!("{}/api/hosts", self.base_url);
let url = format!("{}/api/hosts", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down Expand Up @@ -103,7 +103,7 @@ impl MarzbanAPIClient {
&self,
body: &HashMap<String, Vec<ProxyHost>>,
) -> Result<HashMap<String, Vec<ProxyHost>>, ApiError> {
let url = format!("{}/api/hosts", self.base_url);
let url = format!("{}/api/hosts", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down
26 changes: 13 additions & 13 deletions src/api/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl MarzbanAPIClient {
/// - **on_hold_timeout**: UTC timestamp when `on_hold` status should start or end.
/// - **on_hold_expire_duration**: Duration (in seconds) for how long the user should stay in `on_hold` status.
pub async fn add_user(&self, new_user: &UserCreate) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user", self.base_url);
let url = format!("{}/api/user", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -73,7 +73,7 @@ impl MarzbanAPIClient {
///
/// Get user information
pub async fn get_user(&self, username: &str) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user/{}", self.base_url, username);
let url = format!("{}/api/user/{}", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down Expand Up @@ -121,7 +121,7 @@ impl MarzbanAPIClient {
username: &str,
body: &UserModify,
) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user/{}", self.base_url, username);
let url = format!("{}/api/user/{}", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down Expand Up @@ -153,7 +153,7 @@ impl MarzbanAPIClient {
///
/// Remove a user
pub async fn delete_user(&self, username: &str) -> Result<String, ApiError> {
let url = format!("{}/api/user/{}", self.base_url, username);
let url = format!("{}/api/user/{}", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::DELETE, &url)
.await
Expand Down Expand Up @@ -181,7 +181,7 @@ impl MarzbanAPIClient {
///
/// Reset user data usage
pub async fn reset_user_data_usage(&self, username: &str) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user/{}/reset", self.base_url, username);
let url = format!("{}/api/user/{}/reset", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -215,7 +215,7 @@ impl MarzbanAPIClient {
///
/// Revoke users subscription (Subscription link and proxies)
pub async fn revoke_user_subscription(&self, username: &str) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user/{}/revoke_sub", self.base_url, username);
let url = format!("{}/api/user/{}/revoke_sub", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand Down Expand Up @@ -249,7 +249,7 @@ impl MarzbanAPIClient {
&self,
query_params: &GetUsersQueryParams,
) -> Result<UsersResponse, ApiError> {
let url = format!("{}/api/users", self.base_url);
let url = format!("{}/api/users", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::GET, &url)
.await
Expand Down Expand Up @@ -277,7 +277,7 @@ impl MarzbanAPIClient {
///
/// Reset all users data usage
pub async fn reset_all_users_data_usage(&self) -> Result<String, ApiError> {
let url = format!("{}/api/users/reset", self.base_url);
let url = format!("{}/api/users/reset", self.inner.base_url);
let response = self
.prepare_authorized_request(reqwest::Method::POST, &url)
.await
Expand All @@ -302,7 +302,7 @@ impl MarzbanAPIClient {
start: Option<&str>,
end: Option<&str>,
) -> Result<UserUsagesResponse, ApiError> {
let url = format!("{}/api/user/{}/usage", self.base_url, username);
let url = format!("{}/api/user/{}/usage", self.inner.base_url, username);
let mut params = Vec::new();
if let Some(value) = start {
params.push(("start", value.to_string()))
Expand Down Expand Up @@ -353,7 +353,7 @@ impl MarzbanAPIClient {
end: Option<&str>,
admin: Option<Vec<String>>,
) -> Result<UsersUsagesResponse, ApiError> {
let url = format!("{}/api/users/usage", self.base_url);
let url = format!("{}/api/users/usage", self.inner.base_url);
let mut params = Vec::new();
if let Some(value) = start {
params.push(("start", value.to_string()))
Expand Down Expand Up @@ -396,7 +396,7 @@ impl MarzbanAPIClient {
username: &str,
admin_username: &str,
) -> Result<UserResponse, ApiError> {
let url = format!("{}/api/user/{}/set-owner", self.base_url, username);
let url = format!("{}/api/user/{}/set-owner", self.inner.base_url, username);
let response = self
.prepare_authorized_request(reqwest::Method::PUT, &url)
.await
Expand Down Expand Up @@ -437,7 +437,7 @@ impl MarzbanAPIClient {
expired_before: Option<DateTime<Utc>>,
expired_after: Option<DateTime<Utc>>,
) -> Result<Vec<String>, ApiError> {
let url = format!("{}/api/users/expired", self.base_url);
let url = format!("{}/api/users/expired", self.inner.base_url);
let mut params = Vec::new();
if let Some(value) = expired_before {
params.push(("expired_before", value))
Expand Down Expand Up @@ -481,7 +481,7 @@ impl MarzbanAPIClient {
expired_before: Option<DateTime<Utc>>,
expired_after: Option<DateTime<Utc>>,
) -> Result<Vec<String>, ApiError> {
let url = format!("{}/api/users/expired", self.base_url);
let url = format!("{}/api/users/expired", self.inner.base_url);
let mut params = Vec::new();
if let Some(value) = expired_before {
params.push(("expired_before", value))
Expand Down
Loading

0 comments on commit e96bc4d

Please sign in to comment.