anthropic-api 0.0.5

An unofficial Rust library for the Anthropic API.
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! # API Keys Admin API
//!
//! This module provides a Rust interface to Anthropic's Admin API for managing API keys, which allows you to
//! list, get, and update API keys.
//!
//! ## Key Features
//!
//! - List all API keys with pagination and filtering support
//! - Get detailed information about a specific API key
//! - Update API key properties like name and status
//!
//! ## Basic Usage
//!
//! ```no_run
//! use anthropic_api::{admin::api_keys::*, Credentials};
//!
//! #[tokio::main]
//! async fn main() {
//!     let credentials = Credentials::from_env();
//!
//!     // List API keys
//!     let api_keys = ApiKeyList::builder()
//!         .credentials(credentials.clone())
//!         .create()
//!         .await
//!         .unwrap();
//!
//!     println!("Available API keys: {:?}", api_keys.data);
//!
//!     // Get a specific API key
//!     if let Some(api_key) = api_keys.data.first() {
//!         let api_key_details = ApiKey::builder(&api_key.id)
//!             .credentials(credentials.clone())
//!             .create()
//!             .await
//!             .unwrap();
//!
//!         println!("API key details: {:?}", api_key_details);
//!     }
//! }
//! ```

use crate::{anthropic_request_json, ApiResponseOrError, Credentials};
use derive_builder::Builder;
use reqwest::Method;
use serde::{Deserialize, Serialize};

/// Status of an API key
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ApiKeyStatus {
    /// API key is active and can be used
    Active,
    /// API key is inactive and cannot be used
    Inactive,
    /// API key is archived and cannot be used
    Archived,
}

/// Information about the creator of an API key
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct ApiKeyCreator {
    /// ID of the creator
    pub id: String,
    /// Type of the creator
    #[serde(rename = "type")]
    pub creator_type: String,
}

/// An API key available through the Anthropic Admin API.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct ApiKey {
    /// Unique API key identifier
    pub id: String,
    /// Name of the API key
    pub name: String,
    /// RFC 3339 datetime string representing the time at which the API key was created
    pub created_at: String,
    /// Information about who created the API key
    pub created_by: ApiKeyCreator,
    /// Partially redacted hint for the API key
    pub partial_key_hint: Option<String>,
    /// Status of the API key
    pub status: ApiKeyStatus,
    /// Object type (always "api_key" for API Keys)
    #[serde(rename = "type")]
    pub key_type: String,
    /// ID of the Workspace associated with the API key, or null if the API key belongs to the default Workspace
    pub workspace_id: Option<String>,
}

/// Response from the List API Keys API.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct ApiKeyList {
    /// List of available API keys
    pub data: Vec<ApiKey>,
    /// First ID in the data list (for pagination)
    pub first_id: Option<String>,
    /// Last ID in the data list (for pagination)
    pub last_id: Option<String>,
    /// Indicates if there are more results in the requested page direction
    pub has_more: bool,
}

/// Request parameters for listing API keys.
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "ApiKeyListBuilder")]
#[builder(setter(strip_option, into))]
pub struct ApiKeyListRequest {
    /// ID of the object to use as a cursor for pagination (previous page)
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before_id: Option<String>,

    /// ID of the object to use as a cursor for pagination (next page)
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after_id: Option<String>,

    /// Number of items to return per page (1-1000)
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,

    /// Filter by API key status
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ApiKeyStatus>,

    /// Filter by Workspace ID
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace_id: Option<String>,

    /// Filter by the ID of the User who created the object
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_by_user_id: Option<String>,

    /// Credentials for authentication (not serialized)
    #[serde(skip_serializing)]
    #[builder(default)]
    pub credentials: Option<Credentials>,
}

/// Request parameters for getting a specific API key.
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "ApiKeyBuilder")]
#[builder(setter(strip_option, into))]
pub struct ApiKeyRequest {
    /// API key identifier
    pub api_key_id: String,

    /// Credentials for authentication (not serialized)
    #[serde(skip_serializing)]
    #[builder(default)]
    pub credentials: Option<Credentials>,
}

/// Request parameters for updating an API key.
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "ApiKeyUpdateBuilder")]
#[builder(setter(strip_option, into))]
pub struct ApiKeyUpdateRequest {
    /// API key identifier (not serialized)
    #[serde(skip_serializing)]
    pub api_key_id: String,

    /// New name for the API key
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// New status for the API key
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ApiKeyStatus>,

    /// Credentials for authentication (not serialized)
    #[serde(skip_serializing)]
    #[builder(default)]
    pub credentials: Option<Credentials>,
}

