summaryrefslogtreecommitdiff
path: root/src/helix/endpoints.rs
blob: bb61ae9ae80394b48f5d777e711c20e422484f2f (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use futures::future::Future;
use reqwest::header;
use reqwest::r#async::{RequestBuilder};
use reqwest::r#async::Client as ReqwestClient;

use super::models::{DataContainer, PaginationContainer, User, Video, Clip};
use super::Client; 
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.
 */

impl Client {

   fn apply_standard_headers(&self, request: RequestBuilder) 
       -> RequestBuilder 
    {
        let client_header = header::HeaderValue::from_str(&self.inner.id).unwrap();

        request.header("Client-ID", client_header)
    }

/*
    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;
    }
*/

    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 request = self.inner.client.get(&url);
        let request = self.apply_standard_headers(request);

        request
            .send()
            .map(|mut res| {
                println!("{:?}", res);
                res.json::<DataContainer<Clip>>()
            })
            .and_then(|json| json)
    }
}