bodhi 2.2.0

bodhi REST API client
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::fmt::{Debug, Formatter};

use serde::{Deserialize, Serialize};

use crate::client::DEFAULT_ROWS;
use crate::data::*;
use crate::error::QueryError;
use crate::request::{PaginatedRequest, Pagination, RequestMethod, SingleRequest};

/// data type encapsulating parameters for querying for a [`Update`] by alias
///
/// If no comment with the specified ID is known to bodhi, a [`QueryError::NotFound`] error is
/// returned for the query.
///
/// ```
/// use bodhi::UpdateIDQuery;
///
/// let query = UpdateIDQuery::new("FEDORA-2019-3dd0cf468e");
/// // let update = bodhi.request(&query).unwrap();
/// ```
///
/// API documentation: <https://bodhi.fedoraproject.org/docs/server_api/rest/updates.html#service-0>
#[derive(Debug)]
pub struct UpdateIDQuery<'a> {
    id: &'a str,
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct UpdatePage {
    update: Update,
    can_edit: bool,
}

impl<'a> UpdateIDQuery<'a> {
    /// constructor for [`UpdateIDQuery`] from a comment ID
    pub fn new(id: &'a str) -> Self {
        UpdateIDQuery { id }
    }
}

impl<'a> SingleRequest<UpdatePage, Update> for UpdateIDQuery<'a> {
    fn method(&self) -> RequestMethod {
        RequestMethod::GET
    }

    fn path(&self) -> Result<String, QueryError> {
        Ok(format!("/updates/{}", self.id))
    }

    fn parse(&self, string: &str) -> Result<UpdatePage, QueryError> {
        let page: UpdatePage = serde_json::from_str(string)?;
        Ok(page)
    }

    fn extract(&self, page: UpdatePage) -> Update {
        page.update
    }
}


/// data type encapsulating parameters for querying [`Update`]s
///
/// ```
/// use bodhi::{ContentType, FedoraRelease, UpdateQuery, UpdateRequest};
///
/// let query = UpdateQuery::new()
///     .users(&["decathorpe"])
///     .request(UpdateRequest::Testing);
/// // let updates = bodhi.paginated_request(&query).unwrap();
/// ```
///
/// API documentation: <https://bodhi.fedoraproject.org/docs/server_api/rest/updates.html#service-2>
#[derive(Default)]
pub struct UpdateQuery<'a> {
    active_releases: Option<bool>,
    aliases: Option<&'a [&'a str]>,
    approved_before: Option<&'a BodhiDate>,
    approved_since: Option<&'a BodhiDate>,
    bugs: Option<&'a [u32]>,
    builds: Option<&'a [&'a str]>,
    content_type: Option<ContentType>,
    critpath: Option<bool>,
    cves: Option<&'a [&'a str]>,
    like: Option<&'a str>,
    locked: Option<bool>,
    modified_before: Option<&'a BodhiDate>,
    modified_since: Option<&'a BodhiDate>,
    packages: Option<&'a [&'a str]>,
    pushed: Option<bool>,
    pushed_before: Option<&'a BodhiDate>,
    pushed_since: Option<&'a BodhiDate>,
    releases: Option<&'a [FedoraRelease]>,
    request: Option<UpdateRequest>,
    search: Option<&'a str>,
    severity: Option<UpdateSeverity>,
    status: Option<UpdateStatus>,
    submitted_before: Option<&'a BodhiDate>,
    submitted_since: Option<&'a BodhiDate>,
    suggest: Option<UpdateSuggestion>,
    update_ids: Option<&'a [&'a str]>,
    update_type: Option<UpdateType>,
    users: Option<&'a [&'a str]>,

    // number of results per page
    rows_per_page: u32,
    // optional callback function for reporting progress
    callback: Option<Box<dyn Fn(u32, u32) + 'a>>,
}

