eclipsis_rs/
api.rs

1use crate::{
2    apis::configuration::Configuration,
3    apis::user_api::*,
4    apis::Error,
5    apis::{client_api::*, match_api::*},
6    models::*,
7};
8
9macro_rules! call_api {
10    ($self:ident, $api_call:ident, $enum:ident, $params:expr) => {{
11        let resp = $api_call(&$self.configuration, $params).await?;
12        let content = resp.entity.ok_or(ApiError::UnableToParseResponse)?;
13
14        match content {
15            $enum::Status200(data) => {
16                return if data.success.unwrap_or(false) {
17                    Ok(data.value.expect("value expected"))
18                } else {
19                    Err(ApiError::NotSuccessful)
20                }
21            }
22            _ => return Err(ApiError::InvalidResponse),
23        }
24    }};
25
26    ($self:ident, $api_call:ident, $enum:ident) => {{
27        let resp = $api_call(&$self.configuration).await?;
28        let content = resp.entity.ok_or(ApiError::UnableToParseResponse)?;
29
30        match content {
31            $enum::Status200(data) => {
32                return if data.success.unwrap_or(false) {
33                    Ok(data.value.expect("value expected"))
34                } else {
35                    Err(ApiError::NotSuccessful)
36                }
37            }
38            _ => return Err(ApiError::InvalidResponse),
39        }
40    }};
41}
42
43macro_rules! get_api_key {
44    ($self:ident) => {{
45        $self
46            .configuration
47            .api_key
48            .clone()
49            .expect("api-key expected")
50            .key
51    }};
52}
53
54pub struct EclipseApi {
55    configuration: Configuration,
56}
57
58#[derive(Debug)]
59pub enum ApiError<T> {
60    NotSuccessful,
61    ResponseError(Error<T>),
62    UnableToParseResponse,
63    InvalidResponse,
64}
65
66type Result<T, E> = std::result::Result<T, ApiError<E>>;
67
68impl EclipseApi {
69    pub fn new(configuration: Configuration) -> EclipseApi {
70        EclipseApi { configuration }
71    }
72
73    pub async fn delete_api_key(&self) -> Result<String, ClientDeleteError> {
74        call_api!(
75            self,
76            client_delete,
77            ClientDeleteSuccess,
78            ClientDeleteParams {
79                api_key: Some(get_api_key!(self)),
80            }
81        )
82    }
83
84    pub async fn info_api_key(&self) -> Result<Box<ApiKeyData>, ClientInfoError> {
85        call_api!(self, client_info, ClientInfoSuccess)
86    }
87
88    pub async fn client_login(&self) -> Result<Vec<String>, ClientLoginError> {
89        call_api!(
90            self,
91            client_login,
92            ClientLoginSuccess,
93            ClientLoginParams {
94                api_key: Some(get_api_key!(self)),
95            }
96        )
97    }
98
99    pub async fn client_logout(&self) -> Result<String, ClientLogoutError> {
100        call_api!(self, client_logout, ClientLogoutSuccess)
101    }
102
103    pub async fn get_match_data(
104        &self,
105        match_id: String,
106    ) -> Result<Box<crate::models::Match>, GetMatchError> {
107        call_api!(
108            self,
109            get_match,
110            GetMatchSuccess,
111            GetMatchParams { match_id }
112        )
113    }
114
115    pub async fn get_user_rating_delta(&self, user_id: u64) -> Result<Vec<i32>, GetUserDeltaError> {
116        call_api!(
117            self,
118            get_user_delta,
119            GetUserDeltaSuccess,
120            GetUserDeltaParams {
121                user_id: user_id as i64,
122            }
123        )
124    }
125
126    pub async fn get_user_matches(&self, user_id: u64) -> Result<Vec<String>, GetUserMatchesError> {
127        call_api!(
128            self,
129            get_user_matches,
130            GetUserMatchesSuccess,
131            GetUserMatchesParams {
132                user_id: user_id as i64,
133            }
134        )
135    }
136
137    pub async fn get_user_overview(
138        &self,
139        user_id: u64,
140    ) -> Result<Vec<Vec<i64>>, GetUserOverviewError> {
141        call_api!(
142            self,
143            get_user_overview,
144            GetUserOverviewSuccess,
145            GetUserOverviewParams {
146                user_id: user_id as i64,
147            }
148        )
149    }
150
151    pub async fn get_user_playtime(&self, user_id: u64) -> Result<i64, GetUserPlaytimeError> {
152        call_api!(
153            self,
154            get_user_playtime,
155            GetUserPlaytimeSuccess,
156            GetUserPlaytimeParams {
157                user_id: user_id as i64,
158            }
159        )
160    }
161
162    pub async fn get_user_rating(&self, user_id: u64) -> Result<i64, GetUserRatingError> {
163        call_api!(
164            self,
165            get_user_rating,
166            GetUserRatingSuccess,
167            GetUserRatingParams {
168                user_id: user_id as i64,
169            }
170        )
171    }
172
173    pub async fn get_user_status(
174        &self,
175        user_id: Vec<u64>,
176    ) -> Result<Vec<Vec<i64>>, GetUserStatusError> {
177        call_api!(
178            self,
179            get_user_status,
180            GetUserStatusSuccess,
181            GetUserStatusParams {
182                request_body: user_id.into_iter().map(|id| id as i64).collect(),
183            }
184        )
185    }
186
187    pub async fn get_user_teammates(
188        &self,
189        user_id: u64,
190    ) -> Result<Vec<Vec<i64>>, GetUserTeammatesError> {
191        call_api!(
192            self,
193            get_user_teammates,
194            GetUserTeammatesSuccess,
195            GetUserTeammatesParams {
196                user_id: user_id as i64,
197            }
198        )
199    }
200}
201
202impl<T> From<Error<T>> for ApiError<T> {
203    fn from(error: Error<T>) -> Self {
204        ApiError::ResponseError(error)
205    }
206}