akribes_sdk/sub/
projects.rs1use std::sync::Arc;
2
3use crate::client::{AkribesClient, Inner};
4use crate::error::Result;
5use crate::models::*;
6
7#[derive(Clone, Debug)]
11pub struct ProjectsClient {
12 pub(crate) inner: Arc<Inner>,
13}
14
15impl ProjectsClient {
16 pub(crate) fn new(inner: Arc<Inner>) -> Self {
17 Self { inner }
18 }
19
20 fn c(&self) -> AkribesClient {
22 AkribesClient {
23 inner: Arc::clone(&self.inner),
24 }
25 }
26
27 pub async fn list(&self) -> Result<Vec<Project>> {
28 let url = format!("{}/projects", self.inner.base_url);
29 self.c().get_list(&url).await
30 }
31
32 pub async fn get(&self, project_id: i64) -> Result<Option<Project>> {
33 let url = format!("{}/projects/{}", self.inner.base_url, project_id);
34 self.c().get_opt(&url).await
35 }
36
37 pub async fn create(&self, name: &str) -> Result<Project> {
38 let url = format!("{}/projects", self.inner.base_url);
39 self.c().post(&url, &CreateProjectRequest { name }).await
40 }
41
42 pub async fn update(&self, project_id: i64, name: &str) -> Result<Project> {
43 let url = format!("{}/projects/{}", self.inner.base_url, project_id);
44 self.c().patch(&url, &UpdateProjectRequest { name }).await
45 }
46
47 pub async fn resolve(&self, id_or_name: &str) -> Result<Option<Project>> {
54 if let Ok(id) = id_or_name.parse::<i64>() {
55 return self.get(id).await;
56 }
57 let list = self.list().await?;
58 Ok(list.into_iter().find(|p| p.name == id_or_name))
59 }
60
61 pub async fn delete(&self, project_id: i64) -> Result<()> {
62 let url = format!("{}/projects/{}", self.inner.base_url, project_id);
63 self.c().delete(&url).await?;
64 Ok(())
65 }
66
67 pub async fn duplicate(&self, project_id: i64) -> Result<Project> {
70 let url = format!("{}/projects/{}/duplicate", self.inner.base_url, project_id);
71 self.c().post(&url, &serde_json::json!({})).await
72 }
73
74 pub async fn reorder(&self, order: Vec<i64>) -> Result<()> {
77 let url = format!("{}/projects/reorder", self.inner.base_url);
78 self.c().put_empty(&url, &ReorderRequest { order }).await
79 }
80
81 fn script_url(&self, project_id: i64, script_name: &str) -> String {
91 format!(
92 "{}/projects/{}/scripts/{}",
93 self.inner.base_url,
94 project_id,
95 urlencoding::encode(script_name)
96 )
97 }
98
99 pub async fn list_scripts(&self, project_id: i64) -> Result<Vec<Script>> {
102 let url = format!("{}/projects/{}/scripts", self.inner.base_url, project_id);
103 self.c().get_list(&url).await
104 }
105
106 pub async fn move_script(
109 &self,
110 src_project_id: i64,
111 src_script_name: &str,
112 dest_project_id: i64,
113 ) -> Result<Script> {
114 let url = format!("{}/move", self.script_url(src_project_id, src_script_name));
115 self.c()
116 .post(
117 &url,
118 &MoveScriptRequest {
119 target_project_id: dest_project_id,
120 },
121 )
122 .await
123 }
124
125 pub async fn rename_script(
128 &self,
129 project_id: i64,
130 current_name: &str,
131 new_name: &str,
132 ) -> Result<()> {
133 let url = self.script_url(project_id, current_name);
134 self.c()
135 .patch_empty(&url, &RenameScriptRequest { new_name })
136 .await
137 }
138
139 pub async fn delete_script(&self, project_id: i64, script_name: &str) -> Result<()> {
142 let url = self.script_url(project_id, script_name);
143 self.c().delete(&url).await?;
144 Ok(())
145 }
146
147 pub async fn duplicate_script(
152 &self,
153 project_id: i64,
154 script_name: &str,
155 _new_name: Option<&str>,
156 ) -> Result<Script> {
157 let url = format!("{}/duplicate", self.script_url(project_id, script_name));
158 self.c().post(&url, &serde_json::json!({})).await
159 }
160
161 pub async fn list_channels(
166 &self,
167 project_id: i64,
168 script_name: &str,
169 ) -> Result<Vec<ScriptChannel>> {
170 let url = format!("{}/channels", self.script_url(project_id, script_name));
171 self.c().get_list(&url).await
172 }
173
174 pub async fn reorder_scripts(&self, project_id: i64, order: Vec<i64>) -> Result<()> {
179 let url = format!(
180 "{}/projects/{}/scripts/reorder",
181 self.inner.base_url, project_id
182 );
183 self.c().put_empty(&url, &ReorderRequest { order }).await
184 }
185}