From a2df949412abee207270065be98a71dd17589565 Mon Sep 17 00:00:00 2001 From: nulluser0 Date: Fri, 21 Jun 2024 23:09:36 +1000 Subject: [PATCH] done user template --- src/api/mod.rs | 1 + src/api/user_template.rs | 173 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 src/api/user_template.rs diff --git a/src/api/mod.rs b/src/api/mod.rs index 52e9766..8178b84 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -4,3 +4,4 @@ pub mod node; pub mod subscription; pub mod system; pub mod user; +pub mod user_template; diff --git a/src/api/user_template.rs b/src/api/user_template.rs new file mode 100644 index 0000000..66e5649 --- /dev/null +++ b/src/api/user_template.rs @@ -0,0 +1,173 @@ +use reqwest::StatusCode; + +use crate::{ + client::MarzbanAPIClient, + error::ApiError, + models::{ + errors::HTTPValidationError, + user_template::{UserTemplateCreate, UserTemplateModify, UserTemplateResponse}, + }, +}; + +impl MarzbanAPIClient { + // Get user templates + pub async fn get_user_templates( + &self, + offset: Option, + limit: Option, + ) -> Result, ApiError> { + let url = format!("{}/api/user_template", self.base_url); + let mut params = Vec::new(); + if let Some(value) = offset { + params.push(value) + } + if let Some(value) = limit { + params.push(value) + } + + let response = self + .prepare_authorized_request(reqwest::Method::GET, &url) + .query(¶ms) + .send() + .await?; + + match response.status() { + StatusCode::OK => response + .json::>() + .await + .map_err(ApiError::NetworkError), + StatusCode::UNPROCESSABLE_ENTITY => { + let error_response = response.json::().await?; + Err(ApiError::ApiResponseError(format!( + "Validation Error: {:?}", + error_response + ))) + } + _ => Err(ApiError::UnexpectedResponse), + } + } + + // Add a user template + pub async fn add_user_template( + &self, + body: &UserTemplateCreate, + ) -> Result { + let url = format!("{}/api/user_template", self.base_url); + let response = self + .prepare_authorized_request(reqwest::Method::POST, &url) + .json(body) + .send() + .await?; + + match response.status() { + StatusCode::OK => response + .json::() + .await + .map_err(ApiError::NetworkError), + StatusCode::FORBIDDEN => { + Err(ApiError::ApiResponseError("You're not allowed".to_string())) + } + StatusCode::CONFLICT => Err(ApiError::ApiResponseError( + "Template by this name already exists".to_string(), + )), + StatusCode::UNPROCESSABLE_ENTITY => { + let error_response = response.json::().await?; + Err(ApiError::ApiResponseError(format!( + "Validation Error: {:?}", + error_response + ))) + } + _ => Err(ApiError::UnexpectedResponse), + } + } + + // Get user template with ID + pub async fn get_user_template(&self, id: &i32) -> Result { + let url = format!("{}/api/user_template/{}", self.base_url, id); + let response = self + .prepare_authorized_request(reqwest::Method::GET, &url) + .send() + .await?; + + match response.status() { + StatusCode::OK => response + .json::() + .await + .map_err(ApiError::NetworkError), + StatusCode::NOT_FOUND => Err(ApiError::ApiResponseError("User not found".to_string())), + StatusCode::UNPROCESSABLE_ENTITY => { + let error_response = response.json::().await?; + Err(ApiError::ApiResponseError(format!( + "Validation Error: {:?}", + error_response + ))) + } + _ => Err(ApiError::UnexpectedResponse), + } + } + + // Modify user template + pub async fn modify_user_template( + &self, + id: &i32, + body: &UserTemplateModify, + ) -> Result { + let url = format!("{}/api/user_template/{}", self.base_url, id); + let response = self + .prepare_authorized_request(reqwest::Method::PUT, &url) + .json(body) + .send() + .await?; + + match response.status() { + StatusCode::OK => response + .json::() + .await + .map_err(ApiError::NetworkError), + StatusCode::FORBIDDEN => { + Err(ApiError::ApiResponseError("You're not allowed".to_string())) + } + StatusCode::NOT_FOUND => Err(ApiError::ApiResponseError( + "User Template not found".to_string(), + )), + StatusCode::CONFLICT => Err(ApiError::ApiResponseError( + "Template by this name already exists".to_string(), + )), + StatusCode::UNPROCESSABLE_ENTITY => { + let error_response = response.json::().await?; + Err(ApiError::ApiResponseError(format!( + "Validation Error: {:?}", + error_response + ))) + } + _ => Err(ApiError::UnexpectedResponse), + } + } + + // Remove user template + pub async fn remove_user_template(&self, id: &i32) -> Result { + let url = format!("{}/api/user_template/{}", self.base_url, id); + let response = self + .prepare_authorized_request(reqwest::Method::DELETE, &url) + .send() + .await?; + + match response.status() { + StatusCode::OK => response.text().await.map_err(ApiError::NetworkError), + StatusCode::FORBIDDEN => { + Err(ApiError::ApiResponseError("You're not allowed".to_string())) + } + StatusCode::NOT_FOUND => Err(ApiError::ApiResponseError( + "User Template not found".to_string(), + )), + StatusCode::UNPROCESSABLE_ENTITY => { + let error_response = response.json::().await?; + Err(ApiError::ApiResponseError(format!( + "Validation Error: {:?}", + error_response + ))) + } + _ => Err(ApiError::UnexpectedResponse), + } + } +}