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    /// Access denied, you must be a member to do that.
27    Status451(models::ErrorResponseSchema),
28    /// Request could not be processed due to an invalid payload.
29    Status422(models::ErrorResponseSchema),
30}
31
32impl<'de> Deserialize<'de> for FightSimulationError {
33    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
34    where
35        D: Deserializer<'de>,
36    {
37        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
38        match raw.error.code {
39            404 => Ok(Self::Status404(raw)),
40            451 => Ok(Self::Status451(raw)),
41            422 => Ok(Self::Status422(raw)),
42            _ => Err(de::Error::custom(format!(
43                "Unexpected error code: {}",
44                raw.error.code
45            ))),
46        }
47    }
48}
49
50/// Simulate combat with fake characters against a monster multiple times. Member or founder account required.
51pub async fn fight_simulation(
52    configuration: &configuration::Configuration,
53    params: FightSimulationParams,
54) -> Result<models::CombatSimulationResponseSchema, Error<FightSimulationError>> {
55    let local_var_configuration = configuration;
56
57    // unbox the parameters
58    let combat_simulation_request_schema = params.combat_simulation_request_schema;
59
60    let local_var_client = &local_var_configuration.client;
61
62    let local_var_uri_str = format!(
63        "{}/simulation/fight_simulation",
64        local_var_configuration.base_path
65    );
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(&combat_simulation_request_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<FightSimulationError> =
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}