Skip to main content

gitfox_client/
labels.rs

1//! Label endpoints.
2//!
3//! Verified against the GitFox API v1.3.0 OpenAPI document:
4//!
5//! | Operation | Endpoint |
6//! |---|---|
7//! | list    | `GET /api/v1/repos/{repo_ref}/labels` (`inherited` adds space labels) |
8//! | define  | `POST /api/v1/repos/{repo_ref}/labels` |
9//! | update  | `PATCH /api/v1/repos/{repo_ref}/labels/{key}` |
10//! | delete  | `DELETE /api/v1/repos/{repo_ref}/labels/{key}` |
11//! | values  | `GET /api/v1/repos/{repo_ref}/labels/{key}/values` |
12
13use crate::client::{GitFoxClient, Method, Query};
14use crate::error::Result;
15use crate::models::{Label, LabelInput, LabelValue, RepoRef};
16use crate::pipeline::urlencode;
17
18pub struct LabelsApi<'a> {
19    client: &'a GitFoxClient,
20}
21
22impl<'a> LabelsApi<'a> {
23    pub(crate) fn new(client: &'a GitFoxClient) -> Self {
24        Self { client }
25    }
26
27    fn path(repo: &RepoRef) -> String {
28        format!("/api/v1/repos/{}/labels", repo.encoded())
29    }
30
31    /// `GET …/labels`. With `inherited`, labels defined on the enclosing
32    /// spaces come too — the full set a pull request can be given.
33    pub async fn list(
34        &self,
35        repo: &RepoRef,
36        query: Option<&str>,
37        inherited: bool,
38        page: u32,
39        limit: u32,
40    ) -> Result<Vec<Label>> {
41        let mut q = Query::new();
42        q.push_opt("query", query)
43            .push("inherited", inherited)
44            .push("page", page)
45            .push("limit", limit);
46        self.client.get_json(&q.apply(&Self::path(repo))).await
47    }
48
49    /// `POST …/labels`
50    pub async fn define(&self, repo: &RepoRef, input: &LabelInput) -> Result<Label> {
51        let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
52        self.client
53            .request(Method::POST, &Self::path(repo), Some(&body), &[])
54            .await?
55            .deserialize()
56    }
57
58    /// `PATCH …/labels/{key}`
59    pub async fn update(&self, repo: &RepoRef, key: &str, input: &LabelInput) -> Result<Label> {
60        let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
61        self.client
62            .request(
63                Method::PATCH,
64                &format!("{}/{}", Self::path(repo), urlencode(key)),
65                Some(&body),
66                &[],
67            )
68            .await?
69            .deserialize()
70    }
71
72    /// `DELETE …/labels/{key}`
73    pub async fn delete(&self, repo: &RepoRef, key: &str) -> Result<()> {
74        self.client
75            .request(
76                Method::DELETE,
77                &format!("{}/{}", Self::path(repo), urlencode(key)),
78                None,
79                &[],
80            )
81            .await
82            .map(|_| ())
83    }
84
85    /// `GET …/labels/{key}/values`
86    pub async fn values(&self, repo: &RepoRef, key: &str) -> Result<Vec<LabelValue>> {
87        self.client
88            .get_json(&format!("{}/{}/values", Self::path(repo), urlencode(key)))
89            .await
90    }
91}