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

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

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

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

    /// 通过 asset 记录 id 删除一个 asset
    ///
    /// `DELETE /{repo}/-/assets/{assetID}`
    pub async fn delete_asset(&self, repo: String, asset_id: i64) -> Result<serde_json::Value> {
        let path = format!("/{}/-/assets/{}", repo, asset_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::DELETE, url)
            .await
    }

    /// 获取 issue 文件或合并请求文件的请求，返回文件二进制内容。Request to retrieve file of issues and pull requests, returns binary content.
    ///
    /// `GET /{repo}/-/files/{filePath}`
    pub async fn get_files(&self, repo: String, file_path: String) -> Result<serde_json::Value> {
        let path = format!("/{}/-/files/{}", repo, file_path);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::GET, url)
            .await
    }

    /// 获取 issue 图片或合并请求图片的请求，返回图片二进制内容。Request to retrieve image of issues and pull requests, returns binary content.
    ///
    /// `GET /{repo}/-/imgs/{imgPath}`
    pub async fn get_imgs(&self, repo: String, img_path: String) -> Result<serde_json::Value> {
        let path = format!("/{}/-/imgs/{}", repo, img_path);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::GET, url)
            .await
    }

    /// 仓库的 asset 记录列表
    ///
    /// `GET /{slug}/-/list-assets`
    pub async fn list_assets(
        &self,
        slug: String,
        query: &ListAssetsQuery,
    ) -> Result<Vec<crate::models::AssetRecords>> {
        let path = format!("/{}/-/list-assets", 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());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::AssetRecords>>(Method::GET, url)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListAssetsQuery {
    /// 第几页，从1开始
    #[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 ListAssetsQuery {
    /// 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
    }
}
