// @generated DO NOT EDIT.
//
// Generated from the CNB OpenAPI specification by `cnb-codegen`.
// Run `cargo run -p cnb-codegen --manifest-path codegen/Cargo.toml \
//      -- --spec codegen/spec/swagger.json --out src` to refresh.

#![allow(clippy::all)]
#![allow(missing_docs)]
#![allow(unused_imports)]
#![allow(unused_mut)]

use reqwest::Method;
use serde::Serialize;

use crate::error::{ApiError, Result};
use crate::http::HttpInner;
use crate::models;

// `Users` resource client (generated, 6 operations).

/// Resource client.
#[derive(Debug, Clone)]
pub struct UsersClient {
    inner: HttpInner,
}

impl UsersClient {
    /// Construct a new resource client. Normally obtained
    /// via [`crate::ApiClient`] rather than directly.
    pub fn new(inner: HttpInner) -> Self {
        Self { inner }
    }

    /// 查询当前用户用户拥有指定权限的所有资源列表。List resources that the current user has specified permissions for.
    ///
    /// `GET /user/autocomplete_source`
    pub async fn auto_complete_source(
        &self,
        query: &AutoCompleteSourceQuery,
    ) -> Result<Vec<String>> {
        let path = "/user/autocomplete_source".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.source_type {
                pairs.append_pair("source_type", v);
            }
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            if let Some(ref v) = query.access {
                pairs.append_pair("access", v);
            }
            if let Some(ref v) = query.order_by {
                pairs.append_pair("order_by", v);
            }
            if let Some(v) = query.desc {
                pairs.append_pair("desc", &v.to_string());
            }
            drop(pairs);
        }
        self.inner.execute::<Vec<String>>(Method::GET, url).await
    }

    /// 获取指定用户的详情信息。Get detailed information for a specified user.
    ///
    /// `GET /user`
    pub async fn get_user_info(&self) -> Result<crate::models::UsersResult> {
        let path = "/user".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::UsersResult>(Method::GET, url)
            .await
    }

    /// 获取指定用户的详情信息。Get detailed information for a specified user.
    ///
    /// `GET /users/{username}`
    pub async fn get_user_info_by_name(
        &self,
        username: String,
    ) -> Result<crate::models::UsersResult> {
        let path = format!("/users/{}", username);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::UsersResult>(Method::GET, url)
            .await
    }

    /// 获取用户邮箱列表
    ///
    /// `GET /user/emails`
    pub async fn list_email(&self) -> Result<crate::models::UserEmails> {
        let path = "/user/emails".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::UserEmails>(Method::GET, url)
            .await
    }

    /// 获取用户 GPG keys 列表。List GPG Keys.
    ///
    /// `GET /user/gpg-keys`
    pub async fn list_gpg_keys(
        &self,
        query: &ListGPGKeysQuery,
    ) -> Result<crate::models::GpgPublicKey> {
        let path = "/user/gpg-keys".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.page {
                pairs.append_pair("page", &v.to_string());
            }
            if let Some(v) = query.page_size {
                pairs.append_pair("page_size", &v.to_string());
            }
            if let Some(ref v) = query.keyword {
                pairs.append_pair("keyword", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::GpgPublicKey>(Method::GET, url)
            .await
    }

    /// 更新指定用户的详情信息。Updates the specified user's profile information.
    ///
    /// `POST /user`
    pub async fn update_user_info(
        &self,
        body: &crate::models::UpdateUserInfoPayload,
    ) -> Result<serde_json::Value> {
        let path = "/user".to_string();
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AutoCompleteSourceQuery {
    /// Source type
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_type: Option<String>,
    /// Pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// Filter by resources.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// 最小仓库权限，默认owner。Minima repository permissions
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub access: Option<String>,
    /// Order field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order_by: Option<String>,
    /// 排序顺序。Ordering.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<bool>,
}

impl AutoCompleteSourceQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `source_type` query parameter.
    pub fn source_type(mut self, v: impl Into<String>) -> Self {
        self.source_type = Some(v.into());
        self
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `access` query parameter.
    pub fn access(mut self, v: impl Into<String>) -> Self {
        self.access = Some(v.into());
        self
    }
    /// Set `order_by` query parameter.
    pub fn order_by(mut self, v: impl Into<String>) -> Self {
        self.order_by = Some(v.into());
        self
    }
    /// Set `desc` query parameter.
    pub fn desc(mut self, v: impl Into<bool>) -> Self {
        self.desc = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListGPGKeysQuery {
    /// pagination page number
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// pagination page size
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// gpg search key
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub keyword: Option<String>,
}

impl ListGPGKeysQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `page` query parameter.
    pub fn page(mut self, v: impl Into<i64>) -> Self {
        self.page = Some(v.into());
        self
    }
    /// Set `page_size` query parameter.
    pub fn page_size(mut self, v: impl Into<i64>) -> Self {
        self.page_size = Some(v.into());
        self
    }
    /// Set `keyword` query parameter.
    pub fn keyword(mut self, v: impl Into<String>) -> Self {
        self.keyword = Some(v.into());
        self
    }
}
