rust-cnb 0.1.2

[DEPRECATED - use `cnb` crate instead] rust-cnb with Generated API client
Documentation
//! Organizations API 客户端

use crate::error::{ApiError, Result};
use reqwest::Client;
use serde_json::Value;
use url::Url;

/// Organizations API 客户端
pub struct OrganizationsClient {
    base_url: String,
    client: Client,
}

impl OrganizationsClient {
    /// 创建新的 Organizations API 客户端
    pub fn new(base_url: String, client: Client) -> Self {
        Self { base_url, client }
    }

    /// 设置认证信息
    pub fn with_auth(self, token: &str) -> Self {
        // 这里可以扩展认证逻辑
        self
    }

    /// 获取指定用户拥有权限的顶层组织列表。 Get a list of top-level organizations that the specified user has permissions to access.
    pub async fn get_users_username_groups(
        &self,
        username: String,
        search: Option<String>,
        page: Option<i64>,
        page_size: Option<i64>,
    ) -> Result<Value> {
        let path = format!("/users/{}/groups", username);
        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
        
        if let Some(value) = search {
            url.query_pairs_mut().append_pair("search", &value.to_string());
        }
        if let Some(value) = page {
            url.query_pairs_mut().append_pair("page", &value.to_string());
        }
        if let Some(value) = page_size {
            url.query_pairs_mut().append_pair("page_size", &value.to_string());
        }

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 创建新组织。Create new organization.
    pub async fn post_groups(
        &self,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/groups");
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 获取当前用户拥有权限的顶层组织列表。Get top-level organizations list that the current user has access to.
    pub async fn get_user_groups(
        &self,
        page: Option<i64>,
        page_size: Option<i64>,
        search: Option<String>,
        role: Option<String>,
    ) -> Result<Value> {
        let path = format!("/user/groups");
        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
        
        if let Some(value) = page {
            url.query_pairs_mut().append_pair("page", &value.to_string());
        }
        if let Some(value) = page_size {
            url.query_pairs_mut().append_pair("page_size", &value.to_string());
        }
        if let Some(value) = search {
            url.query_pairs_mut().append_pair("search", &value.to_string());
        }
        if let Some(value) = role {
            url.query_pairs_mut().append_pair("role", &value.to_string());
        }

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 获取指定组织信息。Get information for the specified organization.
    pub async fn get_group(
        &self,
        group: String,
    ) -> Result<Value> {
        let path = format!("/{}", group);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

                let request = self.client.request(
            reqwest::Method::GET,
            url
        );
        



        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 更新组织信息, 可更新的内容为: 组织描述, 组织展示名称, 组织网站, 组织联系邮箱。Updates organization information including: description, display name, website URL and contact email.
    pub async fn put_group(
        &self,
        group: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}", group);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::PUT,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 删除指定组织。Delete the specified organization.
    pub async fn delete_group(
        &self,
        group: String,
        x_cnb_identity_ticket: Option<String>,
    ) -> Result<Value> {
        let path = format!("/{}", group);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

                let mut request = self.client.request(
            reqwest::Method::DELETE,
            url
        );
        

        if let Some(value) = x_cnb_identity_ticket {
            request = request.header("x-cnb-identity-ticket", value);
        }


        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

    /// 转移组织
    pub async fn post_group_transfer(
        &self,
        group: String,
        request_data: serde_json::Value,
    ) -> Result<Value> {
        let path = format!("/{}/-/transfer", group);
        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
        

        
        let mut request = self.client.request(
            reqwest::Method::POST,
            url
        );



        request = request.json(&request_data);

        let response = request.send().await?;
        
        if response.status().is_success() {
            let json: Value = response.json().await?;
            Ok(json)
        } else {
            Err(ApiError::HttpError(response.status().as_u16()))
        }
    }

}