1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![warn(
clippy::complexity,
clippy::correctness,
clippy::perf,
clippy::style,
missing_docs,
rust_2018_idioms
)]
use crate::platform::{recv_reader, Body};
use http::{header::AUTHORIZATION, Request, Response, StatusCode};
use snafu::ResultExt;
mod platform;
pub mod category;
pub mod game;
pub mod race;
pub mod run;
pub mod runner;
mod schema;
mod wrapper;
pub use schema::*;
pub use uuid;
pub struct Client {
client: platform::Client,
access_token: Option<String>,
}
impl Default for Client {
fn default() -> Self {
Client {
client: platform::Client::new(),
access_token: None,
}
}
}
impl Client {
pub fn new() -> Self {
Self::default()
}
pub fn set_access_token(&mut self, access_token: &str) {
let buf = self.access_token.get_or_insert_with(String::new);
buf.clear();
buf.push_str("Bearer ");
buf.push_str(access_token);
}
}
#[derive(Debug, snafu::Snafu)]
pub enum Error {
#[snafu(display("HTTP Status: {}", status.canonical_reason().unwrap_or_else(|| status.as_str())))]
Status {
status: StatusCode,
},
#[snafu(display("{}", message))]
Api {
status: StatusCode,
message: Box<str>,
},
Download {
source: crate::platform::Error,
},
Json {
source: serde_json::Error,
},
UnidentifiableResource,
}
async fn get_response_unchecked(
client: &Client,
mut request: Request<Body>,
) -> Result<Response<Body>, Error> {
if let Some(access_token) = &client.access_token {
if let Ok(access_token) = access_token.parse() {
request.headers_mut().insert(AUTHORIZATION, access_token);
}
}
let response = client.client.request(request).await.context(Download)?;
Ok(response)
}
async fn get_response(client: &Client, request: Request<Body>) -> Result<Response<Body>, Error> {
let response = get_response_unchecked(client, request).await?;
let status = response.status();
if !status.is_success() {
if let Ok(reader) = recv_reader(response.into_body()).await {
if let Ok(error) = serde_json::from_reader::<_, ApiError>(reader) {
return Err(Error::Api {
status,
message: error.error,
});
}
}
return Err(Error::Status { status });
}
Ok(response)
}
async fn get_json<T: serde::de::DeserializeOwned>(
client: &Client,
request: Request<Body>,
) -> Result<T, Error> {
let response = get_response(client, request).await?;
let reader = recv_reader(response.into_body()).await.context(Download)?;
serde_json::from_reader(reader).context(Json)
}
#[derive(serde::Deserialize)]
struct ApiError {
#[serde(alias = "message")]
error: Box<str>,
}