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

// `Build` resource client (generated, 8 operations).

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

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

    /// 同步仓库分支下的定时任务。 Synchronize the content under the repository branch.
    ///
    /// `POST /{repo}/-/build/crontab/sync/{branch}`
    pub async fn build_crontab_sync(
        &self,
        repo: String,
        branch: String,
    ) -> Result<crate::models::BuildCommonResult> {
        let path = format!("/{}/-/build/crontab/sync/{}", repo, branch);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::BuildCommonResult>(Method::POST, url)
            .await
    }

    /// 删除流水线日志内容。Delete pipeline logs content.
    ///
    /// `DELETE /{repo}/-/build/logs/{sn}`
    pub async fn build_logs_delete(
        &self,
        repo: String,
        sn: String,
    ) -> Result<crate::models::BuildCommonResult> {
        let path = format!("/{}/-/build/logs/{}", repo, sn);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::BuildCommonResult>(Method::DELETE, url)
            .await
    }

    /// 流水线runner日志下载。Pipeline runner log download.
    ///
    /// `GET /{repo}/-/build/runner/download/log/{pipelineId}`
    pub async fn build_runner_download_log(
        &self,
        repo: String,
        pipeline_id: String,
    ) -> Result<serde_json::Value> {
        let path = format!("/{}/-/build/runner/download/log/{}", repo, pipeline_id);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<serde_json::Value>(Method::GET, url)
            .await
    }

    /// 查询流水线构建列表。List pipeline builds.
    ///
    /// `GET /{repo}/-/build/logs`
    pub async fn get_build_logs(
        &self,
        repo: String,
        query: &GetBuildLogsQuery,
    ) -> Result<crate::models::BuildLogsResult> {
        let path = format!("/{}/-/build/logs", repo);
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.create_time {
                pairs.append_pair("createTime", v);
            }
            if let Some(ref v) = query.end_time {
                pairs.append_pair("endTime", v);
            }
            if let Some(ref v) = query.event {
                pairs.append_pair("event", 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.sha {
                pairs.append_pair("sha", v);
            }
            if let Some(ref v) = query.sn {
                pairs.append_pair("sn", v);
            }
            if let Some(ref v) = query.source_ref {
                pairs.append_pair("sourceRef", v);
            }
            if let Some(ref v) = query.status {
                pairs.append_pair("status", v);
            }
            if let Some(ref v) = query.target_ref {
                pairs.append_pair("targetRef", v);
            }
            if let Some(ref v) = query.user_id {
                pairs.append_pair("userId", v);
            }
            if let Some(ref v) = query.user_name {
                pairs.append_pair("userName", v);
            }
            drop(pairs);
        }
        self.inner
            .execute::<crate::models::BuildLogsResult>(Method::GET, url)
            .await
    }

    /// 查询流水线Stage详情。Get pipeline build stage detail.
    ///
    /// `GET /{repo}/-/build/logs/stage/{sn}/{pipelineId}/{stageId}`
    pub async fn get_build_stage(
        &self,
        repo: String,
        sn: String,
        pipeline_id: String,
        stage_id: String,
    ) -> Result<crate::models::BuildStageResult> {
        let path = format!(
            "/{}/-/build/logs/stage/{}/{}/{}",
            repo, sn, pipeline_id, stage_id
        );
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::BuildStageResult>(Method::GET, url)
            .await
    }

    /// 查询流水线构建状态。Get pipeline build status.
    ///
    /// `GET /{repo}/-/build/status/{sn}`
    pub async fn get_build_status(
        &self,
        repo: String,
        sn: String,
    ) -> Result<crate::models::BuildStatusResult> {
        let path = format!("/{}/-/build/status/{}", repo, sn);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::BuildStatusResult>(Method::GET, url)
            .await
    }

    /// 开始一个构建。Start a build.
    ///
    /// `POST /{repo}/-/build/start`
    pub async fn start_build(
        &self,
        repo: String,
        body: &crate::models::StartBuildReq,
    ) -> Result<crate::models::BuildResult> {
        let path = format!("/{}/-/build/start", repo);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute_with_body::<crate::models::BuildResult, _>(Method::POST, url, body)
            .await
    }

    /// 停止一个构建。 Stop a build.
    ///
    /// `POST /{repo}/-/build/stop/{sn}`
    pub async fn stop_build(&self, repo: String, sn: String) -> Result<crate::models::BuildResult> {
        let path = format!("/{}/-/build/stop/{}", repo, sn);
        let mut url = self.inner.url(&path)?;
        self.inner
            .execute::<crate::models::BuildResult>(Method::POST, url)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct GetBuildLogsQuery {
    /// Start date in "YYYY-MM-DD" format, e.g. "2024-12-01"
    #[serde(
        rename = "createTime",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub create_time: Option<String>,
    /// End date in "YYYY-MM-DD" format, e.g. "2024-12-01"
    #[serde(rename = "endTime", default, skip_serializing_if = "Option::is_none")]
    pub end_time: Option<String>,
    /// Event name, e.g. "push"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub event: Option<String>,
    /// Pagination page number, default(1)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page: Option<i64>,
    /// Pagination page size, default(30), max(100)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i64>,
    /// Commit ID, e.g. "2221d4535ec0c921bcd0858627c5025a871dd2b5"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha: Option<String>,
    /// Build SN, e.g. "cnb-1qa-1i3f5ecau
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sn: Option<String>,
    /// Source branch name, e.g. "dev"
    #[serde(rename = "sourceRef", default, skip_serializing_if = "Option::is_none")]
    pub source_ref: Option<String>,
    /// Build status: "pending", "success", "error", "cancel"
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// Target branch name, e.g. "main"
    #[serde(rename = "targetRef", default, skip_serializing_if = "Option::is_none")]
    pub target_ref: Option<String>,
    /// User ID
    #[serde(rename = "userId", default, skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,
    /// Username
    #[serde(rename = "userName", default, skip_serializing_if = "Option::is_none")]
    pub user_name: Option<String>,
}

impl GetBuildLogsQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `createTime` query parameter.
    pub fn create_time(mut self, v: impl Into<String>) -> Self {
        self.create_time = Some(v.into());
        self
    }
    /// Set `endTime` query parameter.
    pub fn end_time(mut self, v: impl Into<String>) -> Self {
        self.end_time = Some(v.into());
        self
    }
    /// Set `event` query parameter.
    pub fn event(mut self, v: impl Into<String>) -> Self {
        self.event = 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 `sha` query parameter.
    pub fn sha(mut self, v: impl Into<String>) -> Self {
        self.sha = Some(v.into());
        self
    }
    /// Set `sn` query parameter.
    pub fn sn(mut self, v: impl Into<String>) -> Self {
        self.sn = Some(v.into());
        self
    }
    /// Set `sourceRef` query parameter.
    pub fn source_ref(mut self, v: impl Into<String>) -> Self {
        self.source_ref = 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 `targetRef` query parameter.
    pub fn target_ref(mut self, v: impl Into<String>) -> Self {
        self.target_ref = Some(v.into());
        self
    }
    /// Set `userId` query parameter.
    pub fn user_id(mut self, v: impl Into<String>) -> Self {
        self.user_id = Some(v.into());
        self
    }
    /// Set `userName` query parameter.
    pub fn user_name(mut self, v: impl Into<String>) -> Self {
        self.user_name = Some(v.into());
        self
    }
}
