opencode_rs 0.4.0

Rust SDK for OpenCode (HTTP-first hybrid with SSE streaming)
Documentation
//! Project API for `OpenCode`.
//!
//! Endpoints for project management.

use crate::error::Result;
use crate::http::HttpClient;
use crate::http::encode_path_segment;
use crate::types::project::Project;
use crate::types::project::UpdateProjectRequest;
use reqwest::Method;

/// Project API client.
#[derive(Clone)]
pub struct ProjectApi {
    http: HttpClient,
}

impl ProjectApi {
    /// Create a new Project API client.
    pub fn new(http: HttpClient) -> Self {
        Self { http }
    }

    /// List projects.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails.
    pub async fn list(&self) -> Result<Vec<Project>> {
        self.http.request_json(Method::GET, "/project", None).await
    }

    /// Get current project.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails.
    pub async fn current(&self) -> Result<Project> {
        self.http
            .request_json(Method::GET, "/project/current", None)
            .await
    }

    /// Update a project.
    ///
    /// # Errors
    ///
    /// Returns an error if the request fails.
    pub async fn update(&self, project_id: &str, req: &UpdateProjectRequest) -> Result<Project> {
        let pid = encode_path_segment(project_id);
        let body = serde_json::to_value(req)?;
        self.http
            .request_json(Method::PATCH, &format!("/project/{pid}"), Some(body))
            .await
    }
}