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

// `RepoLabels` resource client (generated, 4 operations).

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

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

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

    /// 查询仓库的标签列表。List repository labels.
    ///
    /// `GET /{repo}/-/labels`
    pub async fn list_labels(
        &self,
        repo: String,
        query: &ListLabelsQuery,
    ) -> Result<Vec<crate::models::Label>> {
        let path = format!("/{}/-/labels", repo);
        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::<Vec<crate::models::Label>>(Method::GET, url)
            .await
    }

    /// 更新标签信息。Update label information.
    ///
    /// `PATCH /{repo}/-/labels/{name}`
    pub async fn patch_label(
        &self,
        repo: String,
        name: String,
        body: &crate::models::PatchLabelForm,
    ) -> Result<crate::models::Label> {
        let path = format!("/{}/-/labels/{}", repo, name);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::Label, _>(Method::PATCH, url, body)
            .await
    }

    /// 创建一个 标签。Create a label.
    ///
    /// `POST /{repo}/-/labels`
    pub async fn post_label(
        &self,
        repo: String,
        body: &crate::models::PostLabelForm,
    ) -> Result<crate::models::Label> {
        let path = format!("/{}/-/labels", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::Label, _>(Method::POST, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListLabelsQuery {
    /// 分页页码。
    #[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 keyword: Option<String>,
}

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