artifacts/apis/
simulation_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 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#[derive(Debug, Clone, Serialize)]
22#[serde(untagged)]
23pub enum FightSimulationError {
24 Status404(models::ErrorResponseSchema),
26 Status451(models::ErrorResponseSchema),
28 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
50pub 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 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}