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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::{aur, config, endpoints, jobs, request, request_error, responses};

use request::{Request, RequestResult};
use std::collections::HashMap;

pub struct LibRb {
    config: config::RequestConfig,
}

pub fn new(config: config::RequestConfig) -> LibRb {
    LibRb { config }
}

impl LibRb {
    /// Return a new AURBuild which allows you to create build AUR jobs
    pub fn new_aurbuild<S: AsRef<str>>(&self, pkg_name: S) -> aur::AURBuild {
        let mut hm: HashMap<String, String> = HashMap::new();

        hm.insert(aur::AUR_PACKAGE.to_owned(), pkg_name.as_ref().to_owned());

        aur::AURBuild {
            librb: self,
            args: hm,
            upload_type: jobs::UploadType::NoUploadType,
            disable_ccache: false,
        }
    }

    /// Return Authorization created from `self`s config
    pub fn auth_from_conf(&self) -> request::Authorization {
        request::Authorization::new(
            request::AuthorizationType::Bearer,
            self.config.token.to_owned(),
        )
    }

    /// List all running and past jobs. `limit` indicates the limit how
    /// much to display
    pub async fn list_jobs(
        &self,
        limit: i32,
    ) -> Result<RequestResult<responses::ListJobs>, request_error::Error> {
        let mut request = Request::new(
            self.config.clone(),
            endpoints::JOBS,
            request::ListJobs { limit },
        );

        request.with_auth(self.auth_from_conf());
        Ok(request.do_request().await?)
    }

    /// Cancel a running job
    pub async fn cancel_job(&self, job_id: u32) -> Result<(), request_error::Error> {
        let mut request = Request::new(
            self.config.clone(),
            endpoints::JOBCANCEL,
            request::JobRequest { job_id },
        );

        request.with_auth(self.auth_from_conf());
        request.with_method(reqwest::Method::POST);
        request.do_request_void().await?;

        Ok(())
    }

    /// Gets information about a job
    pub async fn job_info(
        &self,
        job_id: u32,
    ) -> Result<RequestResult<jobs::Info>, request_error::Error> {
        let mut request = Request::new(
            self.config.clone(),
            endpoints::JOBINFO,
            request::JobRequest { job_id },
        );

        request.with_auth(self.auth_from_conf());
        request.with_method(reqwest::Method::GET);
        Ok(request.do_request().await?)
    }

    /// Creates and adds a new job
    pub async fn add_job(
        &self,
        job_type: jobs::Type,
        upload_type: jobs::UploadType,
        args: HashMap<String, String>,
        disable_ccache: bool,
    ) -> Result<RequestResult<responses::AddJob>, request_error::Error> {
        let mut request = Request::new(
            self.config.clone(),
            endpoints::JOBADD,
            request::AddJobRequest {
                args,
                upload_type,
                disable_ccache,
                job_type,
            },
        );

        request.with_auth(self.auth_from_conf());
        request.with_method(reqwest::Method::PUT);
        Ok(request.do_request().await?)
    }

    /// Set a jobs state either to `paused` or `running`
    /// This allows to pause/continue a task
    pub async fn set_job_state(
        &self,
        job_id: u32,
        state: jobs::Status,
    ) -> Result<(), request_error::Error> {
        let ep = match state {
            jobs::Status::Paused => endpoints::JOBPAUSE,
            jobs::Status::Running => endpoints::JOBRESUME,
            _ => return Err(request_error::Error::InvalidState),
        };

        let mut request = Request::new(self.config.clone(), ep, request::JobRequest { job_id });

        request.with_auth(self.auth_from_conf());
        request.with_method(reqwest::Method::PUT);
        request.do_request_void().await?;

        Ok(())
    }

    /// Login into an existing account. Returns the token on success
    pub async fn login(
        &self,
        username: String,
        password: String,
    ) -> Result<RequestResult<responses::Login>, request_error::Error> {
        let mut request = Request::new(
            self.config.clone(),
            endpoints::LOGIN,
            request::Credential {
                machine_id: self.config.machine_id.clone(),
                username,
                password,
            },
        );

        request.with_method(reqwest::Method::POST);
        Ok(request.do_request().await?)
    }
}