From 98fb79b85e3cfbb547e5340b30623511daf09ef5 Mon Sep 17 00:00:00 2001 From: David Blajda Date: Wed, 8 May 2019 15:38:37 +0000 Subject: :WIP: Move types to a different crate --- src/helix/mod.rs | 83 ----------------------------- src/helix/models.rs | 118 ----------------------------------------- src/helix/namespaces/auth.rs | 19 ------- src/helix/namespaces/clips.rs | 32 ----------- src/helix/namespaces/mod.rs | 26 --------- src/helix/namespaces/users.rs | 44 --------------- src/helix/namespaces/videos.rs | 75 -------------------------- 7 files changed, 397 deletions(-) delete mode 100644 src/helix/mod.rs delete mode 100644 src/helix/models.rs delete mode 100644 src/helix/namespaces/auth.rs delete mode 100644 src/helix/namespaces/clips.rs delete mode 100644 src/helix/namespaces/mod.rs delete mode 100644 src/helix/namespaces/users.rs delete mode 100644 src/helix/namespaces/videos.rs (limited to 'src/helix') diff --git a/src/helix/mod.rs b/src/helix/mod.rs deleted file mode 100644 index da24aa4..0000000 --- a/src/helix/mod.rs +++ /dev/null @@ -1,83 +0,0 @@ -use crate::client::Client as GenericClient; -use crate::client::{Version, ClientConfig}; -use crate::client::ClientTrait; - -use crate::client::{HelixScope, Scope}; - -pub mod models; -pub mod namespaces; - - -#[derive(Clone)] -pub struct Client { - inner: GenericClient -} - -impl Client { - pub fn new(id: &str) -> Client { - let config = ClientConfig::default(); - Client { - inner: GenericClient::new(id, config, Version::Helix) - } - } - - pub fn new_with_config(id: &str, config: ClientConfig) -> Client { - Client { - inner: GenericClient::new(id, config, Version::Helix) - } - } - - pub fn authenticate(self, secret: &str) -> AuthClientBuilder { - AuthClientBuilder::new(self, secret) - } - - pub fn id<'a>(&'a self) -> &'a str { &self.inner.id() } - pub fn domain<'a>(&'a self) -> &'a str { &self.inner.domain() } - pub fn auth_domain<'a>(&'a self) -> &'a str { &self.inner.auth_domain() } - pub fn authenticated(&self) -> bool { self.inner.authenticated() } - - pub fn scopes(&self) -> Vec { - self.inner.scopes().into_iter().filter_map(|item| { - if let Scope::Helix(scope) = item { Some(scope) } else { None } - }).collect() - } -} - -use crate::client::AuthClientBuilder as GenericAuthClientBuilder; - -pub struct AuthClientBuilder { - inner: GenericAuthClientBuilder, -} - -impl AuthClientBuilder { - pub fn new(client: Client, secret: &str) -> AuthClientBuilder { - AuthClientBuilder { - inner: GenericAuthClientBuilder::new(client.inner, secret), - } - } - - pub fn build(self) -> Client { - let client = self.inner.build(); - Client { - inner: client - } - } - - pub fn scope(self, scope: HelixScope) -> AuthClientBuilder { - AuthClientBuilder { - inner: self.inner.scope(Scope::Helix(scope)) - } - } - - pub fn scopes(self, scopes: Vec) -> AuthClientBuilder { - AuthClientBuilder { - inner: self.inner.scopes(scopes.into_iter().map(|e| Scope::Helix(e)).collect()) - } - } - - pub fn token(self, token: &str) -> AuthClientBuilder { - AuthClientBuilder { - inner: self.inner.token(token) - } - } -} diff --git a/src/helix/models.rs b/src/helix/models.rs deleted file mode 100644 index 1e9f0ec..0000000 --- a/src/helix/models.rs +++ /dev/null @@ -1,118 +0,0 @@ -extern crate serde_json; -extern crate chrono; - -use url::Url; -use chrono::{DateTime, Utc}; -use crate::types::{UserId, VideoId, ChannelId}; - -use crate::client::PaginationTrait; - -#[derive(Debug, Deserialize, Serialize)] -pub struct DataContainer { - pub data: Vec -} - -impl PaginationTrait for DataContainer { - fn cursor<'a>(&'a self) -> Option<&'a str> { None } -} - -impl PaginationTrait for PaginationContainer { - fn cursor<'a>(&'a self) -> Option<&'a str> { - match self.pagination.as_ref() { - Some(cursor) => { - match cursor.cursor.as_ref() { - Some(cursor) => Some(cursor), - None => None, - } - }, - None => None - } - } -} - -impl PaginationTrait for Credentials { - fn cursor<'a>(&'a self) -> Option<&'a str> { None } -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct PaginationContainer { - pub data: Vec, - pub pagination: Option -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct Cursor { - pub cursor: Option -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct Video { - pub id: VideoId, - pub user_id: UserId, - pub user_name: String, - pub title: String, - pub description: String, - pub created_at: DateTime, - pub published_at: DateTime, - #[serde(with = "url_serde")] - pub 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, - #[serde(rename = "type")] - pub video_type: String, - //Should be converted to a Duration - pub duration: String, -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct User { - pub id: UserId, - pub login: String, - pub display_name: String, - #[serde(rename = "type")] - pub user_type: String, - pub broadcaster_type: String, - pub description: String, - #[serde(with = "url_serde")] - pub profile_image_url: Url, - //#[serde(with = "url_serde")] - pub offline_image_url: String, // Option, - pub view_count: u32, - pub email: Option, -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct Clip { - pub id: String, - #[serde(with = "url_serde")] - pub url: Url, - #[serde(with = "url_serde")] - pub embed_url: Url, - pub broadcaster_id: ChannelId, - pub broadcaster_name: String, - pub creator_id: UserId, - pub creator_name: String, - pub video_id: VideoId, - pub game_id: String, - pub language: String, - pub title: String, - pub created_at: DateTime, - #[serde(with = "url_serde")] - pub thumbnail_url: Url, - pub view_count: i32, -} - -#[derive(Debug, Deserialize, Serialize)] -pub struct Credentials { - pub access_token: String, - pub refresh_token: Option, - pub expires_in: u32, - pub scope: Option>, - pub token_type: String, -} diff --git a/src/helix/namespaces/auth.rs b/src/helix/namespaces/auth.rs deleted file mode 100644 index d006e2f..0000000 --- a/src/helix/namespaces/auth.rs +++ /dev/null @@ -1,19 +0,0 @@ -use super::*; -use crate::models::Credentials; -use crate::namespace::auth; - -pub struct Auth {} -type AuthNamespace = Namespace; - -impl AuthNamespace { - pub fn client_credentials(self, secret: &str) - -> ApiRequest { - auth::client_credentials(self.client.inner, &secret) - } -} - -impl Client { - pub fn auth(&self) -> AuthNamespace { - AuthNamespace::new(self) - } -} diff --git a/src/helix/namespaces/clips.rs b/src/helix/namespaces/clips.rs deleted file mode 100644 index c4595ce..0000000 --- a/src/helix/namespaces/clips.rs +++ /dev/null @@ -1,32 +0,0 @@ -use super::*; -use super::models::{DataContainer, Clip}; - -pub struct Clips {} -type ClipsNamespace = Namespace; - -impl ClipsNamespace { - pub fn clip(self, id: &S) -> ApiRequest> { - use self::clip; - clip(self.client, id) - } -} - -impl Client { - pub fn clips(&self) -> ClipsNamespace { - ClipsNamespace::new(self) - } -} - - -pub fn clip(client: Client, id: &S) - -> ApiRequest> -{ - let client = client.inner; - let url = - String::from("https://") + - client.domain() + "/helix/clips" + "?id=" + &id.to_string(); - - let params : ParamList = BTreeMap::new(); - - ApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) -} diff --git a/src/helix/namespaces/mod.rs b/src/helix/namespaces/mod.rs deleted file mode 100644 index 1d4239a..0000000 --- a/src/helix/namespaces/mod.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::marker::PhantomData; - -pub use super::Client; -pub use crate::client::{RatelimitKey, ClientTrait, ApiRequest, IterableApiRequest, ParamList}; -pub use std::collections::BTreeMap; -pub use reqwest::Method; -pub use super::models; - -pub mod clips; -pub mod users; -pub mod videos; -pub mod auth; - -pub struct Namespace { - client: Client, - _type: PhantomData -} - -impl Namespace { - pub fn new(client: &Client) -> Self { - Namespace { - client: client.clone(), - _type: PhantomData, - } - } -} diff --git a/src/helix/namespaces/users.rs b/src/helix/namespaces/users.rs deleted file mode 100644 index 2e922d6..0000000 --- a/src/helix/namespaces/users.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::*; -use super::models::{DataContainer, User}; -use std::string::ToString; - -pub struct Users {} -type UsersNamespace = Namespace; - -impl UsersNamespace { - pub fn users(self, ids: &[S], logins: &[S]) -> ApiRequest> { - use self::users; - users(self.client, ids, logins) - } -} - -impl Client { - pub fn users(&self) -> UsersNamespace { - UsersNamespace::new(self) - } -} - -pub fn users( - client: Client, - ids: &[S], - logins: &[S], - ) -> ApiRequest> { - let client = client.inner; - let url = - String::from("https://") + client.domain() + &String::from("/helix/users"); - - let mut params: BTreeMap<&str, &dyn ToString> = BTreeMap::new(); - for id in ids { - params.insert("id", id); - } - - for login in logins { - params.insert("login", login); - } - - ApiRequest::new(url, params, client, Method::GET, Some(RatelimitKey::Default)) -} - -pub fn authed_as(client: Client) -> ApiRequest> { - users(client, &[""], &[""]) -} diff --git a/src/helix/namespaces/videos.rs b/src/helix/namespaces/videos.rs deleted file mode 100644 index b603b8f..0000000 --- a/src/helix/namespaces/videos.rs +++ /dev/null @@ -1,75 +0,0 @@ -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: &[S]) - -> IterableApiRequest> { - use self::by_id; - by_id(self.client, ids) - } - - pub fn by_user(self, user_id: &S) - -> IterableApiRequest> { - use self::by_user; - by_user(self.client, user_id) - } - - pub fn for_game(self, game_id: &S) - -> IterableApiRequest> { - use self::for_game; - for_game(self.client, game_id) - } -} - -impl Client { - - pub fn videos(&self) -> VideosNamespace { - VideosNamespace::new(self) - } -} - -pub fn by_id(client: Client, ids: &[S]) - -> IterableApiRequest> { - let client = client.inner; - let url = - String::from("https://") + client.domain() + &String::from("/helix/videos"); - - let mut params: ParamList = BTreeMap::new(); - for id in ids { - params.insert("id", id); - } - - IterableApiRequest::new(url, params, client, - Method::GET, Some(RatelimitKey::Default)) -} - -pub fn by_user(client: Client, user_id: &S) - -> IterableApiRequest> { - let client = client.inner; - let url = - String::from("https://") + client.domain() + &String::from("/helix/videos"); - - let mut params: ParamList = BTreeMap::new(); - params.insert("user_id", user_id); - - IterableApiRequest::new(url, params, client, - Method::GET, Some(RatelimitKey::Default)) -} - -pub fn for_game(client: Client, game_id: &S) - -> IterableApiRequest> { - let client = client.inner; - let url = - String::from("https://") + client.domain() + &String::from("/helix/videos"); - - let mut params: ParamList = BTreeMap::new(); - params.insert("game_id", game_id); - - IterableApiRequest::new(url, params, client, - Method::GET, Some(RatelimitKey::Default)) -} -- cgit v1.2.3