Skip to main content

lib_remotebuild_rs/
librb.rs

1use crate::{aur, config, endpoints, jobs, request, request_error, responses};
2
3use request::{Request, RequestResult};
4use std::collections::HashMap;
5
6pub struct LibRb {
7    config: config::RequestConfig,
8}
9
10pub fn new(config: config::RequestConfig) -> LibRb {
11    LibRb { config }
12}
13
14impl LibRb {
15    /// Return a new AURBuild which allows you to create build AUR jobs
16    pub fn new_aurbuild<S: AsRef<str>>(&self, pkg_name: S) -> aur::AURBuild {
17        let mut hm: HashMap<String, String> = HashMap::new();
18
19        hm.insert(aur::AUR_PACKAGE.to_owned(), pkg_name.as_ref().to_owned());
20
21        aur::AURBuild {
22            librb: self,
23            args: hm,
24            upload_type: jobs::UploadType::NoUploadType,
25            disable_ccache: false,
26        }
27    }
28
29    /// Return Authorization created from `self`s config
30    pub fn auth_from_conf(&self) -> request::Authorization {
31        request::Authorization::new(
32            request::AuthorizationType::Bearer,
33            self.config.token.to_owned(),
34        )
35    }
36
37    /// List all running and past jobs. `limit` indicates the limit how
38    /// much to display
39    pub async fn list_jobs(
40        &self,
41        limit: i32,
42    ) -> Result<RequestResult<responses::ListJobs>, request_error::Error> {
43        let mut request = Request::new(
44            self.config.clone(),
45            endpoints::JOBS,
46            request::ListJobs { limit },
47        );
48
49        request.with_auth(self.auth_from_conf());
50        Ok(request.do_request().await?)
51    }
52
53    /// Cancel a running job
54    pub async fn cancel_job(&self, job_id: u32) -> Result<(), request_error::Error> {
55        let mut request = Request::new(
56            self.config.clone(),
57            endpoints::JOBCANCEL,
58            request::JobRequest { job_id },
59        );
60
61        request.with_auth(self.auth_from_conf());
62        request.with_method(reqwest::Method::POST);
63        request.do_request_void().await?;
64
65        Ok(())
66    }
67
68    /// Gets information about a job
69    pub async fn job_info(
70        &self,
71        job_id: u32,
72    ) -> Result<RequestResult<jobs::Info>, request_error::Error> {
73        let mut request = Request::new(
74            self.config.clone(),
75            endpoints::JOBINFO,
76            request::JobRequest { job_id },
77        );
78
79        request.with_auth(self.auth_from_conf());
80        request.with_method(reqwest::Method::GET);
81        Ok(request.do_request().await?)
82    }
83
84    /// Creates and adds a new job
85    pub async fn add_job(
86        &self,
87        job_type: jobs::Type,
88        upload_type: jobs::UploadType,
89        args: HashMap<String, String>,
90        disable_ccache: bool,
91    ) -> Result<RequestResult<responses::AddJob>, request_error::Error> {
92        let mut request = Request::new(
93            self.config.clone(),
94            endpoints::JOBADD,
95            request::AddJobRequest {
96                args,
97                upload_type,
98                disable_ccache,
99                job_type,
100            },
101        );
102
103        request.with_auth(self.auth_from_conf());
104        request.with_method(reqwest::Method::PUT);
105        Ok(request.do_request().await?)
106    }
107
108    /// Set a jobs state either to `paused` or `running`
109    /// This allows to pause/continue a task
110    pub async fn set_job_state(
111        &self,
112        job_id: u32,
113        state: jobs::Status,
114    ) -> Result<(), request_error::Error> {
115        let ep = match state {
116            jobs::Status::Paused => endpoints::JOBPAUSE,
117            jobs::Status::Running => endpoints::JOBRESUME,
118            _ => return Err(request_error::Error::InvalidState),
119        };
120
121        let mut request = Request::new(self.config.clone(), ep, request::JobRequest { job_id });
122
123        request.with_auth(self.auth_from_conf());
124        request.with_method(reqwest::Method::PUT);
125        request.do_request_void().await?;
126
127        Ok(())
128    }
129
130    /// Login into an existing account. Returns the token on success
131    pub async fn login(
132        &self,
133        username: String,
134        password: String,
135    ) -> Result<RequestResult<responses::Login>, request_error::Error> {
136        let mut request = Request::new(
137            self.config.clone(),
138            endpoints::LOGIN,
139            request::Credential {
140                machine_id: self.config.machine_id.clone(),
141                username,
142                password,
143            },
144        );
145
146        request.with_method(reqwest::Method::POST);
147        Ok(request.do_request().await?)
148    }
149}