use std::future::Future; use log; use reqwest::Client; use crate::Result; use crate::RiotApiConfig; use crate::consts::Region; use crate::req::RegionalRequester; use crate::util::InsertOnlyCHashMap; /// For retrieving data from the Riot Games API. /// /// # Rate Limiting /// /// The Riot Game API does _dynamic_ rate limiting, meaning that rate limits are /// specified in response headers and (hypothetically) could change at any time. /// Riven keeps track of changing rate limits seamlessly, preventing you from /// getting blacklisted. /// /// Riven's rate limiting is highly efficient, meaning that it can reach the limits /// of your rate limit without going over. pub struct RiotApi { /// Configuration settings. config: RiotApiConfig, /// Client for making requests. client: Client, /// Per-region requesters. regional_requesters: InsertOnlyCHashMap, } impl RiotApi { pub fn with_config(config: RiotApiConfig) -> Self { log::trace!("Creating client (TODO: configuration)."); Self { config: config, client: Client::new(), regional_requesters: InsertOnlyCHashMap::new(), } } pub fn with_key>(api_key: T) -> Self { Self::with_config(RiotApiConfig::with_key(api_key)) } pub fn get<'a, T: serde::de::DeserializeOwned + 'a>(&'a self, method_id: &'static str, region: Region, path: String, query: Option) -> impl Future>> + 'a { // TODO: max concurrent requests? Or can configure client? self.regional_requesters .get_or_insert_with(region, || { log::debug!("Creating requester for region {}.", region.platform); RegionalRequester::new() }) .get(&self.config, &self.client, method_id, region, path, query) } }