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
// docbase_io -- docbase.io cli written in Rust.
// Copyright 2016 Pocket7878 <poketo7878@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// A copy of the License has been included in the root of the repository.
extern crate rustc_serialize;

use hyper::client;
use hyper::header::Headers;
use std::io::Read;
use rustc_serialize::json;

use models::team;
use models::group;
use models::post_search_result;
use models::post;
use models::tag;
use client::request_builder::team_request_builder::TeamRequestBuilder;

mod request_builder;

header! { (XDocBaseToken, "X-DocbaseToken") => [String] }

pub struct Client {
    pub api_key: String,
}

impl Client {
    pub fn teams(&self) -> Vec<team::Team> {
        let client = client::Client::new();
        let mut headers = Headers::new();
        headers.set(XDocBaseToken(self.api_key.to_owned()));
        let mut res = client.get("https://api.docbase.io/teams").headers(headers).send().unwrap();
        let mut buffer = String::new();
        res.read_to_string(&mut buffer).unwrap();
        let teams: Vec<team::Team> = json::decode(&buffer).unwrap();
        return teams;
    }

    pub fn post_detail(&self, domain: &str, post_id: u32) -> post::Post {
        let client = client::Client::new();
        let mut headers = Headers::new();
        headers.set(XDocBaseToken(self.api_key.to_owned()));
        let endpoint_url = format!("https://api.docbase.io/teams/{}/posts/{}", domain, post_id);
        let mut res = client.get(&endpoint_url).headers(headers).send().unwrap();
        let mut buffer = String::new();
        res.read_to_string(&mut buffer).unwrap();
        let post: post::Post = json::decode(&buffer).unwrap();
        return post;
    }

    pub fn load_prev_post_search_result(&self,
                                        res: &post_search_result::PostSearchResult)
                                        -> post_search_result::PostSearchResult {
        match res.meta.previous_page.to_owned() {
            Some(prev_url) => {
                let client = client::Client::new();
                let mut headers = Headers::new();
                headers.set(XDocBaseToken(self.api_key.to_owned()));
                let mut res = client.get(&prev_url).headers(headers).send().unwrap();
                let mut buffer = String::new();
                res.read_to_string(&mut buffer).unwrap();
                let searchResult: post_search_result::PostSearchResult = json::decode(&buffer)
                    .unwrap();
                return searchResult;
            }
            None => {
                panic!("No previous url");
            }
        }
    }

    pub fn load_next_post_search_result(&self,
                                        res: &post_search_result::PostSearchResult)
                                        -> post_search_result::PostSearchResult {
        match res.meta.next_page.to_owned() {
            Some(next_url) => {
                let client = client::Client::new();
                let mut headers = Headers::new();
                headers.set(XDocBaseToken(self.api_key.to_owned()));
                let mut res = client.get(&next_url).headers(headers).send().unwrap();
                let mut buffer = String::new();
                res.read_to_string(&mut buffer).unwrap();
                let searchResult: post_search_result::PostSearchResult = json::decode(&buffer)
                    .unwrap();
                return searchResult;
            }
            None => {
                panic!("No next url");
            }
        }
    }

    pub fn team(&self, team: String) -> TeamRequestBuilder {
        return TeamRequestBuilder::new(self.api_key.to_owned(), team);
    }
}