// @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;

// `Repositories` resource client (generated, 15 operations).

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

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

    /// 仓库归档。Archive a repository.
    ///
    /// `POST /{slug}/-/settings/archive`
    pub async fn archive_repo(&self, slug: String) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings/archive", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::POST, url)
            .await
    }

    /// 创建仓库。Create repositories.
    ///
    /// `POST /{slug}/-/repos`
    pub async fn create_repo(
        &self,
        slug: String,
        body: &crate::models::CreateRepoReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/repos", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 删除指定仓库。Delete the specified repository.
    ///
    /// `DELETE /{repo}`
    pub async fn delete_repo(&self, repo: String) -> Result<serde_json::Value> {
        let path = format!("/{}", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 获取指定仓库信息。Get information for the specified repository.
    ///
    /// `GET /{repo}`
    pub async fn get_by_id(&self, repo: String) -> Result<crate::models::Repos4User> {
        let path = format!("/{}", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::Repos4User>(Method::GET, url)
            .await
    }

    /// 查询组织下访问用户有权限查看到仓库。List the repositories that the user has access to.
    ///
    /// `GET /{slug}/-/repos`
    pub async fn get_group_sub_repos(
        &self,
        slug: String,
        query: &GetGroupSubReposQuery,
    ) -> Result<Vec<crate::models::Repos4User>> {
        let path = format!("/{}/-/repos", slug);
        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.filter_type {
                pairs.append_pair("filter_type", v);
            }
            if let Some(ref v) = query.flags {
                pairs.append_pair("flags", v);
            }
            if let Some(ref v) = query.flags_match {
                pairs.append_pair("flags_match", v);
            }
            if let Some(ref v) = query.status {
                pairs.append_pair("status", 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());
            }
            if let Some(ref v) = query.descendant {
                pairs.append_pair("descendant", v);
            }
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Repos4User>>(Method::GET, url)
            .await
    }

    /// 获取指定组织的仓库墙列表。List the pinned repositories of a group.
    ///
    /// `GET /{slug}/-/pinned-repos`
    pub async fn get_pinned_repo_by_group(
        &self,
        slug: String,
    ) -> Result<Vec<crate::models::Repos4UserBase>> {
        let path = format!("/{}/-/pinned-repos", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<Vec<crate::models::Repos4UserBase>>(Method::GET, url)
            .await
    }

    /// 获取指定用户的用户仓库墙。 Get a list of repositories that the specified user has pinned.
    ///
    /// `GET /users/{username}/pinned-repos`
    pub async fn get_pinned_repo_by_id(
        &self,
        username: String,
    ) -> Result<Vec<crate::models::Repos4User>> {
        let path = format!("/users/{}/pinned-repos", username);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<Vec<crate::models::Repos4User>>(Method::GET, url)
            .await
    }

    /// 获取当前用户拥有指定权限及其以上权限的仓库。List repositories owned by the current user with the specified permissions or higher.
    ///
    /// `GET /user/repos`
    pub async fn get_repos(&self, query: &GetReposQuery) -> Result<Vec<crate::models::Repos4User>> {
        let path = "/user/repos".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.search {
                pairs.append_pair("search", v);
            }
            if let Some(ref v) = query.filter_type {
                pairs.append_pair("filter_type", v);
            }
            if let Some(ref v) = query.role {
                pairs.append_pair("role", v);
            }
            if let Some(ref v) = query.flags {
                pairs.append_pair("flags", v);
            }
            if let Some(ref v) = query.flags_match {
                pairs.append_pair("flags_match", v);
            }
            if let Some(ref v) = query.status {
                pairs.append_pair("status", 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<crate::models::Repos4User>>(Method::GET, url)
            .await
    }

    /// 获取指定用户有指定以上权限并且客人态可见的仓库。List repositories where the specified user has the specified permission level or higher and are visible to guests.
    ///
    /// `GET /users/{username}/repos`
    pub async fn get_repos_by_user_name(
        &self,
        username: String,
        query: &GetReposByUserNameQuery,
    ) -> Result<Vec<crate::models::Repos4User>> {
        let path = format!("/users/{}/repos", username);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.search {
                pairs.append_pair("search", v);
            }
            if let Some(ref v) = query.filter_type {
                pairs.append_pair("filter_type", v);
            }
            if let Some(ref v) = query.flags {
                pairs.append_pair("flags", v);
            }
            if let Some(ref v) = query.flags_match {
                pairs.append_pair("flags_match", v);
            }
            if let Some(ref v) = query.status {
                pairs.append_pair("status", v);
            }
            if let Some(ref v) = query.role {
                pairs.append_pair("role", 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(v) = query.desc {
                pairs.append_pair("desc", &v.to_string());
            }
            if let Some(ref v) = query.order_by {
                pairs.append_pair("order_by", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Repos4User>>(Method::GET, url)
            .await
    }

    /// 获取指定仓库的 fork 列表。Get fork list for specified repository.
    ///
    /// `GET /{repo}/-/forks`
    pub async fn list_forks_repos(
        &self,
        repo: String,
        query: &ListForksReposQuery,
    ) -> Result<crate::models::ListForks> {
        let path = format!("/{}/-/forks", repo);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.start_from_root {
                pairs.append_pair("start_from_root", &v.to_string());
            }
            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());
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::ListForks>(Method::GET, url)
            .await
    }

    /// 更新指定组织仓库墙。Update the pinned repositories of a group.
    ///
    /// `PUT /{slug}/-/pinned-repos`
    pub async fn set_pinned_repo_by_group(
        &self,
        slug: String,
        body: &Vec<String>,
    ) -> Result<Vec<crate::models::Repos4UserBase>> {
        let path = format!("/{}/-/pinned-repos", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<Vec<crate::models::Repos4UserBase>, _>(Method::PUT, url, body)
            .await
    }

    /// 改变仓库可见性。Update visibility of repository.
    ///
    /// `POST /{repo}/-/settings/set_visibility`
    pub async fn set_repo_visibility(
        &self,
        repo: String,
        query: &SetRepoVisibilityQuery,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings/set_visibility", repo);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.visibility {
                pairs.append_pair("visibility", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<serde_json::Value>(Method::POST, url)
            .await
    }

    /// 转移仓库。Transfer a repository.
    ///
    /// `POST /{repo}/-/transfer`
    pub async fn transfer_repo(
        &self,
        repo: String,
        body: &crate::models::TransferSlugReq,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/transfer", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::POST, url, body)
            .await
    }

    /// 解除仓库归档。Unarchive a repository.
    ///
    /// `POST /{slug}/-/settings/unarchive`
    pub async fn un_archive_repo(&self, slug: String) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings/unarchive", slug);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::POST, url)
            .await
    }

    /// 更新仓库信息, 可更新的内容为: 仓库简介, 仓库站点, 仓库主题, 开源许可证。updates repository details including description, website URL,topics and license type.
    ///
    /// `PATCH /{repo}`
    pub async fn update_repo(
        &self,
        repo: String,
        body: &crate::models::RepoPatch,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::PATCH, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetGroupSubReposQuery {
    /// 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>,
    /// Repositories type
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filter_type: Option<String>,
    /// 仓库类型标记，逗号分隔。Repository type flags, comma separated
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
    /// flags 多值匹配模式。Flags match mode when multiple flags provided
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags_match: Option<String>,
    /// 仓库状态。Repository status
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: 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>,
    /// 查全部/查询直接属于当前组织的仓库/查询子组织的仓库。Get all/Get repos belong to current org or sub-organization
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub descendant: Option<String>,
    /// Key word
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
}

impl GetGroupSubReposQuery {
    /// 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 `filter_type` query parameter.
    pub fn filter_type(mut self, v: impl Into<String>) -> Self {
        self.filter_type = Some(v.into());
        self
    }
    /// Set `flags` query parameter.
    pub fn flags(mut self, v: impl Into<String>) -> Self {
        self.flags = Some(v.into());
        self
    }
    /// Set `flags_match` query parameter.
    pub fn flags_match(mut self, v: impl Into<String>) -> Self {
        self.flags_match = Some(v.into());
        self
    }
    /// Set `status` query parameter.
    pub fn status(mut self, v: impl Into<String>) -> Self {
        self.status = 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
    }
    /// Set `descendant` query parameter.
    pub fn descendant(mut self, v: impl Into<String>) -> Self {
        self.descendant = Some(v.into());
        self
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetReposQuery {
    /// 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 repositories
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// RType
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filter_type: Option<String>,
    /// 最小仓库权限，默认owner。Minima repository permissions
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// 仓库类型标记，逗号分隔。Repository type flags, comma separated
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
    /// flags 多值匹配模式。Flags match mode when multiple flags provided
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags_match: Option<String>,
    /// 仓库状态。Repository status
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: 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 GetReposQuery {
    /// 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 `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `filter_type` query parameter.
    pub fn filter_type(mut self, v: impl Into<String>) -> Self {
        self.filter_type = Some(v.into());
        self
    }
    /// Set `role` query parameter.
    pub fn role(mut self, v: impl Into<String>) -> Self {
        self.role = Some(v.into());
        self
    }
    /// Set `flags` query parameter.
    pub fn flags(mut self, v: impl Into<String>) -> Self {
        self.flags = Some(v.into());
        self
    }
    /// Set `flags_match` query parameter.
    pub fn flags_match(mut self, v: impl Into<String>) -> Self {
        self.flags_match = Some(v.into());
        self
    }
    /// Set `status` query parameter.
    pub fn status(mut self, v: impl Into<String>) -> Self {
        self.status = 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 GetReposByUserNameQuery {
    /// Filter by repositories
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    /// Repositories type
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filter_type: Option<String>,
    /// 仓库类型标记，逗号分隔。Repository type flags, comma separated
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
    /// flags 多值匹配模式。Flags match mode when multiple flags provided
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags_match: Option<String>,
    /// 仓库状态。Repository status
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// 最小仓库权限，默认owner。Minima repository permissions.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: 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>,
    /// 排序顺序。Ordering.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<bool>,
    /// Order field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order_by: Option<String>,
}

impl GetReposByUserNameQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }
    /// Set `filter_type` query parameter.
    pub fn filter_type(mut self, v: impl Into<String>) -> Self {
        self.filter_type = Some(v.into());
        self
    }
    /// Set `flags` query parameter.
    pub fn flags(mut self, v: impl Into<String>) -> Self {
        self.flags = Some(v.into());
        self
    }
    /// Set `flags_match` query parameter.
    pub fn flags_match(mut self, v: impl Into<String>) -> Self {
        self.flags_match = Some(v.into());
        self
    }
    /// Set `status` query parameter.
    pub fn status(mut self, v: impl Into<String>) -> Self {
        self.status = Some(v.into());
        self
    }
    /// Set `role` query parameter.
    pub fn role(mut self, v: impl Into<String>) -> Self {
        self.role = 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 `desc` query parameter.
    pub fn desc(mut self, v: impl Into<bool>) -> Self {
        self.desc = 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
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListForksReposQuery {
    /// 是否从fork根节点开始展示。Whether to start from the root node of the fork.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start_from_root: Option<bool>,
    /// 页码。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>,
}

impl ListForksReposQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `start_from_root` query parameter.
    pub fn start_from_root(mut self, v: impl Into<bool>) -> Self {
        self.start_from_root = 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
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct SetRepoVisibilityQuery {
    /// 仓库可见性
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub visibility: Option<String>,
}

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