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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate reqwest;

pub mod structs;
use self::structs::*;


pub struct Client {
    auth_token:   String,
    base_url:     String,
    client:       reqwest::Client,
}

impl Client {
    /// Creates a new client for making requests
    /// # Arguments
    /// * `auth_token` - The authorization token provided in your account
    /// * `base_url`   - The url with no path
    ///
    /// # Examples
    /// ```
    /// extern crate drone_api;
    /// use rusty_drone::Client;
    ///
    /// let drone_client = Client::new(
    ///     "randomsupersecrettoken".to_owned(),
    ///     "drone.example.org".to_owned()
    /// );
    /// ```
    /// note that there is no trasport in the url
    pub fn new(auth_token: String, base_url: String) -> Client {
        Client {
            auth_token: format!("Bearer {}",    auth_token),
            base_url:   format!("https://{}/api", base_url),
            client:     reqwest::Client::new(),
        }
    }

    fn get(&self, url: &String) -> String {
        self.client.get(url)
            .header("Authorization", self.auth_token.clone())
            .send().unwrap().text().unwrap()
    }

    /// Retrieves the user information corresponfing to the
    /// provided authorization token.
    pub fn get_current_user(&self) -> Result<UserInfo, serde_json::Error> {
        let url = format!("{}/user", self.base_url);
        let serial = self.get(&url);
        let user_info: UserInfo = serde_json::from_str(&serial)?;

        return Ok(user_info)
    }

    /// Retrieves the repos registered to the corresponding
    /// user.
    pub fn get_current_user_repos(&self) -> Result<Vec<Repo>, serde_json::Error> {
        let url = format!("{}/user/repos", self.base_url);
        let serial = self.get(&url);
        let user_repos: Vec<Repo> = serde_json::from_str(&serial)?;

        return Ok(user_repos)
    }

    pub fn get_current_user_feed(&self) -> Result<Vec<FeedElement>, serde_json::Error> {
        let url = format!("{}/user/feed", self.base_url);
        let serial = self.get(&url);
        let user_feed: Vec<FeedElement> = serde_json::from_str(&serial)?;

        return Ok(user_feed)
    }

    pub fn get_users(&self) -> Result<Vec<UserInfo>, serde_json::Error> {
        let url = format!("{}/users", self.base_url);
        let serial = self.get(&url);
        let users: Vec<UserInfo> = serde_json::from_str(&serial)?;

        return Ok(users)
    }

    pub fn get_repo_info(&self, owner: &String, repo: &String) -> Result<Repo, serde_json::Error> {
        let url = format!("{}/repos/{}/{}", self.base_url, owner, repo);
        let serial = self.get(&url);
        let repo: Repo = serde_json::from_str(&serial)?;

        return Ok(repo)
    }

    pub fn get_build_info(&self, owner: &String, repo: &String, build: &String) -> Result<Build, serde_json::Error> {
        let url = format!("{}/repos/{owner}/{repo}/builds/{build}", self.base_url,
            owner = owner, repo = repo, build = build);
        let serial = self.get(&url);
        let build: Build = serde_json::from_str(&serial)?;

        return Ok(build)
    }

    pub fn get_build_list(&self, owner: &String, repo: &String) -> Result<Vec<Build>, serde_json::Error> {
        let url = format!("{}/repos/{owner}/{repo}/builds", self.base_url,
            owner = owner, repo = repo);
        let serial = self.get(&url);
        let builds: Vec<Build> = serde_json::from_str(&serial)?;

        return Ok(builds)
    }

    pub fn get_registry_info(&self, owner: &String, repo: &String, registry: &String) -> Result<RegistryInfo, serde_json::Error> {
        let url = format!("{}/repos/{owner}/{repo}/registry/{registry}", self.base_url,
            owner = owner, repo = repo, registry = registry);
        let serial = self.get(&url);
        let registry: RegistryInfo = serde_json::from_str(&serial)?;

        return Ok(registry)
    }

    pub fn get_registry_list(&self, owner: &String, repo: &String) -> Result<Vec<RegistryInfo>, serde_json::Error> {
        let url = format!("{}/repos/{owner}/{repo}/registry", self.base_url,
            owner = owner, repo = repo);
        let serial = self.get(&url);
        let registries: Vec<RegistryInfo> = serde_json::from_str(&serial)?;

        return Ok(registries)
    }
}