summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 142afc8e304099cfa9e703f3210667d4291925aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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=serde_json::Value, Error=reqwest::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 {
            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 client = Client::builder()
                        .default_headers(headers)
                        .build().unwrap();
        

        let mut f = client
                        .get(&url)
                        .send()
                        .map(|mut res| {
                            res.json::<serde_json::Value>()
                        })
                        .and_then(|json| {
                            json
                        });

        return Box::new(f);
    }
}