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
559
560
561
562
563
564
565
566
567
568
569
570
571
//! # Organization Invites Admin API
//!
//! This module provides a Rust interface to Anthropic's Admin API for managing organization invites, which allows you to
//! list, get, create, and delete invites to your organization.
//!
//! ## Key Features
//!
//! - List all invites with pagination support
//! - Get detailed information about a specific invite
//! - Create new invites to the organization
//! - Delete pending invites
//!
//! ## Basic Usage
//!
//! ```no_run
//! use anthropic_api::{admin::invites::*, Credentials};
//!
//! #[tokio::main]
//! async fn main() {
//!     let credentials = Credentials::from_env();
//!
//!     // List invites
//!     let invites = InviteList::builder()
//!         .credentials(credentials.clone())
//!         .create()
//!         .await
//!         .unwrap();
//!
//!     println!("Organization invites: {:?}", invites.data);
//!
//!     // Get a specific invite
//!     if let Some(invite) = invites.data.first() {
//!         let invite_details = Invite::builder(&invite.id)
//!             .credentials(credentials.clone())
//!             .create()
//!             .await
//!             .unwrap();
//!
//!         println!("Invite details: {:?}", invite_details);
//!     }
//! }
//! ```

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

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

/// Status of an invite
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum InviteStatus {
    /// Invite has been accepted
    Accepted,
    /// Invite has expired
    Expired,
    /// Invite has been deleted
    Deleted,
    /// Invite is pending acceptance
    Pending,
}

/// An invite to the organization
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct Invite {
    /// Unique invite identifier
    pub id: String,
    /// Email of the user being invited
    pub email: String,
    /// RFC 3339 datetime string indicating when the invite was created
    pub invited_at: String,
    /// RFC 3339 datetime string indicating when the invite expires
    pub expires_at: String,
    /// Role assigned to the invited user
    pub role: InviteRole,
    /// Current status of the invite
    pub status: InviteStatus,
    /// Object type (always "invite" for Invites)
    #[serde(rename = "type")]
    pub invite_type: String,
}

/// Response from the List Invites API
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct InviteList {
    /// List of invites in the organization
    pub data: Vec<Invite>,
    /// 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 Delete Invite API
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct InviteDeleted {
    /// ID of the deleted invite
    pub id: String,
    /// Object type (always "invite_deleted" for deleted invites)
    #[serde(rename = "type")]
    pub deleted_type: String,
}

/// Request parameters for listing invites
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "InviteListBuilder")]
#[builder(setter(strip_option, into))]
pub struct InviteListRequest {
    /// 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>,

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

/// Request parameters for getting a specific invite
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "InviteBuilder")]
#[builder(setter(strip_option, into))]
pub struct InviteRequest {
    /// Invite identifier
    pub invite_id: String,

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

/// Request parameters for creating an invite
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "InviteCreateBuilder")]
#[builder(setter(strip_option, into))]
pub struct InviteCreateRequest {
    /// Email of the user to invite
    pub email: String,

    /// Role for the invited user (cannot be "admin")
    pub role: InviteRole,

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

/// Request parameters for deleting an invite
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "InviteDeleteBuilder")]
#[builder(setter(strip_option, into))]
pub struct InviteDeleteRequest {
    /// Invite identifier
    pub invite_id: String,

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

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

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

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

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

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

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

    /// Creates a builder for creating a new invite.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::invites::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let new_invite = Invite::create_builder()
    ///     .credentials(credentials)
    ///     .email("user@example.com")
    ///     .role(InviteRole::Developer)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_builder() -> InviteCreateBuilder {
        InviteCreateBuilder::create_empty()
    }

    /// Creates a new invite with the given request parameters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::invites::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = InviteCreateRequest {
    ///     email: "user@example.com".to_string(),
    ///     role: InviteRole::Developer,
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let new_invite = Invite::create_new(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create_new(request: InviteCreateRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();

        anthropic_request_json(
            Method::POST,
            "organizations/invites",
            |r| r.json(&request),
            credentials_opt,
        )
        .await
    }

    /// Creates a builder for deleting an invite.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::invites::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let deleted_invite = Invite::delete_builder("invite_123456789")
    ///     .credentials(credentials)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn delete_builder(invite_id: impl Into<String>) -> InviteDeleteBuilder {
        InviteDeleteBuilder::create_empty().invite_id(invite_id)
    }

    /// Deletes an invite from the organization.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::invites::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = InviteDeleteRequest {
    ///     invite_id: "invite_123456789".to_string(),
    ///     credentials: Some(credentials),
    /// };
    ///
    /// let deleted_invite = Invite::delete(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete(request: InviteDeleteRequest) -> ApiResponseOrError<InviteDeleted> {
        let credentials_opt = request.credentials.clone();
        let route = format!("organizations/invites/{}", request.invite_id);

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

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

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

impl InviteCreateBuilder {
    /// Creates a new invite create request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the Invites API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{admin::invites::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let new_invite = Invite::create_builder()
    ///     .credentials(credentials)
    ///     .email("user@example.com")
    ///     .role(InviteRole::Developer)
    ///     .create()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<Invite> {
        let request = self.build().unwrap();
        Invite::create_new(request).await
    }
}

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

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

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

        let invites = InviteList::builder()
            .credentials(credentials)
            .create()
            .await
            .unwrap();

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

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

        // First get an invite ID from the list
        let invites = InviteList::builder()
            .credentials(credentials.clone())
            .create()
            .await
            .unwrap();

        if let Some(invite) = invites.data.first() {
            let invite_id = &invite.id;

            // Then get that specific invite
            let invite_details = Invite::builder(invite_id)
                .credentials(credentials)
                .create()
                .await
                .unwrap();

            assert_eq!(invite_details.id, *invite_id);
        }
    }
}