From 678e3d3f28cb8594204dc5e2b7597ae66a4582c7 Mon Sep 17 00:00:00 2001 From: David Blajda Date: Thu, 27 Dec 2018 22:03:23 +0000 Subject: Use Id types for endpoints and implement kraken headers --- src/helix/models.rs | 7 ++++-- src/helix/namespaces/auth.rs | 31 +++--------------------- src/helix/namespaces/clips.rs | 17 +++++-------- src/helix/namespaces/mod.rs | 7 +++++- src/helix/namespaces/users.rs | 55 +++++++++++++++++------------------------- src/helix/namespaces/videos.rs | 28 +++++++++------------ 6 files changed, 55 insertions(+), 90 deletions(-) (limited to 'src/helix') diff --git a/src/helix/models.rs b/src/helix/models.rs index 47a1288..4c1566c 100644 --- a/src/helix/models.rs +++ b/src/helix/models.rs @@ -56,8 +56,11 @@ pub struct Video { pub published_at: DateTime, #[serde(with = "url_serde")] pub url: Url, - #[serde(with = "url_serde")] - pub thumbnail_url: Url, + /*FIXME: Serde will attempt to parse an empty string. + * In this case this should be None when thumbnail_url is an empty string + */ + //#[serde(with = "url_serde")] + pub thumbnail_url: String, //Option, pub viewable: String, pub view_count: i32, pub language: String, diff --git a/src/helix/namespaces/auth.rs b/src/helix/namespaces/auth.rs index 1ad5c57..1900fbf 100644 --- a/src/helix/namespaces/auth.rs +++ b/src/helix/namespaces/auth.rs @@ -1,10 +1,6 @@ -use std::collections::BTreeMap; -use crate::helix::models::Credentials; -use super::super::Client; -const ID_DOMAIN: &'static str = "id.twitch.tv"; -use super::Namespace; -use crate::client::{ClientTrait, ApiRequest}; -use reqwest::Method; +use super::*; +use crate::models::Credentials; +use crate::namespace::auth; pub struct Auth {} type AuthNamespace = Namespace; @@ -12,8 +8,7 @@ type AuthNamespace = Namespace; impl AuthNamespace { pub fn client_credentials(self, secret: &str) -> ApiRequest { - use self::client_credentials; - client_credentials(self.client, secret) + auth::client_credentials(self.client.inner, secret) } } @@ -22,21 +17,3 @@ impl Client { AuthNamespace::new(self) } } - -//TODO: Implement scopes -pub fn client_credentials(client: Client, secret: &str) - -> ApiRequest { - - let client = client.inner; - let url = - String::from("https://") + - ID_DOMAIN + "/oauth2/token"; - - let mut params = BTreeMap::new(); - params.insert("client_id", client.id()); - params.insert("client_secret", secret); - params.insert("grant_type", "client_credentials"); - params.insert("scope", ""); - - ApiRequest::new(url, params, client.clone(), Method::POST, None) -} diff --git a/src/helix/namespaces/clips.rs b/src/helix/namespaces/clips.rs index 28b66f7..1de2e33 100644 --- a/src/helix/namespaces/clips.rs +++ b/src/helix/namespaces/clips.rs @@ -1,36 +1,31 @@ -use std::collections::BTreeMap; -use super::super::models::{DataContainer, Clip}; -use super::super::Client; -use super::Namespace; - -use crate::client::{RatelimitKey, ClientTrait, ApiRequest}; +use super::*; +use super::models::{DataContainer, Clip}; +use crate::types::ClipId; pub struct Clips {} type ClipsNamespace = Namespace; impl ClipsNamespace { - pub fn clip(self, id: &str) -> ApiRequest> { + pub fn clip(self, id: &ClipId) -> ApiRequest> { use self::clip; clip(self.client, id) } } impl Client { - pub fn clips(&self) -> ClipsNamespace { ClipsNamespace::new(self) } } -use reqwest::Method; -pub fn clip(client: Client, id: &str) +pub fn clip(client: Client, id: &ClipId) -> ApiRequest> { let client = client.inner; let url = String::from("https://") + - client.domain() + "/helix/clips" + "?id=" + id; + client.domain() + "/helix/clips" + "?id=" + id.as_ref(); let params = BTreeMap::new(); diff --git a/src/helix/namespaces/mod.rs b/src/helix/namespaces/mod.rs index 1c0d08e..8adc1e3 100644 --- a/src/helix/namespaces/mod.rs +++ b/src/helix/namespaces/mod.rs @@ -1,5 +1,10 @@ use std::marker::PhantomData; -use super::Client; + +pub use super::Client; +pub use crate::client::{RatelimitKey, ClientTrait, ApiRequest, IterableApiRequest}; +pub use std::collections::BTreeMap; +pub use reqwest::Method; +pub use super::models; pub mod clips; pub mod users; diff --git a/src/helix/namespaces/users.rs b/src/helix/namespaces/users.rs index 3e4f1dd..20f2914 100644 --- a/src/helix/namespaces/users.rs +++ b/src/helix/namespaces/users.rs @@ -1,56 +1,45 @@ -use futures::future::Future; -use super::super::models::{DataContainer, PaginationContainer, User, Video, Clip}; -use super::super::Client; -use std::collections::BTreeMap; -const API_DOMAIN: &'static str = "api.twitch.tv"; -use super::Namespace; +use super::*; +use super::models::{DataContainer, User}; +use crate::types::UserId; pub struct Users {} type UsersNamespace = Namespace; impl UsersNamespace { - /* - pub fn users(self, id: Vec<&str>, login: Vec<&str>) -> impl Future, Error=reqwest::Error> { - //use self::users; - //users(self.client, id, login) + pub fn users(self, ids: Vec<&UserId>, logins: Vec<&str>) + -> ApiRequest> { + use self::users; + users(self.client, ids, logins) } - */ } -/* -impl Client { +impl Client { pub fn users(&self) -> UsersNamespace { UsersNamespace::new(self) } } -*/ -/* + pub fn users( client: Client, - id: Vec<&str>, - login: Vec<&str>, - ) -> impl Future, Error = reqwest::Error> { + ids: Vec<&UserId>, + logins: Vec<&str>, + ) -> ApiRequest> { + let client = client.inner; let url = - String::from("https://") + &String::from(API_DOMAIN) + &String::from("/helix/users"); + String::from("https://") + client.domain() + &String::from("/helix/users"); let mut params = BTreeMap::new(); - for i in id { - params.insert("id", i); + for id in ids { + params.insert("id", id.as_ref()); } - for log in login { - params.insert("login", log); + for login in logins { + params.insert("login", login); } - let request = client.client().get(&url); - let request = client.apply_standard_headers(request); - let request = request.query(¶ms); + ApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) +} - request - .send() - .map(|mut res| { - res.json::>() - }) - .and_then(|json| json) +pub fn authed_as(client: Client) -> ApiRequest> { + users(client, Vec::with_capacity(0), Vec::with_capacity(0)) } -*/ diff --git a/src/helix/namespaces/videos.rs b/src/helix/namespaces/videos.rs index a8021f3..75b702e 100644 --- a/src/helix/namespaces/videos.rs +++ b/src/helix/namespaces/videos.rs @@ -1,29 +1,25 @@ -use super::super::models::{PaginationContainer, Video}; -use super::super::Client; -use super::Namespace; - -use crate::client::{ClientTrait, RatelimitKey, IterableApiRequest}; -use std::collections::BTreeMap; -use reqwest::Method; +use super::*; +use super::models::{PaginationContainer, Video}; +use crate::types::{UserId, GameId, VideoId}; pub struct Videos {} type VideosNamespace = Namespace; impl VideosNamespace { - pub fn by_id(self, ids: Vec<&str>) + pub fn by_id(self, ids: Vec<&VideoId>) -> IterableApiRequest> { use self::by_id; by_id(self.client, ids) } - pub fn by_user(self, user_id: &str) + pub fn by_user(self, user_id: &UserId) -> IterableApiRequest> { use self::by_user; by_user(self.client, user_id) } - pub fn for_game(self, game_id: &str) + pub fn for_game(self, game_id: &GameId) -> IterableApiRequest> { use self::for_game; for_game(self.client, game_id) @@ -37,7 +33,7 @@ impl Client { } } -pub fn by_id(client: Client, ids: Vec<&str>) +pub fn by_id(client: Client, ids: Vec<&VideoId>) -> IterableApiRequest> { let client = client.inner; let url = @@ -45,34 +41,34 @@ pub fn by_id(client: Client, ids: Vec<&str>) let mut params = BTreeMap::new(); for id in ids { - params.insert("id", id); + params.insert("id", id.as_ref()); } IterableApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) } -pub fn by_user(client: Client, user_id: &str) +pub fn by_user(client: Client, user_id: &UserId) -> IterableApiRequest> { let client = client.inner; let url = String::from("https://") + client.domain() + &String::from("/helix/videos"); let mut params = BTreeMap::new(); - params.insert("user_id", user_id); + params.insert("user_id", user_id.as_ref()); IterableApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) } -pub fn for_game(client: Client, game_id: &str) +pub fn for_game(client: Client, game_id: &GameId) -> IterableApiRequest> { let client = client.inner; let url = String::from("https://") + client.domain() + &String::from("/helix/videos"); let mut params = BTreeMap::new(); - params.insert("game_id", game_id); + params.insert("game_id", game_id.as_ref()); IterableApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) -- cgit v1.2.3