use crate::client::{GitFoxClient, Method, Query};
use crate::error::Result;
use crate::models::{Label, LabelInput, LabelValue, RepoRef};
use crate::pipeline::urlencode;
pub struct LabelsApi<'a> {
client: &'a GitFoxClient,
}
impl<'a> LabelsApi<'a> {
pub(crate) fn new(client: &'a GitFoxClient) -> Self {
Self { client }
}
fn path(repo: &RepoRef) -> String {
format!("/api/v1/repos/{}/labels", repo.encoded())
}
pub async fn list(
&self,
repo: &RepoRef,
query: Option<&str>,
inherited: bool,
page: u32,
limit: u32,
) -> Result<Vec<Label>> {
let mut q = Query::new();
q.push_opt("query", query)
.push("inherited", inherited)
.push("page", page)
.push("limit", limit);
self.client.get_json(&q.apply(&Self::path(repo))).await
}
pub async fn define(&self, repo: &RepoRef, input: &LabelInput) -> Result<Label> {
let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
self.client
.request(Method::POST, &Self::path(repo), Some(&body), &[])
.await?
.deserialize()
}
pub async fn update(&self, repo: &RepoRef, key: &str, input: &LabelInput) -> Result<Label> {
let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
self.client
.request(
Method::PATCH,
&format!("{}/{}", Self::path(repo), urlencode(key)),
Some(&body),
&[],
)
.await?
.deserialize()
}
pub async fn delete(&self, repo: &RepoRef, key: &str) -> Result<()> {
self.client
.request(
Method::DELETE,
&format!("{}/{}", Self::path(repo), urlencode(key)),
None,
&[],
)
.await
.map(|_| ())
}
pub async fn values(&self, repo: &RepoRef, key: &str) -> Result<Vec<LabelValue>> {
self.client
.get_json(&format!("{}/{}/values", Self::path(repo), urlencode(key)))
.await
}
}