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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Provider Account methods for [`IronflowClient`]. Admin only.
//!
//! No response carries the account credential.
use ironflow_types::ApiResponse;
use serde_json::json;
use crate::client::IronflowClient;
use crate::error::Error;
use crate::types;
/// Percent-encode one path segment (an account UUID or name).
fn segment(raw: &str) -> String {
raw.bytes()
.map(|b| {
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
(b as char).to_string()
} else {
format!("%{b:02X}")
}
})
.collect()
}
impl IronflowClient {
/// List Provider Accounts with their latest usage windows.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the response cannot be deserialized.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let accounts = client.list_provider_accounts().await?;
/// # Ok(())
/// # }
/// ```
pub async fn list_provider_accounts(
&self,
) -> Result<ApiResponse<Vec<types::ProviderAccountResponse>>, Error> {
self.send_envelope(self.get("/api/v1/provider-accounts?per_page=100"))
.await
}
/// Add a Provider Account. The token is checked against the provider first.
///
/// # Errors
///
/// Returns an error if the HTTP request fails, the token is rejected (422)
/// or the name is taken (409).
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
/// use ironflow_sdk::types::CreateProviderAccountRequest;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let account = client.create_provider_account(&CreateProviderAccountRequest {
/// name: "perso".to_string(),
/// kind: "claude_subscription".to_string(),
/// token: "sk-ant-oat01-...".to_string(),
/// alert_threshold: None,
/// display_name: None,
/// enabled: None,
/// expires_at: None,
/// max_concurrency: None,
/// plan: None,
/// priority: None,
/// tags: None,
/// }).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_provider_account(
&self,
request: &types::CreateProviderAccountRequest,
) -> Result<ApiResponse<types::ProviderAccountResponse>, Error> {
self.send_envelope(self.post("/api/v1/provider-accounts").json(request))
.await
}
/// Get a Provider Account by UUID or name.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the account does not exist.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let account = client.get_provider_account("perso").await?;
/// # Ok(())
/// # }
/// ```
pub async fn get_provider_account(
&self,
account: &str,
) -> Result<ApiResponse<types::ProviderAccountResponse>, Error> {
self.send_envelope(self.get(&format!("/api/v1/provider-accounts/{}", segment(account))))
.await
}
/// Update a Provider Account by UUID or name.
///
/// # Errors
///
/// Returns an error if the HTTP request fails, the account does not exist
/// or a new token is rejected.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
/// use ironflow_sdk::types::UpdateProviderAccountRequest;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let update = UpdateProviderAccountRequest { enabled: Some(false), ..Default::default() };
/// client.update_provider_account("perso", &update).await?;
/// # Ok(())
/// # }
/// ```
pub async fn update_provider_account(
&self,
account: &str,
request: &types::UpdateProviderAccountRequest,
) -> Result<ApiResponse<types::ProviderAccountResponse>, Error> {
self.send_envelope(
self.patch(&format!("/api/v1/provider-accounts/{}", segment(account)))
.json(request),
)
.await
}
/// Remove the `max_concurrency` limit of a Provider Account.
///
/// [`update_provider_account`](Self::update_provider_account) leaves an
/// absent field unchanged, so clearing needs an explicit `null`.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the account does not exist.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// client.clear_provider_account_max_concurrency("perso").await?;
/// # Ok(())
/// # }
/// ```
pub async fn clear_provider_account_max_concurrency(
&self,
account: &str,
) -> Result<ApiResponse<types::ProviderAccountResponse>, Error> {
self.send_envelope(
self.patch(&format!("/api/v1/provider-accounts/{}", segment(account)))
.json(&json!({ "max_concurrency": null })),
)
.await
}
/// Delete a Provider Account and its credential.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the account does not exist.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// client.delete_provider_account("perso").await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_provider_account(&self, account: &str) -> Result<(), Error> {
self.send_no_content(
self.delete(&format!("/api/v1/provider-accounts/{}", segment(account))),
)
.await
}
/// Check the stored credential of a Provider Account against the provider.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the provider is unreachable.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let outcome = client.test_provider_account("perso").await?;
/// # Ok(())
/// # }
/// ```
pub async fn test_provider_account(
&self,
account: &str,
) -> Result<ApiResponse<types::ProviderAccountTestResponse>, Error> {
self.send_envelope(self.post(&format!(
"/api/v1/provider-accounts/{}/test",
segment(account)
)))
.await
}
/// Current windows and 30-day history of a Provider Account.
///
/// # Errors
///
/// Returns an error if the HTTP request fails or the account does not exist.
///
/// # Examples
///
/// ```no_run
/// use ironflow_sdk::IronflowClient;
///
/// # async fn example() -> Result<(), ironflow_sdk::Error> {
/// let client = IronflowClient::new("https://ironflow.example.com", "key");
/// let usage = client.provider_account_usage("perso").await?;
/// # Ok(())
/// # }
/// ```
pub async fn provider_account_usage(
&self,
account: &str,
) -> Result<ApiResponse<types::ProviderAccountUsageResponse>, Error> {
self.send_envelope(self.get(&format!(
"/api/v1/provider-accounts/{}/usage",
segment(account)
)))
.await
}
}
#[cfg(test)]
mod tests {
use super::segment;
#[test]
fn segment_keeps_slugs_and_encodes_the_rest() {
assert_eq!(segment("perso-max"), "perso-max");
assert_eq!(
segment("0192f0c1-0000-7000-8000-000000000000"),
"0192f0c1-0000-7000-8000-000000000000"
);
assert_eq!(segment("a/b c"), "a%2Fb%20c");
assert_eq!(segment("../x"), "..%2Fx");
}
}