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

// `Registries` resource client (generated, 10 operations).

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

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

    /// 删除制品。 Delete the specific package.
    ///
    /// `DELETE /{slug}/-/packages/{type}/{name}`
    pub async fn delete_package(
        &self,
        slug: String,
        type_: String,
        name: String,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/packages/{}/{}", slug, type_, name);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 删除制品标签。 Delete the specific tag under specific package
    ///
    /// `DELETE /{slug}/-/packages/{type}/{name}/-/tag/{tag}`
    pub async fn delete_package_tag(
        &self,
        slug: String,
        type_: String,
        name: String,
        tag: String,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/packages/{}/{}/-/tag/{}", slug, type_, name, tag);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 删除制品库。Delete the registry.
    ///
    /// `DELETE /{registry}`
    pub async fn delete_registry(&self, registry: String) -> Result<serde_json::Value> {
        let path = format!("/{}", registry);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 查询组织下面用户有权限查看到的制品仓库。Query all registries that the user has permission to see under specific organization.
    ///
    /// `GET /{slug}/-/registries`
    pub async fn get_group_sub_registries(
        &self,
        slug: String,
        query: &GetGroupSubRegistriesQuery,
    ) -> Result<Vec<crate::models::Registry4User>> {
        let path = format!("/{}/-/registries", 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.registry_type {
                pairs.append_pair("registry_type", v);
            }
            if let Some(ref v) = query.filter_type {
                pairs.append_pair("filter_type", 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::Registry4User>>(Method::GET, url)
            .await
    }

    /// 获取指定制品的详细信息。 Get the package detail.
    ///
    /// `GET /{slug}/-/packages/{type}/{name}`
    pub async fn get_package(
        &self,
        slug: String,
        type_: String,
        name: String,
    ) -> Result<crate::models::PackageDetail> {
        let path = format!("/{}/-/packages/{}/{}", slug, type_, name);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::PackageDetail>(Method::GET, url)
            .await
    }

    /// 获取制品标签详情。 Get the specific tag under specific package.
    ///
    /// `GET /{slug}/-/packages/{type}/{name}/-/tag/{tag}`
    pub async fn get_package_tag_detail(
        &self,
        slug: String,
        type_: String,
        name: String,
        tag: String,
        query: &GetPackageTagDetailQuery,
    ) -> Result<crate::models::TagDetail> {
        let path = format!("/{}/-/packages/{}/{}/-/tag/{}", slug, type_, name, tag);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.sha256 {
                pairs.append_pair("sha256", v);
            }
            if let Some(ref v) = query.arch {
                pairs.append_pair("arch", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::TagDetail>(Method::GET, url)
            .await
    }

    /// 获取制品标签的出生证明。 Get the specific tag provenance under specific package.
    ///
    /// `GET /{slug}/-/packages/{type}/{name}/-/tag/{tag}/provenance`
    pub async fn get_package_tag_provenance(
        &self,
        slug: String,
        type_: String,
        name: String,
        tag: String,
    ) -> Result<crate::models::Provenance> {
        let path = format!(
            "/{}/-/packages/{}/{}/-/tag/{}/provenance",
            slug, type_, name, tag
        );
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::Provenance>(Method::GET, url)
            .await
    }

    /// 查询制品标签列表。 List all tags under specific package.
    ///
    /// `GET /{slug}/-/packages/{type}/{name}/-/tags`
    pub async fn list_package_tags(
        &self,
        slug: String,
        type_: String,
        name: String,
        query: &ListPackageTagsQuery,
    ) -> Result<crate::models::Tag> {
        let path = format!("/{}/-/packages/{}/{}/-/tags", slug, type_, name);
        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.ordering {
                pairs.append_pair("ordering", v);
            }
            if let Some(ref v) = query.tag_name {
                pairs.append_pair("tag_name", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::Tag>(Method::GET, url)
            .await
    }

    /// 查询制品列表。 List all packages.
    ///
    /// `GET /{slug}/-/packages`
    pub async fn list_packages(
        &self,
        slug: String,
        query: &ListPackagesQuery,
    ) -> Result<Vec<crate::models::Package>> {
        let path = format!("/{}/-/packages", slug);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.type_ {
                pairs.append_pair("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.ordering {
                pairs.append_pair("ordering", v);
            }
            if let Some(ref v) = query.name {
                pairs.append_pair("name", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Package>>(Method::GET, url)
            .await
    }

    /// 改变制品仓库可见性。Update visibility of registry.
    ///
    /// `POST /{registry}/-/settings/set_visibility`
    pub async fn set_registry_visibility(
        &self,
        registry: String,
        query: &SetRegistryVisibilityQuery,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/settings/set_visibility", registry);
        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
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetGroupSubRegistriesQuery {
    /// 页码
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// 每页数量
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// 制品仓库类型
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub registry_type: Option<String>,
    /// 制品仓库可见性类型
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filter_type: Option<String>,
    /// 排序类型，默认created_at
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub order_by: Option<String>,
    /// 排序顺序
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<bool>,
    /// 查全部/查询直接属于当前组织的仓库/查询子组织的制品仓库
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub descendant: Option<String>,
    /// 搜索关键字
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
}

impl GetGroupSubRegistriesQuery {
    /// 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 `registry_type` query parameter.
    pub fn registry_type(mut self, v: impl Into<String>) -> Self {
        self.registry_type = 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 `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 GetPackageTagDetailQuery {
    /// 摘要
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
    /// 架构，docker制品必需，例: linux/amd64/v3。required for docker artifacts
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub arch: Option<String>,
}

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

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListPackageTagsQuery {
    /// 页码。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 type.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ordering: Option<String>,
    /// 关键字。Key word to search tag name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tag_name: Option<String>,
}

impl ListPackageTagsQuery {
    /// 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 `ordering` query parameter.
    pub fn ordering(mut self, v: impl Into<String>) -> Self {
        self.ordering = Some(v.into());
        self
    }
    /// Set `tag_name` query parameter.
    pub fn tag_name(mut self, v: impl Into<String>) -> Self {
        self.tag_name = Some(v.into());
        self
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListPackagesQuery {
    /// 制品类型。Type.
    #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")]
    pub 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>,
    /// 顺序类型。Ordering type.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ordering: Option<String>,
    /// 关键字。Key word to search package name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

impl ListPackagesQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `type` query parameter.
    pub fn type_(mut self, v: impl Into<String>) -> Self {
        self.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 `ordering` query parameter.
    pub fn ordering(mut self, v: impl Into<String>) -> Self {
        self.ordering = Some(v.into());
        self
    }
    /// Set `name` query parameter.
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.name = Some(v.into());
        self
    }
}

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

impl SetRegistryVisibilityQuery {
    /// 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
    }
}
