Skip to main content

opencode_sdk/http/
files.rs

1//! Files API for OpenCode.
2//!
3//! Endpoints for file operations.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::types::file::{FileContent, FileInfo, FileStatus};
8use reqwest::Method;
9
10/// Files API client.
11#[derive(Clone)]
12pub struct FilesApi {
13    http: HttpClient,
14}
15
16impl FilesApi {
17    /// Create a new Files API client.
18    pub fn new(http: HttpClient) -> Self {
19        Self { http }
20    }
21
22    /// List files in the project.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the request fails.
27    pub async fn list(&self) -> Result<Vec<FileInfo>> {
28        self.http.request_json(Method::GET, "/file", None).await
29    }
30
31    /// Read file content.
32    ///
33    /// # Errors
34    ///
35    /// Returns an error if the request fails.
36    pub async fn read(&self, path: &str) -> Result<FileContent> {
37        let encoded = urlencoding::encode(path);
38        self.http
39            .request_json(
40                Method::GET,
41                &format!("/file/content?path={}", encoded),
42                None,
43            )
44            .await
45    }
46
47    /// Get file VCS status.
48    ///
49    /// # Errors
50    ///
51    /// Returns an error if the request fails.
52    pub async fn status(&self) -> Result<Vec<FileStatus>> {
53        self.http
54            .request_json(Method::GET, "/file/status", None)
55            .await
56    }
57}