Skip to main content

artifacts/apis/
game_assistant_api.rs

1use super::{configuration, Error};
2use crate::{apis::ResponseContent, models};
3use reqwest::StatusCode;
4use serde::{de, Deserialize, Deserializer, Serialize};
5
6/// struct for passing parameters to the method [`ask_game_assistant`]
7#[derive(Clone, Debug)]
8pub struct AskGameAssistantParams {
9    pub assistant_question_schema: models::AssistantQuestionSchema,
10}
11
12impl AskGameAssistantParams {
13    pub fn new(assistant_question_schema: models::AssistantQuestionSchema) -> Self {
14        Self {
15            assistant_question_schema,
16        }
17    }
18}
19
20/// struct for typed errors of method [`ask_game_assistant`]
21#[derive(Debug, Clone, Serialize)]
22#[serde(untagged)]
23pub enum AskGameAssistantError {
24    /// Access denied, you must be a member to do that.
25    Status451(models::ErrorResponseSchema),
26    /// You have reached the daily limit for assistant questions. Try again tomorrow.
27    Status570(models::ErrorResponseSchema),
28    /// Insufficient gems.
29    Status563(models::ErrorResponseSchema),
30    /// Request could not be processed due to an invalid payload.
31    Status422(models::ErrorResponseSchema),
32}
33
34impl<'de> Deserialize<'de> for AskGameAssistantError {
35    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
36    where
37        D: Deserializer<'de>,
38    {
39        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
40        match raw.error.code {
41            451 => Ok(Self::Status451(raw)),
42            570 => Ok(Self::Status570(raw)),
43            563 => Ok(Self::Status563(raw)),
44            422 => Ok(Self::Status422(raw)),
45            _ => Err(de::Error::custom(format!(
46                "Unexpected error code: {}",
47                raw.error.code
48            ))),
49        }
50    }
51}
52
53/// Ask the game assistant a question about game mechanics or public API usage. An active membership is required. Members receive a limited number of free questions per day. When no free question is available, the request can spend 1 gem with pay_with_gems=true.
54pub async fn ask_game_assistant(
55    configuration: &configuration::Configuration,
56    params: AskGameAssistantParams,
57) -> Result<models::AssistantAnswerSchema, Error<AskGameAssistantError>> {
58    let local_var_configuration = configuration;
59
60    // unbox the parameters
61    let assistant_question_schema = params.assistant_question_schema;
62
63    let local_var_client = &local_var_configuration.client;
64
65    let local_var_uri_str = format!("{}/game_assistant/ask", local_var_configuration.base_path);
66    let mut local_var_req_builder =
67        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
68
69    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
70        local_var_req_builder =
71            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
72    }
73    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
74        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
75    };
76    local_var_req_builder = local_var_req_builder.json(&assistant_question_schema);
77
78    let local_var_req = local_var_req_builder.build()?;
79    let local_var_resp = local_var_client.execute(local_var_req).await?;
80
81    let local_var_status = local_var_resp.status();
82    let local_var_content = local_var_resp.text().await?;
83
84    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
85        serde_json::from_str(&local_var_content).map_err(Error::from)
86    } else {
87        let local_var_entity: Option<AskGameAssistantError> =
88            serde_json::from_str(&local_var_content).ok();
89        let local_var_error = ResponseContent {
90            status: local_var_status,
91            content: local_var_content,
92            entity: local_var_entity,
93        };
94        Err(Error::ResponseError(local_var_error))
95    }
96}