artifacts/apis/
badges_api.rs1use super::{configuration, Error};
2use crate::{apis::ResponseContent, models};
3use reqwest::StatusCode;
4use serde::{de, Deserialize, Deserializer, Serialize};
5
6#[derive(Clone, Debug)]
8pub struct GetAllBadgesParams {
9    pub page: Option<u32>,
11    pub size: Option<u32>,
13}
14
15impl GetAllBadgesParams {
16    pub fn new(page: Option<u32>, size: Option<u32>) -> Self {
17        Self { page, size }
18    }
19}
20
21#[derive(Clone, Debug)]
23pub struct GetBadgeParams {
24    pub code: String,
26}
27
28impl GetBadgeParams {
29    pub fn new(code: String) -> Self {
30        Self { code }
31    }
32}
33
34#[derive(Debug, Clone, Serialize)]
36#[serde(untagged)]
37pub enum GetAllBadgesError {}
38
39impl<'de> Deserialize<'de> for GetAllBadgesError {
40    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
41    where
42        D: Deserializer<'de>,
43    {
44        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
45        Err(de::Error::custom(format!(
46            "Unexpected error code: {}",
47            raw.error.code
48        )))
49    }
50}
51
52#[derive(Debug, Clone, Serialize)]
54#[serde(untagged)]
55pub enum GetBadgeError {
56    Status404(models::ErrorResponseSchema),
58}
59
60impl<'de> Deserialize<'de> for GetBadgeError {
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62    where
63        D: Deserializer<'de>,
64    {
65        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
66        match raw.error.code {
67            404 => Ok(Self::Status404(raw)),
68            _ => Err(de::Error::custom(format!(
69                "Unexpected error code: {}",
70                raw.error.code
71            ))),
72        }
73    }
74}
75
76pub async fn get_all_badges(
78    configuration: &configuration::Configuration,
79    params: GetAllBadgesParams,
80) -> Result<models::DataPageBadgeSchema, Error<GetAllBadgesError>> {
81    let local_var_configuration = configuration;
82
83    let page = params.page;
85    let size = params.size;
87
88    let local_var_client = &local_var_configuration.client;
89
90    let local_var_uri_str = format!("{}/badges", local_var_configuration.base_path);
91    let mut local_var_req_builder =
92        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
93
94    if let Some(ref local_var_str) = page {
95        local_var_req_builder =
96            local_var_req_builder.query(&[("page", &local_var_str.to_string())]);
97    }
98    if let Some(ref local_var_str) = size {
99        local_var_req_builder =
100            local_var_req_builder.query(&[("size", &local_var_str.to_string())]);
101    }
102    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
103        local_var_req_builder =
104            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
105    }
106
107    let local_var_req = local_var_req_builder.build()?;
108    let local_var_resp = local_var_client.execute(local_var_req).await?;
109
110    let local_var_status = local_var_resp.status();
111    let local_var_content = local_var_resp.text().await?;
112
113    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
114        serde_json::from_str(&local_var_content).map_err(Error::from)
115    } else {
116        let local_var_entity: Option<GetAllBadgesError> =
117            serde_json::from_str(&local_var_content).ok();
118        let local_var_error = ResponseContent {
119            status: local_var_status,
120            content: local_var_content,
121            entity: local_var_entity,
122        };
123        Err(Error::ResponseError(local_var_error))
124    }
125}
126
127pub async fn get_badge(
129    configuration: &configuration::Configuration,
130    params: GetBadgeParams,
131) -> Result<models::BadgeResponseSchema, Error<GetBadgeError>> {
132    let local_var_configuration = configuration;
133
134    let code = params.code;
136
137    let local_var_client = &local_var_configuration.client;
138
139    let local_var_uri_str = format!(
140        "{}/badges/{code}",
141        local_var_configuration.base_path,
142        code = crate::apis::urlencode(code)
143    );
144    let mut local_var_req_builder =
145        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
146
147    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
148        local_var_req_builder =
149            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
150    }
151
152    let local_var_req = local_var_req_builder.build()?;
153    let local_var_resp = local_var_client.execute(local_var_req).await?;
154
155    let local_var_status = local_var_resp.status();
156    let local_var_content = local_var_resp.text().await?;
157
158    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
159        serde_json::from_str(&local_var_content).map_err(Error::from)
160    } else {
161        let local_var_entity: Option<GetBadgeError> = serde_json::from_str(&local_var_content).ok();
162        let local_var_error = ResponseContent {
163            status: local_var_status,
164            content: local_var_content,
165            entity: local_var_entity,
166        };
167        Err(Error::ResponseError(local_var_error))
168    }
169}