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
use reqwest::Response;
use crate::{Result, RiotApiError};
/// A "raw" unparsed successful response from the Riot API, for internal or advanced use cases.
#[non_exhaustive]
pub struct ResponseInfo {
/// The reqwest response.
pub response: Response,
/// The number of retries used, zero for first-try success.
pub retries: u8,
/// If the response has an HTTP status code indicating a `None` response (i.e. 204, 404).
pub status_none: bool,
/// Any reqwest errors that occurred. A response may still be successful even if this is non-empty due to retrying.
pub reqwest_errors: Vec<reqwest::Error>,
}
impl ResponseInfo {
/// Helper to deserialize the JSON-encoded value from a `ResponseInfo`, properly handling errors.
pub(crate) async fn json<T: for<'de> crate::de::Deserialize<'de>>(self) -> Result<T> {
let Self {
response,
retries,
status_none: _,
mut reqwest_errors,
} = self;
let status = response.status();
let bytes = match response.bytes().await {
Ok(bytes) => bytes,
Err(err) => {
reqwest_errors.push(err);
return Err(RiotApiError::new(
reqwest_errors,
None,
retries,
None, // `.bytes()` consumes the response.
Some(status),
));
}
};
let value = match crate::de::from_slice::<T>(&bytes) {
Ok(value) => value,
Err(serde_err) => {
return Err(RiotApiError::new(
reqwest_errors,
Some(serde_err),
retries,
None, // `.bytes()` consumes the response.
Some(status),
));
}
};
Ok(value)
}
}