impl ApiKeyList {
    /// Creates a builder for listing API keys.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let api_keys = ApiKeyList::builder()
    ///     .credentials(credentials)
    ///     .limit(10u32)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> ApiKeyListBuilder {
        ApiKeyListBuilder::create_empty()
    }

    /// Lists available API keys with the given request parameters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = ApiKeyListRequest {
    ///     before_id: None,
    ///     after_id: None,
    ///     limit: Some(20),
    ///     status: None,
    ///     workspace_id: None,
    ///     created_by_user_id: None,
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let api_keys = ApiKeyList::create(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(request: ApiKeyListRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();

        // Build query parameters
        let mut query_params = Vec::new();
        if let Some(before_id) = &request.before_id {
            query_params.push(("before_id", before_id.clone()));
        }
        if let Some(after_id) = &request.after_id {
            query_params.push(("after_id", after_id.clone()));
        }
        if let Some(limit) = request.limit {
            query_params.push(("limit", limit.to_string()));
        }
        if let Some(status) = &request.status {
            query_params.push(("status", format!("{:?}", status).to_lowercase()));
        }
        if let Some(workspace_id) = &request.workspace_id {
            query_params.push(("workspace_id", workspace_id.clone()));
        }
        if let Some(created_by_user_id) = &request.created_by_user_id {
            query_params.push(("created_by_user_id", created_by_user_id.clone()));
        }

        anthropic_request_json(
            Method::GET,
            "organizations/api_keys",
            |r| r.query(&query_params),
            credentials_opt,
        )
        .await
    }
}

impl ApiKey {
    /// Creates a builder for getting a specific API key.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let api_key = ApiKey::builder("api_key_123456789")
    ///     .credentials(credentials)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder(api_key_id: impl Into<String>) -> ApiKeyBuilder {
        ApiKeyBuilder::create_empty().api_key_id(api_key_id)
    }

    /// Gets information about a specific API key.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = ApiKeyRequest {
    ///     api_key_id: "api_key_123456789".to_string(),
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let api_key = ApiKey::create(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(request: ApiKeyRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/api_keys/{}", request.api_key_id);

        anthropic_request_json(Method::GET, &route, |r| r, credentials_opt).await
    }

    /// Creates a builder for updating an API key.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let updated_api_key = ApiKey::update_builder("api_key_123456789")
    ///     .credentials(credentials)
    ///     .name("New API Key Name")
    ///     .status(ApiKeyStatus::Inactive)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn update_builder(api_key_id: impl Into<String>) -> ApiKeyUpdateBuilder {
        ApiKeyUpdateBuilder::create_empty().api_key_id(api_key_id)
    }

    /// Updates an API key with the given request parameters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = ApiKeyUpdateRequest {
    ///     api_key_id: "api_key_123456789".to_string(),
    ///     name: Some("New API Key Name".to_string()),
    ///     status: Some(ApiKeyStatus::Inactive),
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let updated_api_key = ApiKey::update(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn update(request: ApiKeyUpdateRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/api_keys/{}", request.api_key_id);

        anthropic_request_json(Method::POST, &route, |r| r.json(&request), credentials_opt).await
    }
}

// Builder convenience methods
impl ApiKeyListBuilder {
    /// Creates a new API key list request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the API Keys API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let api_keys = ApiKeyList::builder()
    ///     .credentials(credentials)
    ///     .limit(10u32)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<ApiKeyList> {
        let request = self.build().unwrap();
        ApiKeyList::create(request).await
    }
}

impl ApiKeyBuilder {
    /// Creates a new API key request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the API Keys API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let api_key = ApiKey::builder("api_key_123456789")
    ///     .credentials(credentials)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<ApiKey> {
        let request = self.build().unwrap();
        ApiKey::create(request).await
    }
}

impl ApiKeyUpdateBuilder {
    /// Creates a new API key update request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the API Keys API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::api_keys::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let updated_api_key = ApiKey::update_builder("api_key_123456789")
    ///     .credentials(credentials)
    ///     .name("New API Key Name")
    ///     .status(ApiKeyStatus::Inactive)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<ApiKey> {
        let request = self.build().unwrap();
        ApiKey::update(request).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Credentials;

    #[tokio::test]
    #[ignore] // Requires admin API key
    async fn test_list_api_keys() {
        let credentials = Credentials::from_env();

        let api_keys = ApiKeyList::builder()
            .credentials(credentials)
            .create()
            .await
            .unwrap();

        assert!(api_keys.data.len() > 0);
    }

    #[tokio::test]
    #[ignore] // Requires admin API key
    async fn test_get_api_key() {
        let credentials = Credentials::from_env();

        // First get an API key ID from the list
        let api_keys = ApiKeyList::builder()
            .credentials(credentials.clone())
            .create()
            .await
            .unwrap();

        if let Some(api_key) = api_keys.data.first() {
            let api_key_id = &api_key.id;

            // Then get that specific API key
            let api_key_details = ApiKey::builder(api_key_id)
                .credentials(credentials)
                .create()
                .await
                .unwrap();

            assert_eq!(api_key_details.id, *api_key_id);
        }
    }
}