Skip to main content

artifacts/apis/
simulation_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 [`fight_simulation`]
7#[derive(Clone, Debug)]
8pub struct FightSimulationParams {
9    pub combat_simulation_request_schema: models::CombatSimulationRequestSchema,
10}
11
12impl FightSimulationParams {
13    pub fn new(combat_simulation_request_schema: models::CombatSimulationRequestSchema) -> Self {
14        Self {
15            combat_simulation_request_schema,
16        }
17    }
18}
19
20/// struct for typed errors of method [`fight_simulation`]
21#[derive(Debug, Clone, Serialize)]
22#[serde(untagged)]
23pub enum FightSimulationError {
24    /// Monster not found.
25    Status404(models::ErrorResponseSchema),
26    /// Request could not be processed due to an invalid payload.
27    Status422(models::ErrorResponseSchema),
28}
29
30impl<'de> Deserialize<'de> for FightSimulationError {
31    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
32    where
33        D: Deserializer<'de>,
34    {
35        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
36        match raw.error.code {
37            404 => Ok(Self::Status404(raw)),
38            422 => Ok(Self::Status422(raw)),
39            _ => Err(de::Error::custom(format!(
40                "Unexpected error code: {}",
41                raw.error.code
42            ))),
43        }
44    }
45}
46
47/// Simulate combat with fake characters against a monster multiple times. Member or founder account required.
48pub async fn fight_simulation(
49    configuration: &configuration::Configuration,
50    params: FightSimulationParams,
51) -> Result<models::CombatSimulationResponseSchema, Error<FightSimulationError>> {
52    let local_var_configuration = configuration;
53
54    // unbox the parameters
55    let combat_simulation_request_schema = params.combat_simulation_request_schema;
56
57    let local_var_client = &local_var_configuration.client;
58
59    let local_var_uri_str = format!(
60        "{}/simulation/fight_simulation",
61        local_var_configuration.base_path
62    );
63    let mut local_var_req_builder =
64        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
65
66    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
67        local_var_req_builder =
68            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
69    }
70    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
71        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
72    };
73    local_var_req_builder = local_var_req_builder.json(&combat_simulation_request_schema);
74
75    let local_var_req = local_var_req_builder.build()?;
76    let local_var_resp = local_var_client.execute(local_var_req).await?;
77
78    let local_var_status = local_var_resp.status();
79    let local_var_content = local_var_resp.text().await?;
80
81    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
82        serde_json::from_str(&local_var_content).map_err(Error::from)
83    } else {
84        let local_var_entity: Option<FightSimulationError> =
85            serde_json::from_str(&local_var_content).ok();
86        let local_var_error = ResponseContent {
87            status: local_var_status,
88            content: local_var_content,
89            entity: local_var_entity,
90        };
91        Err(Error::ResponseError(local_var_error))
92    }
93}