impl<'a> Debug for UpdateQuery<'a> {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        f.debug_struct("UpdateQuery")
            .field("active_releases", &self.active_releases)
            .field("aliases", &self.aliases)
            .field("approved_before", &self.approved_before)
            .field("approved_since", &self.approved_since)
            .field("bugs", &self.bugs)
            .field("builds", &self.builds)
            .field("content_type", &self.content_type)
            .field("critpath", &self.critpath)
            .field("cves", &self.cves)
            .field("like", &self.like)
            .field("locked", &self.locked)
            .field("modified_before", &self.modified_before)
            .field("modified_since", &self.modified_since)
            .field("packages", &self.packages)
            .field("pushed", &self.pushed)
            .field("pushed_before", &self.pushed_before)
            .field("pushed_since", &self.pushed_since)
            .field("releases", &self.releases)
            .field("request", &self.request)
            .field("search", &self.search)
            .field("severity", &self.severity)
            .field("status", &self.status)
            .field("submitted_before", &self.submitted_before)
            .field("submitted_since", &self.submitted_since)
            .field("suggest", &self.suggest)
            .field("update_ids", &self.update_ids)
            .field("update_type", &self.update_type)
            .field("users", &self.users)
            .field("rows_per_page", &self.rows_per_page)
            .field("callback", &"(function pointer)")
            .finish()
    }
}

impl<'a> UpdateQuery<'a> {
    /// constructor for [`UpdateQuery`] without any filters
    pub fn new() -> Self {
        UpdateQuery {
            rows_per_page: DEFAULT_ROWS,
            ..Default::default()
        }
    }

    /// override the default number of results per page
    #[must_use]
    pub fn rows_per_page(mut self, rows_per_page: u32) -> Self {
        self.rows_per_page = rows_per_page;
        self
    }

    /// add callback function for progress reporting during long-running queries
    ///
    /// The specified function will be called with the current result page and the number of total
    /// pages as arguments.
    #[must_use]
    pub fn callback(mut self, fun: impl Fn(u32, u32) + 'a) -> Self {
        self.callback = Some(Box::new(fun));
        self
    }

    /// restrict query to updates from (in)active releases
    #[must_use]
    pub fn active_releases(mut self, active_releases: bool) -> Self {
        self.active_releases = Some(active_releases);
        self
    }

