summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Blajda <blajda@hotmail.com>2018-12-09 21:17:11 +0000
committerDavid Blajda <blajda@hotmail.com>2018-12-09 21:17:11 +0000
commit17c6e1ecf82d5a68eccdd62f7a9d757d101759fb (patch)
treef94e1bd4f0dd761074d680048c809801da4bb399
Init commit. Get json using async reqwest
-rw-r--r--.gitignore2
-rw-r--r--Cargo.toml13
-rw-r--r--src/bin/main.rs14
-rw-r--r--src/lib.rs97
4 files changed, 126 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..53eaa21
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/target
+**/*.rs.bk
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..0232d21
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "twitch_api"
+version = "0.1.0"
+authors = ["David Blajda <blajda@hotmail.com>"]
+edition = "2018"
+
+[dependencies]
+reqwest = "0.9.5"
+futures = "0.1.25"
+tokio = "0.1.13"
+dotenv = "0.13.0"
+serde = "1.0.81"
+serde_json = "1.0.33"
diff --git a/src/bin/main.rs b/src/bin/main.rs
new file mode 100644
index 0000000..8a4c870
--- /dev/null
+++ b/src/bin/main.rs
@@ -0,0 +1,14 @@
+extern crate twitch_api;
+extern crate tokio;
+extern crate dotenv;
+
+use twitch_api::TwitchApi;
+use std::env;
+
+fn main() {
+ dotenv::dotenv().unwrap();
+ let mut twitch_api = TwitchApi::new(env::var("TWITCH_API").unwrap());
+ let users = twitch_api.users(vec![], vec!["shroud"]);
+
+ tokio::run(users);
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..6b75ba0
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,97 @@
+extern crate reqwest;
+extern crate futures;
+extern crate serde;
+
+use reqwest::r#async::{Client, Request, Chunk, Response, Decoder};
+use reqwest::header;
+use futures::future::Future;
+use std::iter::Iterator;
+use futures::Stream;
+
+const API_DOMAIN: &'static str = "api.twitch.tv";
+
+pub struct TwitchApi {
+ client_id: String,
+}
+
+impl TwitchApi {
+ pub fn new(client_id: String) -> TwitchApi {
+ TwitchApi {
+ client_id
+ }
+ }
+
+ pub fn users(&mut self, id: Vec<&str>, login: Vec<&str>) -> Box<Future<Item=(), Error=()> + Send> {
+ let mut headers = header::HeaderMap::new();
+ let auth_key = &self.client_id;
+ let header_value = header::HeaderValue::from_str(&auth_key).unwrap();
+ headers.insert("Client-ID", header_value);
+ 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 {
+ url.push_str("id=");
+ for index in 0..id.len() {
+ if index != id.len() - 1 {
+ url.push_str(id[index]);
+ url.push(',');
+ } else {
+ url.push_str(id[index]);
+ }
+ }
+ }
+
+ if id.len() > 0 && login.len() > 0 {
+ url.push_str("&");
+ }
+
+ if login.len() > 0 {
+ url.push_str("login=");
+ for index in 0..login.len() {
+ if index != login.len() - 1 {
+ url.push_str(login[index]);
+ url.push(',');
+ } else {
+ url.push_str(login[index]);
+ }
+ }
+ }
+
+ let client = Client::builder()
+ .default_headers(headers)
+ .build().unwrap();
+
+
+ let mut response = client
+ .get(&url)
+ .send();
+
+ let f = response
+ .map_err(|_| ())
+ .and_then(|res| {
+ let decoder = res.into_body();
+ decoder.collect()
+ .map(|chunks| {
+ let mut data: Vec<u8> = Vec::new();
+ for chunk in chunks {
+ for byte in chunk {
+ data.push(byte);
+ }
+ }
+ data
+ })
+ .map(|data: Vec<u8>| {
+ let s = String::from_utf8_lossy(&data[..]);
+ let j = serde_json::from_str::<serde_json::Value>(&s);
+ println!("{:?}", j);
+ ()
+ })
+ .map_err(|_| ())
+ });
+
+ return Box::new(f);
+ }
+}