Updating to use custom error struct, adding docs

pull/5/head
Mingwei Samuel 2019-10-23 00:39:40 -07:00
parent c3c3e639ee
commit 79ad933e91
13 changed files with 88 additions and 9 deletions

View File

@ -1,7 +1,7 @@
 
// This file is automatically generated. // This file is automatically generated.
// Do not directly edit. // Do not directly edit.
// Generated on 2019-10-23T01:41:03.103Z // Generated on 2019-10-23T07:19:13.049Z
use std::fmt; use std::fmt;
use num_derive; use num_derive;

View File

@ -1,3 +1,5 @@
//! Constant data and Enums relevant to the Riot Games API.
mod region; mod region;
mod champion; mod champion;

View File

@ -1,3 +1,6 @@
//! A region served by a single game server.
//! Each Riot Games API request is directed at a particular region,
//! with tournament API requests directed at the AMERICAS "global" region.
#[derive(Debug)] #[derive(Debug)]
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -54,6 +57,7 @@ impl Region {
AMERICAS => "AMERICAS"; AMERICAS => "AMERICAS";
EUROPE => "EUROPE"; EUROPE => "EUROPE";
ASIA => "ASIA"; ASIA => "ASIA";
} }
} }

View File

@ -1,11 +1,13 @@
// This file is automatically generated. // This file is automatically generated.
// Do not directly edit. // Do not directly edit.
// Generated on 2019-10-23T01:41:03.165Z // Generated on 2019-10-23T07:19:13.042Z
// http://www.mingweisamuel.com/riotapi-schema/tool/ // http://www.mingweisamuel.com/riotapi-schema/tool/
// Version 0c74167e0eaaeb6de1c7e8219fecaabcf8386d1f // Version 0c74167e0eaaeb6de1c7e8219fecaabcf8386d1f
//! Automatically generated endpoint handles and data transfer structs.
mod dto; mod dto;
pub use dto::*; pub use dto::*;

View File

@ -1,7 +1,7 @@
// This file is automatically generated. // This file is automatically generated.
// Do not directly edit. // Do not directly edit.
// Generated on 2019-10-23T01:41:03.173Z // Generated on 2019-10-23T07:19:13.050Z
// http://www.mingweisamuel.com/riotapi-schema/tool/ // http://www.mingweisamuel.com/riotapi-schema/tool/
// Version 0c74167e0eaaeb6de1c7e8219fecaabcf8386d1f // Version 0c74167e0eaaeb6de1c7e8219fecaabcf8386d1f

View File

@ -1,5 +1,7 @@
pub use reqwest::Error as Error; //! Module docs TODO.
pub use reqwest::Result as Result;
mod riot_api_error;
pub use riot_api_error::*;
pub mod consts; pub mod consts;

View File

