Skip to main content

opencode_sdk/http/
project.rs

1//! Project API for OpenCode.
2//!
3//! Endpoints for project management.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::types::project::{Project, UpdateProjectRequest};
8use reqwest::Method;
9
10/// Project API client.
11#[derive(Clone)]
12pub struct ProjectApi {
13    http: HttpClient,
14}
15
16impl ProjectApi {
17    /// Create a new Project API client.
18    pub fn new(http: HttpClient) -> Self {
19        Self { http }
20    }
21
22    /// List projects.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the request fails.
27    pub async fn list(&self) -> Result<Vec<Project>> {
28        self.http.request_json(Method::GET, "/project", None).await
29    }
30
31    /// Get current project.
32    ///
33    /// # Errors
34    ///
35    /// Returns an error if the request fails.
36    pub async fn current(&self) -> Result<Project> {
37        self.http
38            .request_json(Method::GET, "/project/current", None)
39            .await
40    }
41
42    /// Update a project.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if the request fails.
47    pub async fn update(&self, project_id: &str, req: &UpdateProjectRequest) -> Result<Project> {
48        let body = serde_json::to_value(req)?;
49        self.http
50            .request_json(
51                Method::PATCH,
52                &format!("/project/{}", project_id),
53                Some(body),
54            )
55            .await
56    }
57}