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

// `Releases` resource client (generated, 12 operations).

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

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

    /// 删除指定的 release。Delete a release.
    ///
    /// `DELETE /{repo}/-/releases/{release_id}`
    pub async fn delete_release(
        &self,
        repo: String,
        release_id: String,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/releases/{}", repo, release_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 删除指定的 release 附件 the specified release asset.
    ///
    /// `DELETE /{repo}/-/releases/{release_id}/assets/{asset_id}`
    pub async fn delete_release_asset(
        &self,
        repo: String,
        release_id: String,
        asset_id: String,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/releases/{}/assets/{}", repo, release_id, asset_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 查询最新的 release。Query the latest release.
    ///
    /// `GET /{repo}/-/releases/latest`
    pub async fn get_latest_release(&self, repo: String) -> Result<crate::models::Release> {
        let path = format!("/{}/-/releases/latest", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::Release>(Method::GET, url)
            .await
    }

    /// 查询指定的 release 附件 the specified release asset.
    ///
    /// `GET /{repo}/-/releases/{release_id}/assets/{asset_id}`
    pub async fn get_release_asset(
        &self,
        repo: String,
        release_id: String,
        asset_id: String,
    ) -> Result<crate::models::ReleaseAsset> {
        let path = format!("/{}/-/releases/{}/assets/{}", repo, release_id, asset_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::ReleaseAsset>(Method::GET, url)
            .await
    }

    /// 根据 id	查询指定 release, 包含附件信息。Get a release by id, include assets information.
    ///
    /// `GET /{repo}/-/releases/{release_id}`
    pub async fn get_release_by_id(
        &self,
        repo: String,
        release_id: String,
    ) -> Result<crate::models::Release> {
        let path = format!("/{}/-/releases/{}", repo, release_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::Release>(Method::GET, url)
            .await
    }

    /// 通过 tag 查询指定 release,包含附件信息。Get a release by tag, include assets information.
    ///
    /// `GET /{repo}/-/releases/tags/{tag}`
    pub async fn get_release_by_tag(
        &self,
        repo: String,
        tag: String,
    ) -> Result<crate::models::Release> {
        let path = format!("/{}/-/releases/tags/{}", repo, tag);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::Release>(Method::GET, url)
            .await
    }

    /// 发起一个获取 release 附件的请求， 302到有一定效期的下载地址。Get a request to fetch a release assets and returns 302 redirect to the assets URL with specific valid time.
    ///
    /// `GET /{repo}/-/releases/download/{tag}/{filename}`
    pub async fn get_releases_asset(
        &self,
        repo: String,
        tag: String,
        filename: String,
        query: &GetReleasesAssetQuery,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/releases/download/{}/{}", repo, tag, filename);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.share {
                pairs.append_pair("share", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<serde_json::Value>(Method::GET, url)
            .await
    }

    /// 查询 release 列表。List releases.
    ///
    /// `GET /{repo}/-/releases`
    pub async fn list_releases(
        &self,
        repo: String,
        query: &ListReleasesQuery,
    ) -> Result<Vec<crate::models::Release>> {
        let path = format!("/{}/-/releases", 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());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Release>>(Method::GET, url)
            .await
    }

    /// 更新 release。Update a release.
    ///
    /// `PATCH /{repo}/-/releases/{release_id}`
    pub async fn patch_release(
        &self,
        repo: String,
        release_id: String,
        body: &crate::models::PatchReleaseForm,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/releases/{}", repo, release_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<serde_json::Value, _>(Method::PATCH, url, body)
            .await
    }

    /// 新增一个 release。Create a release.
    ///
    /// `POST /{repo}/-/releases`
    pub async fn post_release(
        &self,
        repo: String,
        body: &crate::models::PostReleaseForm,
    ) -> Result<crate::models::Release> {
        let path = format!("/{}/-/releases", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::Release, _>(Method::POST, url, body)
            .await
    }

    /// 确认  release 附件上传完成。Confirm release asset upload.
    ///
    /// `POST /{repo}/-/releases/{release_id}/asset-upload-confirmation/{upload_token}/{asset_path}`
    pub async fn post_release_asset_upload_confirmation(
        &self,
        repo: String,
        release_id: String,
        upload_token: String,
        asset_path: String,
        query: &PostReleaseAssetUploadConfirmationQuery,
    ) -> Result<serde_json::Value> {
        let path = format!(
            "/{}/-/releases/{}/asset-upload-confirmation/{}/{}",
            repo, release_id, upload_token, asset_path
        );
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(v) = query.ttl {
                pairs.append_pair("ttl", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<serde_json::Value>(Method::POST, url)
            .await
    }

    /// 新增一个 release 附件。Create a release asset.
    ///
    /// `POST /{repo}/-/releases/{release_id}/asset-upload-url`
    pub async fn post_release_asset_upload_url(
        &self,
        repo: String,
        release_id: String,
        body: &crate::models::PostReleaseAssetUploadUrlForm,
    ) -> Result<crate::models::ReleaseAssetUploadUrl> {
        let path = format!("/{}/-/releases/{}/asset-upload-url", repo, release_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::ReleaseAssetUploadUrl, _>(Method::POST, url, body)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetReleasesAssetQuery {
    /// 是否可以下载，true表示302的下载地址有效期12小时，最多下载10次。
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub share: Option<bool>,
}

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

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

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

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct PostReleaseAssetUploadConfirmationQuery {
    /// 附件保持的天数。0 表示永久，最大不能超过 180 天
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl: Option<i64>,
}

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