1use std::time::Duration;
21
22use serde_json::json;
23
24use crate::client::{GitFoxClient, Method, Query};
25use crate::error::Result;
26use crate::models::{CreateGitspace, Gitspace, LogLine, RepoRef, Rule};
27use crate::pipeline::urlencode;
28
29pub struct RulesApi<'a> {
30 client: &'a GitFoxClient,
31}
32
33impl<'a> RulesApi<'a> {
34 pub(crate) fn new(client: &'a GitFoxClient) -> Self {
35 Self { client }
36 }
37
38 pub async fn list(&self, repo: &RepoRef, page: u32, limit: u32) -> Result<Vec<Rule>> {
40 let mut q = Query::new();
41 q.push("page", page).push("limit", limit);
42 self.client
43 .get_json(&q.apply(&format!("/api/v1/repos/{}/rules", repo.encoded())))
44 .await
45 }
46
47 pub async fn get(&self, repo: &RepoRef, identifier: &str) -> Result<Rule> {
49 self.client
50 .get_json(&format!(
51 "/api/v1/repos/{}/rules/{}",
52 repo.encoded(),
53 urlencode(identifier)
54 ))
55 .await
56 }
57}
58
59pub struct GitspacesApi<'a> {
60 client: &'a GitFoxClient,
61}
62
63impl<'a> GitspacesApi<'a> {
64 pub(crate) fn new(client: &'a GitFoxClient) -> Self {
65 Self { client }
66 }
67
68 fn path(identifier: &str) -> String {
69 format!("/api/v1/gitspaces/{}", urlencode(identifier))
70 }
71
72 pub async fn list(&self, page: u32, limit: u32) -> Result<Vec<Gitspace>> {
74 let mut q = Query::new();
75 q.push("sort", "updated")
76 .push("order", "desc")
77 .push("page", page)
78 .push("limit", limit);
79 self.client.get_json(&q.apply("/api/v1/gitspaces")).await
80 }
81
82 pub async fn get(&self, identifier: &str) -> Result<Gitspace> {
84 self.client.get_json(&Self::path(identifier)).await
85 }
86
87 pub async fn create(&self, input: &CreateGitspace) -> Result<Gitspace> {
89 let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
90 self.client
91 .request(Method::POST, "/api/v1/gitspaces", Some(&body), &[])
92 .await?
93 .deserialize()
94 }
95
96 pub async fn delete(&self, identifier: &str) -> Result<()> {
98 self.client
99 .request(Method::DELETE, &Self::path(identifier), None, &[])
100 .await
101 .map(|_| ())
102 }
103
104 pub async fn action(&self, identifier: &str, action: &str) -> Result<Gitspace> {
106 self.client
107 .request(
108 Method::POST,
109 &format!("{}/action", Self::path(identifier)),
110 Some(&json!({ "action": action })),
111 &[],
112 )
113 .await?
114 .deserialize()
115 }
116
117 pub async fn logs(&self, identifier: &str, idle: Duration) -> Result<Vec<LogLine>> {
119 self.client
120 .sse_lines(&format!("{}/logs/stream", Self::path(identifier)), idle)
121 .await
122 }
123}