Skip to main content

opencode_sdk/http/
providers.rs

1//! Providers API for OpenCode.
2//!
3//! Endpoints for managing LLM providers.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::types::api::{OAuthCallbackResponse, SetAuthResponse};
8use crate::types::provider::{
9    OAuthAuthorizeResponse, OAuthCallbackRequest, ProviderAuth, ProviderListResponse,
10    SetAuthRequest,
11};
12use reqwest::Method;
13
14/// Providers API client.
15#[derive(Clone)]
16pub struct ProvidersApi {
17    http: HttpClient,
18}
19
20impl ProvidersApi {
21    /// Create a new Providers API client.
22    pub fn new(http: HttpClient) -> Self {
23        Self { http }
24    }
25
26    /// List available providers.
27    ///
28    /// Returns a response containing all providers, their default models,
29    /// and which providers are connected/authenticated.
30    ///
31    /// # Errors
32    ///
33    /// Returns an error if the request fails.
34    pub async fn list(&self) -> Result<ProviderListResponse> {
35        self.http.request_json(Method::GET, "/provider", None).await
36    }
37
38    /// Get provider authentication info.
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the request fails.
43    pub async fn auth(&self) -> Result<Vec<ProviderAuth>> {
44        self.http
45            .request_json(Method::GET, "/provider/auth", None)
46            .await
47    }
48
49    /// Start OAuth authorization flow.
50    ///
51    /// # Errors
52    ///
53    /// Returns an error if the request fails.
54    pub async fn oauth_authorize(&self, provider_id: &str) -> Result<OAuthAuthorizeResponse> {
55        self.http
56            .request_json(
57                Method::POST,
58                &format!("/provider/{}/oauth/authorize", provider_id),
59                Some(serde_json::json!({})),
60            )
61            .await
62    }
63
64    /// Complete OAuth callback.
65    ///
66    /// # Errors
67    ///
68    /// Returns an error if the request fails.
69    pub async fn oauth_callback(
70        &self,
71        provider_id: &str,
72        req: &OAuthCallbackRequest,
73    ) -> Result<OAuthCallbackResponse> {
74        let body = serde_json::to_value(req)?;
75        self.http
76            .request_json(
77                Method::POST,
78                &format!("/provider/{}/oauth/callback", provider_id),
79                Some(body),
80            )
81            .await
82    }
83
84    /// Set authentication for a provider.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if the request fails.
89    pub async fn set_auth(
90        &self,
91        provider_id: &str,
92        req: &SetAuthRequest,
93    ) -> Result<SetAuthResponse> {
94        let body = serde_json::to_value(req)?;
95        self.http
96            .request_json(Method::PUT, &format!("/auth/{}", provider_id), Some(body))
97            .await
98    }
99}