    /// restrict query to updates matching the specified aliases
    #[must_use]
    pub fn aliases(mut self, aliases: &'a [&'a str]) -> Self {
        self.aliases = Some(aliases);
        self
    }

    /// restrict query to updates that have been approved before the specified date & time
    #[deprecated(
        since = "2.0.0",
        note = "`date_approved` is an unused field: <https://github.com/fedora-infra/bodhi/issues/4171>"
    )]
    #[must_use]
    pub fn approved_before(mut self, approved_before: &'a BodhiDate) -> Self {
        self.approved_before = Some(approved_before);
        self
    }

    /// restrict query to updates that have been approved since the specified date & time
    #[deprecated(
        since = "2.0.0",
        note = "`date_approved` is an unused field: <https://github.com/fedora-infra/bodhi/issues/4171>"
    )]
    #[must_use]
    pub fn approved_since(mut self, approved_since: &'a BodhiDate) -> Self {
        self.approved_since = Some(approved_since);
        self
    }

    /// restrict query to updates that are associated with any of the specified bugs
    #[must_use]
    pub fn bugs(mut self, bugs: &'a [u32]) -> Self {
        self.bugs = Some(bugs);
        self
    }

    /// restrict query to updates that are associated with any of the specified builds
    #[must_use]
    pub fn builds(mut self, builds: &'a [&'a str]) -> Self {
        self.builds = Some(builds);
        self
    }

    /// restrict query to updates of the given content type
    #[must_use]
    pub fn content_type(mut self, content_type: ContentType) -> Self {
        self.content_type = Some(content_type);
        self
    }

    /// restrict query to updates that do (not) contain packages in the "critical path"
    #[must_use]
    pub fn critpath(mut self, critpath: bool) -> Self {
        self.critpath = Some(critpath);
        self
    }

    /// restrict query to updates that are associated with any of the specified CVEs
    #[must_use]
    pub fn cves(mut self, cves: &'a [&'a str]) -> Self {
        self.cves = Some(cves);
        self
    }

    /// restrict query to updates where the text is "like" the given string (in the SQL sense)
    #[must_use]
    pub fn like(mut self, like: &'a str) -> Self {
        self.like = Some(like);
        self
    }

    /// restrict query to updates that are (not) locked
    #[must_use]
    pub fn locked(mut self, locked: bool) -> Self {
        self.locked = Some(locked);
        self
    }

    /// restrict query to updates that have been modified before the specified date & time
    #[must_use]
    pub fn modified_before(mut self, modified_before: &'a BodhiDate) -> Self {
        self.modified_before = Some(modified_before);
        self
    }

    /// restrict query to updates that have been modified since the specified date & time
    #[must_use]
    pub fn modified_since(mut self, modified_since: &'a BodhiDate) -> Self {
        self.modified_since = Some(modified_since);
        self
    }

    /// restrict query to updates that contain any of the specified packages
    #[must_use]
    pub fn packages(mut self, packages: &'a [&'a str]) -> Self {
        self.packages = Some(packages);
        self
    }

    /// restrict query to updates that have (not) been pushed
    #[must_use]
    pub fn pushed(mut self, pushed: bool) -> Self {
        self.pushed = Some(pushed);
        self
    }

    /// restrict query to updates that have been pushed before the specified date & time
    #[must_use]
    pub fn pushed_before(mut self, pushed_before: &'a BodhiDate) -> Self {
        self.pushed_before = Some(pushed_before);
        self
    }

    /// restrict query to updates that have been pushed since the specified date & time
    #[must_use]
    pub fn pushed_since(mut self, pushed_since: &'a BodhiDate) -> Self {
        self.pushed_since = Some(pushed_since);
        self
    }

    /// restrict query to updates for any of the specified releases
    #[must_use]
    pub fn releases(mut self, releases: &'a [FedoraRelease]) -> Self {
        self.releases = Some(releases);
        self
    }

    /// restrict query to updates that have been requested for another state
    #[must_use]
    pub fn request(mut self, request: UpdateRequest) -> Self {
        self.request = Some(request);
        self
    }

    /// restrict query to updates matching a search keyword
    #[must_use]
    pub fn search(mut self, search: &'a str) -> Self {
        self.search = Some(search);
        self
    }

    /// restrict query to updates with the specified severity
    #[must_use]
    pub fn severity(mut self, severity: UpdateSeverity) -> Self {
        self.severity = Some(severity);
        self
    }

    /// restrict query to updates with the specified status
    #[must_use]
    pub fn status(mut self, status: UpdateStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// restrict query to updates that have been submitted before the specified date & time
    #[must_use]
    pub fn submitted_before(mut self, submitted_before: &'a BodhiDate) -> Self {
        self.submitted_before = Some(submitted_before);
        self
    }

    /// restrict query to updates that have been submitted since the specified date & time
    #[must_use]
    pub fn submitted_since(mut self, submitted_since: &'a BodhiDate) -> Self {
        self.submitted_since = Some(submitted_since);
        self
    }

    /// restrict query to updates with the specified suggested action
    #[must_use]
    pub fn suggest(mut self, suggest: UpdateSuggestion) -> Self {
        self.suggest = Some(suggest);
        self
    }

    /// restrict query to updates matching any of the specified update IDs
    #[must_use]
    pub fn update_ids(mut self, update_ids: &'a [&'a str]) -> Self {
        self.update_ids = Some(update_ids);
        self
    }

    /// restrict query to updates with the specified update type
    #[must_use]
    pub fn update_type(mut self, update_type: UpdateType) -> Self {
        self.update_type = Some(update_type);
        self
    }

    /// restrict query to updates that have been submitted by any of the specified users
    #[must_use]
    pub fn users(mut self, users: &'a [&'a str]) -> Self {
        self.users = Some(users);
        self
    }
}


