diff options
author | David Blajda <blajda@hotmail.com> | 2018-12-16 21:45:58 +0000 |
---|---|---|
committer | David Blajda <blajda@hotmail.com> | 2018-12-16 21:45:58 +0000 |
commit | 23ea9413cd0e29eb451df08c5c799d64f56a1342 (patch) | |
tree | 226a2bc179e41c675aa0ed34ddc1fda7c55c277a | |
parent | 27267ed98fc839b51ae4621fd1aa230f7f068bdc (diff) |
Move endpoints functions into namespaces
-rw-r--r-- | src/bin/main.rs | 3 | ||||
-rw-r--r-- | src/deserializers.rs | 0 | ||||
-rw-r--r-- | src/helix/endpoints.rs | 119 | ||||
-rw-r--r-- | src/helix/mod.rs | 66 | ||||
-rw-r--r-- | src/helix/namespaces/clips.rs | 42 | ||||
-rw-r--r-- | src/helix/namespaces/mod.rs | 3 | ||||
-rw-r--r-- | src/helix/namespaces/users.rs | 52 | ||||
-rw-r--r-- | src/helix/namespaces/videos.rs | 54 |
8 files changed, 185 insertions, 154 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 3467711..dbaaa41 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -14,7 +14,8 @@ fn main() { let client = Client::new(client_id); - let clip = client.helix.clips() + let clip = client.helix + .clips() .clip(&"EnergeticApatheticTarsierThisIsSparta") .map_err(|err| { println!("{:?}", err); diff --git a/src/deserializers.rs b/src/deserializers.rs deleted file mode 100644 index e69de29..0000000 --- a/src/deserializers.rs +++ /dev/null diff --git a/src/helix/endpoints.rs b/src/helix/endpoints.rs deleted file mode 100644 index a02bf73..0000000 --- a/src/helix/endpoints.rs +++ /dev/null @@ -1,119 +0,0 @@ -use futures::future::Future; -use reqwest::header; -use reqwest::r#async::{RequestBuilder}; -use reqwest::r#async::Client as ReqwestClient; -use std::sync::Arc; - -use super::models::{DataContainer, PaginationContainer, User, Video, Clip}; -use super::Client; -const API_DOMAIN: &'static str = "api.twitch.tv"; - -fn apply_standard_headers(client: Client, request: RequestBuilder) - -> RequestBuilder -{ - let client_header = header::HeaderValue::from_str(client.id()).unwrap(); - - request.header("Client-ID", client_header) -} - -pub fn clip(client: Client, id: &str) - -> impl Future<Item=DataContainer<Clip>, Error=reqwest::Error> -{ - let url = - String::from("https://") + - API_DOMAIN + "/helix/clips" + "?id=" + id; - - - let request = client.client().get(&url); - let request = apply_standard_headers(client, request); - - request - .send() - .map(|mut res| { - println!("{:?}", res); - res.json::<DataContainer<Clip>>() - }) - .and_then(|json| json) - /* - GetRequest { - inner: Arc::new(GetRequestRef { - url: url.to_owned(), - returns: PhantomData, - }) - } - */ -} - -impl Client { - - - -/* - pub fn users( - &self, - id: Vec<&str>, - login: Vec<&str>, - ) -> impl Future<Item = DataContainer<User>, Error = reqwest::Error> { - let mut url = - String::from("https://") + &String::from(API_DOMAIN) + &String::from("/helix/users"); - - if id.len() > 0 || login.len() > 0 { - url.push_str("?"); - } - - if id.len() > 0 { - for index in 0..id.len() { - url.push_str("id="); - url.push_str(id[index]); - url.push('&'); - } - } - - if login.len() > 0 { - for index in 0..login.len() { - url.push_str("login="); - url.push_str(login[index]); - url.push('&'); - } - } - - - let f = self.create_client() - .get(&url) - .send() - .map(|mut res| res.json::<DataContainer<User>>()) - .and_then(|json| json); - - return f; - } - - pub fn videos( - &self, - video_id: Option<Vec<&str>>, - user_id: Option<&str>, - game_id: Option<&str>, - ) -> impl Future<Item = PaginationContainer<Video>, Error = reqwest::Error> { - let mut url = - String::from("https://") + &String::from(API_DOMAIN) + &String::from("/helix/videos"); - - url.push_str("?"); - if let Some(user_id) = user_id { - url.push_str("user_id="); - url.push_str(user_id); - url.push('&'); - } - - let f = self.create_client() - .get(&url) - .send() - .map(|mut res| { - res.json::<PaginationContainer<Video>>() - }) - .and_then(|json| json); - - return f; - } -*/ - -} - diff --git a/src/helix/mod.rs b/src/helix/mod.rs index fe873dd..650c4b0 100644 --- a/src/helix/mod.rs +++ b/src/helix/mod.rs @@ -4,9 +4,8 @@ use reqwest::r#async::Client as ReqwestClient; pub use super::types; use std::marker::PhantomData; - -pub mod endpoints; pub mod models; +pub mod namespaces; use std::collections::HashSet; @@ -18,10 +17,6 @@ pub trait UsersEndpoint {} pub trait VideosEndpoint {} -pub trait ClipsEndpointTrait { - fn clip(&self, id: &str) -> EndPointResult<DataContainer<Clip>>; -} - pub trait AuthEndpoint {} pub struct Namespace<T> { @@ -38,10 +33,6 @@ impl<T> Namespace<T> { } } -pub struct Clips {} - -type ClipsNamespace = Namespace<Clips>; - #[derive(Clone)] pub struct Client { inner: Arc<ClientRef>, @@ -79,13 +70,6 @@ impl Client { }) } } -} - - -use reqwest::r#async::{RequestBuilder}; -use reqwest::header; - -impl Client { pub fn id(&self) -> &str { &self.inner.id @@ -95,6 +79,30 @@ impl Client { &self.inner.client } + pub fn authenticated(&self) -> bool { + let mut_data = self.inner.inner.lock().unwrap(); + mut_data.token.is_some() + } + + /* + pub fn scopes(&self) -> Vec<Scope> { + let mut_data = self.inner.inner.lock().unwrap(); + (&mut_data.scopes).into_iter().to_owned().collect() + } + */ + + pub fn authenticate(self) -> AuthClientBuilder { + AuthClientBuilder::new(self) + } + + pub fn deauthenticate(self) -> Client { + let mut_data = self.inner.inner.lock().unwrap(); + match &mut_data.previous { + Some(old_client) => old_client.clone(), + None => self.clone() + } + } + fn apply_standard_headers(&self, request: RequestBuilder) -> RequestBuilder { @@ -103,37 +111,27 @@ impl Client { } } -impl Client { - - pub fn clips(&self) -> ClipsNamespace { - ClipsNamespace::new(self) - } -} +use reqwest::r#async::{RequestBuilder}; +use reqwest::header; -impl ClipsNamespace { - pub fn clip(self, id: &str) -> impl Future<Item=DataContainer<Clip>, Error=reqwest::Error> { - use self::endpoints::clip; - clip(self.client, id) - } -} pub struct AuthClientBuilder { scopes: HashSet<Scope>, - secret: String, + secret: Option<String>, + token: Option<String>, client: Client, /*If the user supplies a token, * then we can skip fetching it from the server and are authenticated */ - token: Option<String>, } impl AuthClientBuilder { - pub fn new(client: Client, secret: &str) -> AuthClientBuilder { + pub fn new(client: Client) -> AuthClientBuilder { AuthClientBuilder { scopes: HashSet::new(), - secret: secret.to_owned(), client: client, + secret: None, token: None, } } @@ -164,7 +162,7 @@ impl AuthClientBuilder { } -#[derive(PartialEq, Hash, Eq)] +#[derive(PartialEq, Hash, Eq, Clone)] pub enum Scope { AnalyticsReadExtensions, AnalyticsReadGames, diff --git a/src/helix/namespaces/clips.rs b/src/helix/namespaces/clips.rs new file mode 100644 index 0000000..32793c0 --- /dev/null +++ b/src/helix/namespaces/clips.rs @@ -0,0 +1,42 @@ +use futures::future::Future; +use super::super::models::{DataContainer, PaginationContainer, User, Video, Clip}; +use super::super::Client; +const API_DOMAIN: &'static str = "api.twitch.tv"; +use super::super::Namespace; + +pub struct Clips {} +type ClipsNamespace = Namespace<Clips>; + +impl ClipsNamespace { + pub fn clip(self, id: &str) -> impl Future<Item=DataContainer<Clip>, Error=reqwest::Error> { + use self::clip; + clip(self.client, id) + } +} + +impl Client { + + pub fn clips(&self) -> ClipsNamespace { + ClipsNamespace::new(self) + } +} + +pub fn clip(client: Client, id: &str) + -> impl Future<Item=DataContainer<Clip>, Error=reqwest::Error> +{ + let url = + String::from("https://") + + API_DOMAIN + "/helix/clips" + "?id=" + id; + + + let request = client.client().get(&url); + let request = client.apply_standard_headers(request); + + request + .send() + .map(|mut res| { + println!("{:?}", res); + res.json::<DataContainer<Clip>>() + }) + .and_then(|json| json) +} diff --git a/src/helix/namespaces/mod.rs b/src/helix/namespaces/mod.rs new file mode 100644 index 0000000..ad73aa3 --- /dev/null +++ b/src/helix/namespaces/mod.rs @@ -0,0 +1,3 @@ +pub mod clips; +pub mod users; +pub mod videos; diff --git a/src/helix/namespaces/users.rs b/src/helix/namespaces/users.rs new file mode 100644 index 0000000..c809b95 --- /dev/null +++ b/src/helix/namespaces/users.rs @@ -0,0 +1,52 @@ +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::super::Namespace; + +pub struct Users {} +type UsersNamespace = Namespace<Users>; + +impl UsersNamespace { + pub fn users(self, id: Vec<&str>, login: Vec<&str>) -> impl Future<Item=DataContainer<User>, Error=reqwest::Error> { + use self::users; + users(self.client, id, login) + } +} + +impl Client { + + pub fn users(&self) -> UsersNamespace { + UsersNamespace::new(self) + } +} + +pub fn users( + client: Client, + id: Vec<&str>, + login: Vec<&str>, + ) -> impl Future<Item = DataContainer<User>, Error = reqwest::Error> { + let url = + String::from("https://") + &String::from(API_DOMAIN) + &String::from("/helix/users"); + + let mut params = BTreeMap::new(); + for i in id { + params.insert("id", i); + } + + for log in login { + params.insert("login", log); + } + + let request = client.client().get(&url); + let request = client.apply_standard_headers(request); + let request = request.query(¶ms); + + request + .send() + .map(|mut res| { + res.json::<DataContainer<User>>() + }) + .and_then(|json| json) +} diff --git a/src/helix/namespaces/videos.rs b/src/helix/namespaces/videos.rs new file mode 100644 index 0000000..ad5ca28 --- /dev/null +++ b/src/helix/namespaces/videos.rs @@ -0,0 +1,54 @@ +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::super::Namespace; + +pub struct Videos {} +type VideosNamespace = Namespace<Videos>; + +impl VideosNamespace { + /* + pub fn videos(self, video_id) -> impl Future<Item=DataContainer<User>, Error=reqwest::Error> { + use self::videos; + users(self.client, id, login) + } + */ +} + +impl Client { + + pub fn videos(&self) -> VideosNamespace { + VideosNamespace::new(self) + } +} + +/* +pub fn videos( + client: Client, + video_id: Option<Vec<&str>>, + user_id: Option<&str>, + game_id: Option<&str>, +) -> impl Future<Item = PaginationContainer<Video>, Error = reqwest::Error> { + let mut url = + String::from("https://") + &String::from(API_DOMAIN) + &String::from("/helix/videos"); + + let mut params = BTreeMap::new(); + for user in user_id { + params.insert("user_id", user); + } + + let request = client.client().get(&url); + let request = client.apply_standard_headers(request); + let request = request.query(¶ms); + + request + .send() + .map(|mut res| { + res.json::<PaginationContainer<Video>>() + }) + .and_then(|json| json) + +} +*/ |