first request made :)

pull/5/head
Mingwei Samuel 2019-10-17 16:00:04 -07:00
parent d8d2492c93
commit ee75f75e0c
7 changed files with 69 additions and 13 deletions

View File

@ -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"

View File

@ -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,
};

View File

@ -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::<u32>("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());
}
}

View File

@ -69,7 +69,8 @@ impl RateLimit {
}
pub fn on_response(&self, _response: &reqwest::Response) {
unimplemented!();
return;
// TODO!!!!!!!!!!
}
}

View File

@ -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<T: serde::de::DeserializeOwned>(
&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<Option<T>, 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
}
}

26
src/riot_api.rs Normal file
View File

@ -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<T: serde::de::DeserializeOwned>(
&'a self, method_id: &'a str, region: &'a Region<'a>, relative_url: &'_ str,
query: &[(&'_ str, &'_ str)]) -> Result<Option<T>, reqwest::Error>
{
self.requester_manager.get(method_id, region, relative_url, query).await
}
}

View File

@ -2,3 +2,12 @@ pub struct RiotApiConfig<'a> {
pub api_key: &'a str,
pub retries: u8,
}
impl<'a> RiotApiConfig<'a> {
pub fn with_key(api_key: &'a str) -> Self {
Self {
api_key: api_key,
retries: 3 // TODO defaults.
}
}
}