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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Providers API endpoints.
use crate::client::LettaClient;
use crate::error::LettaResult;
use crate::pagination::PaginatedStream;
use crate::types::{
LettaId, ListProvidersParams, PaginationParams, Provider, ProviderCheckResponse,
ProviderCreate, ProviderDeleteResponse, ProviderUpdate,
};
/// Providers API operations.
#[derive(Debug)]
pub struct ProvidersApi<'a> {
client: &'a LettaClient,
}
impl<'a> ProvidersApi<'a> {
/// Create a new providers API instance.
pub fn new(client: &'a LettaClient) -> Self {
Self { client }
}
/// List all custom providers.
///
/// # Arguments
///
/// * `params` - Optional parameters for filtering and pagination
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn list(&self, params: Option<ListProvidersParams>) -> LettaResult<Vec<Provider>> {
let mut query_params = Vec::new();
if let Some(params) = params {
if let Some(provider_category) = params.provider_category {
query_params.push(("provider_category", provider_category.to_string()));
}
if let Some(provider_type) = params.provider_type {
query_params.push(("provider_type", provider_type.to_string()));
}
if let Some(after) = params.after {
query_params.push(("after", after));
}
if let Some(limit) = params.limit {
query_params.push(("limit", limit.to_string()));
}
}
if query_params.is_empty() {
self.client.get("v1/providers").await
} else {
self.client
.get_with_query("v1/providers", &query_params)
.await
}
}
/// Create a new custom provider.
///
/// # Arguments
///
/// * `provider` - The provider configuration to create
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn create(&self, provider: ProviderCreate) -> LettaResult<Provider> {
self.client.post("v1/providers", &provider).await
}
/// Delete a provider.
///
/// # Arguments
///
/// * `provider_id` - The ID of the provider to delete
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails.
pub async fn delete(&self, provider_id: &LettaId) -> LettaResult<ProviderDeleteResponse> {
self.client
.delete(&format!("v1/providers/{}", provider_id))
.await
}
/// Update a provider.
///
/// # Arguments
///
/// * `provider_id` - The ID of the provider to update
/// * `update` - The fields to update
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn update(
&self,
provider_id: &LettaId,
update: ProviderUpdate,
) -> LettaResult<Provider> {
self.client
.patch(&format!("v1/providers/{}", provider_id), &update)
.await
}
/// Check provider status.
///
/// This endpoint verifies that a provider is reachable and configured correctly.
///
/// # Arguments
///
/// * `provider_id` - The ID of the provider to check
///
/// # Errors
///
/// Returns a [crate::error::LettaError] if the request fails or if the response cannot be parsed.
pub async fn check(&self, provider_id: &LettaId) -> LettaResult<ProviderCheckResponse> {
self.client
.get(&format!("v1/providers/{}/check", provider_id))
.await
}
/// Get a paginated stream of providers.
///
/// This method returns a [`PaginatedStream`] that automatically handles pagination
/// and allows streaming through all providers using async iteration.
///
/// # Arguments
///
/// * `params` - Optional pagination parameters
///
/// # Example
///
/// ```no_run
/// # use letta::client::{ClientConfig, LettaClient};
/// # use letta::{types::PaginationParams, pagination::PaginationExt};
/// # use futures::StreamExt;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = LettaClient::new(ClientConfig::new("http://localhost:8283")?)?;
///
/// let mut stream = client.providers().paginated(None);
/// while let Some(provider) = stream.next().await {
/// let provider = provider?;
/// println!("Provider: {} ({})", provider.name, provider.provider_type);
/// }
/// # Ok(())
/// # }
/// ```
pub fn paginated(&self, params: Option<PaginationParams>) -> PaginatedStream<Provider> {
let client = self.client.clone();
let fetch_fn = move |params: Option<PaginationParams>| {
let client = client.clone();
async move {
let mut query_params = Vec::new();
if let Some(params) = params {
if let Some(after) = params.after {
query_params.push(("after", after));
}
if let Some(limit) = params.limit {
query_params.push(("limit", limit.to_string()));
}
}
if query_params.is_empty() {
client.get("v1/providers").await
} else {
client.get_with_query("v1/providers", &query_params).await
}
}
};
PaginatedStream::new_with_string_cursor(params, fetch_fn, |provider: &Provider| {
provider.id.to_string()
})
}
}
/// Convenience method for providers operations.
impl LettaClient {
/// Get the providers API for this client.
pub fn providers(&self) -> ProvidersApi {
ProvidersApi::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::ClientConfig;
#[test]
fn test_providers_api_creation() {
let config = ClientConfig::new("http://localhost:8283").unwrap();
let client = LettaClient::new(config).unwrap();
let _api = ProvidersApi::new(&client);
}
}