summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorDavid Blajda <blajda@hotmail.com>2018-12-13 20:56:55 +0000
committerDavid Blajda <blajda@hotmail.com>2018-12-13 20:56:55 +0000
commit8615cc2f030240ba2982dba893fe63f11a0c8a88 (patch)
tree30c7103625323404696331c2384130ce06f8bc96 /src/lib.rs
parent54bcdf63d941dbbd0b3e565259515f013f1ecd13 (diff)
Restructure project and included kraken endpoint
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs122
1 files changed, 9 insertions, 113 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 450ffde..06239b9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,126 +6,22 @@ extern crate chrono;
#[macro_use]
extern crate serde_derive;
-pub mod models;
+mod helix;
+mod kraken;
-use futures::future::Future;
-use reqwest::header;
-use reqwest::r#async::{Chunk, Decoder, Request, Response};
-use reqwest::r#async::Client as ReqwestClient;
-
-use self::models::{DataContainer, PaginationContainer, User, Video, Clip};
-
-const API_DOMAIN: &'static str = "api.twitch.tv";
-
-/* When Client owns a ReqwestClient, any futures spawned do not immediately
- * terminate but 'hang'. When creating a new client for each request this problem
- * does not occur. This would need to be resolved so we can benefit from keep alive
- * connections.
- *
- */
+pub use self::helix::endpoints::Client as HelixClient;
+pub use self::kraken::Client as KrakenClient;
pub struct Client {
- id: String,
+ pub helix: HelixClient,
+ pub kraken: KrakenClient,
}
impl Client {
pub fn new(client_id: &str) -> Client {
- Client {
- id: client_id.to_owned(),
- }
- }
-
- fn create_client(&self) -> ReqwestClient {
- let mut headers = header::HeaderMap::new();
- let auth_key = &self.id;
- let header_value = header::HeaderValue::from_str(auth_key).unwrap();
- headers.insert("Client-ID", header_value);
-
- let client = ReqwestClient::builder().default_headers(headers).build().unwrap();
- 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('&');
- }
+ Client {
+ helix: HelixClient::new(client_id),
+ kraken: KrakenClient::new(client_id),
}
-
-
- 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;
- }
-
- pub fn clip(&self, id: &str)
- -> impl Future<Item=DataContainer<Clip>, Error=reqwest::Error>
- {
- let url =
- String::from("https://") +
- API_DOMAIN + "/helix/clips" + "?id=" + id;
-
- let f = self.create_client()
- .get(&url)
- .send()
- .map(|mut res| {
- println!("{:?}", res);
- res.json::<DataContainer<Clip>>()
- })
- .and_then(|json| json);
-
- return f;
}
}