@ -6,6 +6,7 @@ use reqwest::{ Client, StatusCode, Url };
use tokio_timer::delay_for; use tokio_timer::delay_for;
use crate::Result; use crate::Result;
use crate::RiotApiError;
use crate::riot_api_config::RiotApiConfig; use crate::riot_api_config::RiotApiConfig;
use crate::consts::Region; use crate::consts::Region;
use crate::util::InsertOnlyCHashMap; use crate::util::InsertOnlyCHashMap;
@ -63,7 +64,8 @@ impl RegionalRequester {
let response = client.get(url) let response = client.get(url)
.header(Self::RIOT_KEY_HEADER, &*config.api_key) .header(Self::RIOT_KEY_HEADER, &*config.api_key)
.send() .send()
.await?; .await
.map_err(|e| RiotApiError::new(e, retries, None))?;
// Maybe update rate limits (based on response headers). // Maybe update rate limits (based on response headers).
self.app_rate_limit.on_response(&response); self.app_rate_limit.on_response(&response);
@ -81,7 +83,8 @@ impl RegionalRequester {
// Success. // Success.
Ok(_) => { Ok(_) => {
let value = response.json::<T>().await; let value = response.json::<T>().await;
break value.map(|v| Some(v)); break value.map(|v| Some(v))
.map_err(|e| RiotApiError::new(e, retries, None));
}, },
// Failure, may or may not be retryable. // Failure, may or may not be retryable.
Err(err) => { Err(err) => {
@ -91,7 +94,7 @@ impl RegionalRequester {
(StatusCode::TOO_MANY_REQUESTS != status (StatusCode::TOO_MANY_REQUESTS != status
&& !status.is_server_error()) && !status.is_server_error())
{ {
break Err(err); break Err(RiotApiError::new(err, retries, Some(response)));
} }
}, },
}; };

View File

@ -3,11 +3,13 @@ use std::future::Future;
use log; use log;
use reqwest::Client; use reqwest::Client;
use crate::Result;
use crate::RiotApiConfig; use crate::RiotApiConfig;
use crate::consts::Region; use crate::consts::Region;
use crate::req::RegionalRequester; use crate::req::RegionalRequester;
use crate::util::InsertOnlyCHashMap; use crate::util::InsertOnlyCHashMap;
/// For retrieving data from the Riot Games API.
pub struct RiotApi { pub struct RiotApi {
/// Configuration settings. /// Configuration settings.
config: RiotApiConfig, config: RiotApiConfig,
@ -34,7 +36,7 @@ impl RiotApi {
pub fn get<'a, T: serde::de::DeserializeOwned + 'a>(&'a self, pub fn get<'a, T: serde::de::DeserializeOwned + 'a>(&'a self,
method_id: &'static str, region: Region, path: String, query: Option<String>) method_id: &'static str, region: Region, path: String, query: Option<String>)
-> impl Future<Output = Result<Option<T>, reqwest::Error>> + 'a -> impl Future<Output = Result<Option<T>>> + 'a
{ {
// TODO: max concurrent requests? Or can configure client? // TODO: max concurrent requests? Or can configure client?
self.regional_requesters self.regional_requesters

View File

@ -1,3 +1,4 @@
/// Configuration for instantiating RiotApi.
#[derive(Debug)] #[derive(Debug)]
pub struct RiotApiConfig { pub struct RiotApiConfig {
pub api_key: String, pub api_key: String,

50
src/riot_api_error.rs Normal file
View File

@ -0,0 +1,50 @@
use std::error::Error;
use std::fmt;
pub use reqwest::Error as ReqwestError;
pub use reqwest::Response;
/// Result containing RiotApiError on failure.
pub type Result<T> = std::result::Result<T, RiotApiError>;
/// An error that occurred while processing a Riot API request.
///
/// Although Riven may make multiple requests due to retries, this will always
/// contain exactly one ReqwestError for the final request which failed.
#[derive(Debug)]
pub struct RiotApiError {
reqwest_error: ReqwestError,
retries: u8,
response: Option<Response>,
}
impl RiotApiError {
pub fn new(reqwest_error: ReqwestError, retries: u8, response: Option<Response>) -> Self {
Self {
reqwest_error: reqwest_error,
retries: retries,
response: response,
}
}
/// The ReqwestError for the final failed request.
pub fn source_reqwest_error(&self) -> &ReqwestError {
&self.reqwest_error
}
/// The number of retires attempted. Zero means exactly one request, zero retries.
pub fn retries(&self) -> u8 {
self.retries
}
/// The failed response, if the request sent.
pub fn response<'a>(&self) -> Option<&Response> {
self.response.as_ref()
}
}
impl fmt::Display for RiotApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#?}", self)
}
}
impl Error for RiotApiError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.reqwest_error)
}
}

View File

@ -13,6 +13,12 @@ Array.prototype.groupBy = function(lambda) {
}, {})); }, {}));
} }
function preamble() {
return `// This file is automatically generated.
// Do not directly edit.
// Generated on ${(new Date).toISOString()}.`;
}
function capitalize(input) { function capitalize(input) {
return input[0].toUpperCase() + input.slice(1); return input[0].toUpperCase() + input.slice(1);
} }
@ -112,6 +118,7 @@ function formatRouteArgument(route, pathParams = []) {
module.exports = { module.exports = {
changeCase, changeCase,
preamble,
capitalize, capitalize,
decapitalize, decapitalize,
normalizeSchemaName, normalizeSchemaName,

View File

@ -9,6 +9,8 @@
// http://www.mingweisamuel.com/riotapi-schema/tool/ // http://www.mingweisamuel.com/riotapi-schema/tool/
// Version {{= spec.info.version }} // Version {{= spec.info.version }}
//! Automatically generated endpoint handles and data transfer structs.
mod dto; mod dto;
pub use dto::*; pub use dto::*;

View File

@ -14,6 +14,10 @@ const files = [
[ [
'http://www.mingweisamuel.com/riotapi-schema/openapi-3.0.0.json', 'http://www.mingweisamuel.com/riotapi-schema/openapi-3.0.0.json',
'.spec.json' '.spec.json'
],
[
'http://static.developer.riotgames.com/docs/lol/seasons.json',
'.seasons.json'
] ]
] ]