Skip to main content

opencode_sdk/http/
config.rs

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