From ee75f75e0c01b0f3259cf1371846ff49638422d1 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Thu, 17 Oct 2019 16:00:04 -0700 Subject: [PATCH] first request made :) --- Cargo.toml | 1 + src/consts/region.rs | 2 +- src/lib.rs | 22 ++++++++++++++++++++-- src/req/rate_limit.rs | 3 ++- src/req/requester_manager.rs | 17 +++++++++-------- src/riot_api.rs | 26 ++++++++++++++++++++++++++ src/riot_api_config.rs | 11 ++++++++++- 7 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 src/riot_api.rs diff --git a/Cargo.toml b/Cargo.toml index 5180364..c3cfa8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ async-std = "0.99" parking_lot = { version = "0.9", features = [ "nightly" ] } reqwest = { version = "0.10.0-alpha.1", features = [ "gzip", "json" ] } serde = "^1.0" +tokio = "0.2.0-alpha.6" diff --git a/src/consts/region.rs b/src/consts/region.rs index f439466..5722f48 100644 --- a/src/consts/region.rs +++ b/src/consts/region.rs @@ -11,7 +11,7 @@ macro_rules! regions { )* ) => { $( - const $key: &'static Region<'static> = &Region { + pub const $key: &'static Region<'static> = &Region { key: stringify!($key), platform: $plat, }; diff --git a/src/lib.rs b/src/lib.rs index 5c06ea9..1b0bdd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,14 +2,32 @@ pub mod consts; +mod riot_api_config; +pub use riot_api_config::*; + +mod riot_api; +pub use riot_api::*; + mod req; mod util; -mod riot_api_config; #[cfg(test)] mod tests { + use tokio::runtime::Runtime; + use super::*; + + const api_key: &'static str = "RGAPI-nothinghereowo"; + #[test] fn it_works() { - assert_eq!(2 + 2, 4); + let rt = Runtime::new().unwrap(); + let riot_api = RiotApi::with_key(api_key); + + // https://na1.api.riotgames.com/lol/champion-mastery/v4/scores/by-summoner/SBM8Ubipo4ge2yj7bhEzL7yvV0C9Oc1XA2l6v5okGMA_nCw + let my_future = riot_api.get::("asdf", consts::Region::NA, + "/lol/champion-mastery/v4/scores/by-summoner/SBM8Ubipo4ge2yj7bhEzL7yvV0C9Oc1XA2l6v5okGMA_nCw", + &[]); + let val = rt.block_on(my_future).unwrap(); + println!("VAL: {}", val.unwrap()); } } diff --git a/src/req/rate_limit.rs b/src/req/rate_limit.rs index 9fddc97..d75dbca 100644 --- a/src/req/rate_limit.rs +++ b/src/req/rate_limit.rs @@ -69,7 +69,8 @@ impl RateLimit { } pub fn on_response(&self, _response: &reqwest::Response) { - unimplemented!(); + return; + // TODO!!!!!!!!!! } } diff --git a/src/req/requester_manager.rs b/src/req/requester_manager.rs index 55174fd..b6b8320 100644 --- a/src/req/requester_manager.rs +++ b/src/req/requester_manager.rs @@ -1,9 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; -use reqwest::{ - Client, -}; +use reqwest::Client; use crate::riot_api_config::RiotApiConfig; use crate::consts::Region; @@ -13,16 +11,18 @@ use super::RegionalRequester; pub struct RequesterManager<'a> { /// Configuration settings. - riot_api_config: &'a RiotApiConfig<'a>, + riot_api_config: RiotApiConfig<'a>, /// Client for making requests. - client: &'a Client, + client: Client, /// Per-region requesters. regional_requesters: InsertOnlyCHashMap<&'a Region<'a>, RegionalRequester<'a>>, } impl<'a> RequesterManager<'a> { - pub fn new(riot_api_config: &'a RiotApiConfig<'a>, client: &'a Client) -> Self { + pub fn new(riot_api_config: RiotApiConfig<'a>) -> Self { + // TODO client. + let client = Client::new(); Self { riot_api_config: riot_api_config, client: client, @@ -31,11 +31,12 @@ impl<'a> RequesterManager<'a> { } pub async fn get( - &mut self, method_id: &'a str, region: &'a Region<'a>, relative_url: &'_ str, + &'a self, method_id: &'a str, region: &'a Region<'a>, relative_url: &'_ str, query: &[(&'_ str, &'_ str)]) -> Result, reqwest::Error> { + // TODO: max concurrent requests? let regional_requester = self.regional_requesters - .get_or_insert_with(region, || RegionalRequester::new(self.riot_api_config, self.client)); + .get_or_insert_with(region, || RegionalRequester::new(&self.riot_api_config, &self.client)); regional_requester.get(method_id, region, relative_url, query).await } } diff --git a/src/riot_api.rs b/src/riot_api.rs new file mode 100644 index 0000000..345b857 --- /dev/null +++ b/src/riot_api.rs @@ -0,0 +1,26 @@ +use crate::RiotApiConfig; +use crate::consts::Region; +use crate::req::RequesterManager; + +pub struct RiotApi<'a> { + requester_manager: RequesterManager<'a>, +} + +impl<'a> RiotApi<'a> { + pub fn with_config(config: RiotApiConfig<'a>) -> Self { + Self { + requester_manager: RequesterManager::new(config), + } + } + + pub fn with_key(api_key: &'a str) -> Self { + Self::with_config(RiotApiConfig::with_key(api_key)) + } + + pub async fn get( + &'a self, method_id: &'a str, region: &'a Region<'a>, relative_url: &'_ str, + query: &[(&'_ str, &'_ str)]) -> Result, reqwest::Error> + { + self.requester_manager.get(method_id, region, relative_url, query).await + } +} diff --git a/src/riot_api_config.rs b/src/riot_api_config.rs index f033646..40d652b 100644 --- a/src/riot_api_config.rs +++ b/src/riot_api_config.rs @@ -1,4 +1,13 @@ pub struct RiotApiConfig<'a> { pub api_key: &'a str, pub retries: u8, -} \ No newline at end of file +} + +impl<'a> RiotApiConfig<'a> { + pub fn with_key(api_key: &'a str) -> Self { + Self { + api_key: api_key, + retries: 3 // TODO defaults. + } + } +}