1use 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 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 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 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 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 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}