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

// `Search` resource client (generated, 1 operations).

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

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

    /// Search resource with the key
    ///
    /// `GET /search/public-repos`
    pub async fn list_public_repos(
        &self,
        query: &ListPublicReposQuery,
    ) -> Result<Vec<crate::models::Repos4UserBase>> {
        let path = "/search/public-repos".to_string();
        let mut url = self.inner.url(&path)?;
        {
            let mut pairs = url.query_pairs_mut();
            if let Some(ref v) = query.key {
                pairs.append_pair("key", v);
            }
            if let Some(ref v) = query.flags {
                pairs.append_pair("flags", v);
            }
            if let Some(ref v) = query.flags_match {
                pairs.append_pair("flags_match", 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(v) = query.top_n {
                pairs.append_pair("topN", &v.to_string());
            }
            drop(pairs);
        }
        self.inner
            .execute::<Vec<crate::models::Repos4UserBase>>(Method::GET, url)
            .await
    }
}

/// Query parameters for the matching method on the resource client.
#[derive(Debug, Clone, Default, Serialize)]
pub struct ListPublicReposQuery {
    /// key
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    /// 仓库类型标记，逗号分隔。Repository type flags, comma separated
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags: Option<String>,
    /// flags 多值匹配模式。Flags match mode when multiple flags provided
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flags_match: Option<String>,
    /// 排序类型，默认last_updated_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>,
    /// 排行前N位，默认10，最大值100
    #[serde(rename = "topN", default, skip_serializing_if = "Option::is_none")]
    pub top_n: Option<i64>,
}

impl ListPublicReposQuery {
    /// Construct an empty query.
    pub fn new() -> Self {
        Self::default()
    }
    /// Set `key` query parameter.
    pub fn key(mut self, v: impl Into<String>) -> Self {
        self.key = Some(v.into());
        self
    }
    /// Set `flags` query parameter.
    pub fn flags(mut self, v: impl Into<String>) -> Self {
        self.flags = Some(v.into());
        self
    }
    /// Set `flags_match` query parameter.
    pub fn flags_match(mut self, v: impl Into<String>) -> Self {
        self.flags_match = 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 `topN` query parameter.
    pub fn top_n(mut self, v: impl Into<i64>) -> Self {
        self.top_n = Some(v.into());
        self
    }
}
