Skip to content

Commit

Permalink
done user template
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser0 committed Jun 21, 2024
1 parent 46e1a00 commit a2df949
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod node;
pub mod subscription;
pub mod system;
pub mod user;
pub mod user_template;
173 changes: 173 additions & 0 deletions src/api/user_template.rs
Original file line number Diff line number Diff line change
@@ -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<i32>,
limit: Option<i32>,
) -> Result<Vec<UserTemplateResponse>, 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(&params)
.send()
.await?;

match response.status() {
StatusCode::OK => response
.json::<Vec<UserTemplateResponse>>()
.await
.map_err(ApiError::NetworkError),
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().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<UserTemplateResponse, ApiError> {
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::<UserTemplateResponse>()
.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::<HTTPValidationError>().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<UserTemplateResponse, ApiError> {
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::<UserTemplateResponse>()
.await
.map_err(ApiError::NetworkError),
StatusCode::NOT_FOUND => Err(ApiError::ApiResponseError("User not found".to_string())),
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().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<UserTemplateResponse, ApiError> {
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::<UserTemplateResponse>()
.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::<HTTPValidationError>().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<String, ApiError> {
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::<HTTPValidationError>().await?;
Err(ApiError::ApiResponseError(format!(
"Validation Error: {:?}",
error_response
)))
}
_ => Err(ApiError::UnexpectedResponse),
}
}
}

0 comments on commit a2df949

Please sign in to comment.