Skip to main content

nubis_sdk/resources/
projects.rs

1use crate::client::NubisClient;
2use crate::error::Result;
3use crate::types::Project;
4
5/// Projects resource API
6pub struct Projects<'a> {
7    client: &'a NubisClient,
8}
9
10impl<'a> Projects<'a> {
11    pub(crate) fn new(client: &'a NubisClient) -> Self {
12        Self { client }
13    }
14
15    /// List all projects in an organization
16    pub async fn list(&self, org_id: &str) -> Result<Vec<Project>> {
17        let response: serde_json::Value = self
18            .client
19            .get(&format!("/api/v1/orgs/{}/projects", org_id))
20            .await?;
21
22        if let Some(projects) = response.get("projects").and_then(|v| v.as_array()) {
23            Ok(serde_json::from_value(serde_json::Value::Array(
24                projects.clone(),
25            ))?)
26        } else if let Some(projects) = response.as_array() {
27            Ok(serde_json::from_value(serde_json::Value::Array(
28                projects.clone(),
29            ))?)
30        } else {
31            Ok(vec![])
32        }
33    }
34
35    /// Get a specific project by ID
36    pub async fn get(&self, org_id: &str, project_id: &str) -> Result<Project> {
37        self.client
38            .get(&format!("/api/v1/orgs/{}/projects/{}", org_id, project_id))
39            .await
40    }
41
42    /// Create a new project
43    pub async fn create(&self, org_id: &str, name: &str, slug: &str) -> Result<Project> {
44        self.client
45            .post(
46                &format!("/api/v1/orgs/{}/projects", org_id),
47                &serde_json::json!({
48                    "name": name,
49                    "slug": slug,
50                }),
51            )
52            .await
53    }
54
55    /// Delete a project
56    pub async fn delete(&self, org_id: &str, project_id: &str) -> Result<()> {
57        self.client
58            .delete::<serde_json::Value>(&format!(
59                "/api/v1/orgs/{}/projects/{}",
60                org_id, project_id
61            ))
62            .await?;
63        Ok(())
64    }
65}