/// data type encapsulating parameters for querying specific [`UpdateQuery`] result pages
#[derive(Debug, Serialize)]
pub struct UpdatePageQuery<'a> {
    active_releases: Option<bool>,
    #[serde(rename = "alias")]
    aliases: Option<&'a [&'a str]>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    approved_before: Option<&'a BodhiDate>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    approved_since: Option<&'a BodhiDate>,
    bugs: Option<&'a [u32]>,
    builds: Option<&'a [&'a str]>,
    content_type: Option<ContentType>,
    critpath: Option<bool>,
    cves: Option<&'a [&'a str]>,
    like: Option<&'a str>,
    locked: Option<bool>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    modified_before: Option<&'a BodhiDate>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    modified_since: Option<&'a BodhiDate>,
    packages: Option<&'a [&'a str]>,
    pushed: Option<bool>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    pushed_before: Option<&'a BodhiDate>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    pushed_since: Option<&'a BodhiDate>,
    releases: Option<&'a [FedoraRelease]>,
    request: Option<UpdateRequest>,
    search: Option<&'a str>,
    severity: Option<UpdateSeverity>,
    status: Option<UpdateStatus>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    submitted_before: Option<&'a BodhiDate>,
    #[serde(with = "crate::option_bodhi_date_format_ref")]
    submitted_since: Option<&'a BodhiDate>,
    suggest: Option<UpdateSuggestion>,
    #[serde(rename = "updateid")]
    update_ids: Option<&'a [&'a str]>,
    #[serde(rename = "type")]
    update_type: Option<UpdateType>,
    #[serde(rename = "user")]
    users: Option<&'a [&'a str]>,

    page: u32,
    rows_per_page: u32,
}

impl<'a> UpdatePageQuery<'a> {
    /// constructor for [`UpdatePageQuery`] taking parameters from an existing [`UpdateQuery`]
    pub fn from_query(query: &'a UpdateQuery, page: u32) -> Self {
        UpdatePageQuery {
            active_releases: query.active_releases,
            aliases: query.aliases,
            approved_before: query.approved_before,
            approved_since: query.approved_since,
            bugs: query.bugs,
            builds: query.builds,
            content_type: query.content_type,
            critpath: query.critpath,
            cves: query.cves,
            like: query.like,
            locked: query.locked,
            modified_before: query.modified_before,
            modified_since: query.modified_since,
            packages: query.packages,
            pushed: query.pushed,
            pushed_before: query.pushed_before,
            pushed_since: query.pushed_since,
            releases: query.releases,
            request: query.request,
            search: query.search,
            severity: query.severity,
            status: query.status,
            submitted_before: query.submitted_before,
            submitted_since: query.submitted_since,
            suggest: query.suggest,
            update_ids: query.update_ids,
            update_type: query.update_type,
            users: query.users,
            page,
            rows_per_page: query.rows_per_page,
        }
    }
}

impl<'a> SingleRequest<UpdateListPage, Vec<Update>> for UpdatePageQuery<'a> {
    fn method(&self) -> RequestMethod {
        RequestMethod::GET
    }

    fn path(&self) -> Result<String, QueryError> {
        Ok(format!("/updates/?{}", serde_url_params::to_string(self)?))
    }

    fn parse(&self, string: &str) -> Result<UpdateListPage, QueryError> {
        let page: UpdateListPage = serde_json::from_str(string)?;
        Ok(page)
    }

    fn extract(&self, page: UpdateListPage) -> Vec<Update> {
        page.updates
    }
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct UpdateListPage {
    updates: Vec<Update>,
    page: u32,
    pages: u32,
    rows_per_page: u32,
    total: u32,
}

impl Pagination for UpdateListPage {
    fn pages(&self) -> u32 {
        self.pages
    }
}

impl<'a> PaginatedRequest<UpdateListPage, Vec<Update>> for UpdateQuery<'a> {
    fn page_request<'b>(&'b self, page: u32) -> Box<dyn SingleRequest<UpdateListPage, Vec<Update>> + 'b> {
        Box::new(UpdatePageQuery::from_query(self, page))
    }

    fn callback(&self, page: u32, pages: u32) {
        if let Some(ref callback) = &self.callback {
            callback(page, pages)
        }
    }
}