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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! # Organization Members Admin API
//!
//! This module provides a Rust interface to Anthropic's Admin API for managing organization members, which allows you to
//! list, get, update, and remove users from your organization.
//!
//! ## Key Features
//!
//! - List all users with pagination and filtering support
//! - Get detailed information about a specific user
//! - Update user roles within the organization
//! - Remove users from the organization
//!
//! ## Basic Usage
//!
//! ```no_run
//! use anthropic_api::{admin::members::*, Credentials};
//!
//! #[tokio::main]
//! async fn main() {
//!     let credentials = Credentials::from_env();
//!
//!     // List users
//!     let users = UserList::builder()
//!         .credentials(credentials.clone())
//!         .create()
//!         .await
//!         .unwrap();
//!
//!     println!("Organization members: {:?}", users.data);
//!
//!     // Get a specific user
//!     if let Some(user) = users.data.first() {
//!         let user_details = User::builder(&user.id)
//!             .credentials(credentials.clone())
//!             .create()
//!             .await
//!             .unwrap();
//!
//!         println!("User details: {:?}", user_details);
//!     }
//! }
//! ```

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

/// Organization role of a user
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum UserRole {
    /// Regular user
    User,
    /// Developer role
    Developer,
    /// Billing administrator
    Billing,
    /// Organization administrator
    Admin,
}

/// A user in the organization
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct User {
    /// Unique user identifier
    pub id: String,
    /// User's email address
    pub email: String,
    /// User's name
    pub name: String,
    /// RFC 3339 datetime string indicating when the user joined the organization
    pub added_at: String,
    /// User's role in the organization
    pub role: UserRole,
    /// Object type (always "user" for Users)
    #[serde(rename = "type")]
    pub user_type: String,
}

/// Response from the List Users API
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct UserList {
    /// List of users in the organization
    pub data: Vec<User>,
    /// 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,
}

/// Response from the Remove User API
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct UserDeleted {
    /// ID of the deleted user
    pub id: String,
    /// Object type (always "user_deleted" for deleted users)
    #[serde(rename = "type")]
    pub deleted_type: String,
}

/// Request parameters for listing users
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "UserListBuilder")]
#[builder(setter(strip_option, into))]
pub struct UserListRequest {
    /// 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 user email
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

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

/// Request parameters for getting a specific user
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "UserBuilder")]
#[builder(setter(strip_option, into))]
pub struct UserRequest {
    /// User identifier
    pub user_id: String,

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

/// Request parameters for updating a user
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "UserUpdateBuilder")]
#[builder(setter(strip_option, into))]
pub struct UserUpdateRequest {
    /// User identifier (not serialized)
    #[serde(skip_serializing)]
    pub user_id: String,

    /// New role for the user (cannot be "admin")
    pub role: UserRole,

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

/// Request parameters for removing a user
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "UserRemoveBuilder")]
#[builder(setter(strip_option, into))]
pub struct UserRemoveRequest {
    /// User identifier
    pub user_id: String,

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

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

    /// Lists users in the organization with the given request parameters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = UserListRequest {
    ///     before_id: None,
    ///     after_id: None,
    ///     limit: Some(20),
    ///     email: None,
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let users = UserList::create(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(request: UserListRequest) -> 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(email) = &request.email {
            query_params.push(("email", email.clone()));
        }

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

impl User {
    /// Creates a builder for getting a specific user.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let user = User::builder("user_123456789")
    ///     .credentials(credentials)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder(user_id: impl Into<String>) -> UserBuilder {
        UserBuilder::create_empty().user_id(user_id)
    }

    /// Gets information about a specific user.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = UserRequest {
    ///     user_id: "user_123456789".to_string(),
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let user = User::create(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(request: UserRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/users/{}", request.user_id);

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

    /// Creates a builder for updating a user.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let updated_user = User::update_builder("user_123456789")
    ///     .credentials(credentials)
    ///     .role(UserRole::Developer)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn update_builder(user_id: impl Into<String>) -> UserUpdateBuilder {
        UserUpdateBuilder::create_empty().user_id(user_id)
    }

    /// Updates a user with the given request parameters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = UserUpdateRequest {
    ///     user_id: "user_123456789".to_string(),
    ///     role: UserRole::Developer,
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let updated_user = User::update(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn update(request: UserUpdateRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/users/{}", request.user_id);

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

    /// Creates a builder for removing a user.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let removed_user = User::remove_builder("user_123456789")
    ///     .credentials(credentials)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn remove_builder(user_id: impl Into<String>) -> UserRemoveBuilder {
        UserRemoveBuilder::create_empty().user_id(user_id)
    }

    /// Removes a user from the organization.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = UserRemoveRequest {
    ///     user_id: "user_123456789".to_string(),
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let removed_user = User::remove(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn remove(request: UserRemoveRequest) -> ApiResponseOrError<UserDeleted> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/users/{}", request.user_id);

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

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

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

impl UserUpdateBuilder {
    /// Creates a new user update request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the Users API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::members::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let updated_user = User::update_builder("user_123456789")
    ///     .credentials(credentials)
    ///     .role(UserRole::Developer)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<User> {
        let request = self.build().unwrap();
        User::update(request).await
    }
}

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

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

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

        let users = UserList::builder()
            .credentials(credentials)
            .create()
            .await
            .unwrap();

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

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

        // First get a user ID from the list
        let users = UserList::builder()
            .credentials(credentials.clone())
            .create()
            .await
            .unwrap();

        if let Some(user) = users.data.first() {
            let user_id = &user.id;

            // Then get that specific user
            let user_details = User::builder(user_id)
                .credentials(credentials)
                .create()
                .await
                .unwrap();

            assert_eq!(user_details.id, *user_id);
        }
    }
}