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