anthropic_ai_sdk/types/admin/
api_keys.rs

1//! Admin API
2//!
3//! This module contains the types and functions for the Anthropic Admin API.
4//!
5use async_trait::async_trait;
6use serde::{Deserialize, Serialize};
7use super::users::{ListUsersParams, ListUsersResponse};
8use super::workspaces::{
9    GetWorkspaceResponse, ListWorkspacesParams, ListWorkspacesResponse,
10};
11use thiserror::Error;
12use super::workspace_members::{GetWorkspaceMemberResponse, ListWorkspaceMembersParams, ListWorkspaceMembersResponse};
13use super::invites::{
14    DeleteInviteResponse, GetInviteResponse, ListInvitesParams, ListInvitesResponse,
15};
16use time::OffsetDateTime;
17use time::serde::rfc3339;
18
19/// Error types for the Admin API
20#[derive(Debug, Error)]
21pub enum AdminError {
22    #[error("Invalid pagination parameters")]
23    InvalidPagination,
24    #[error("Invalid limit value: {0}")]
25    InvalidLimit(u16),
26    #[error("API request failed: {0}")]
27    RequestFailed(String),
28    #[error("API error: {0}")]
29    ApiError(String),
30}
31
32impl From<String> for AdminError {
33    fn from(error: String) -> Self {
34        AdminError::ApiError(error)
35    }
36}
37
38#[async_trait]
39pub trait AdminClient {
40    async fn list_api_keys<'a>(
41        &'a self,
42        params: Option<&'a ListApiKeysParams>,
43    ) -> Result<ListApiKeysResponse, AdminError>;
44
45    async fn get_api_key_by_id<'a>(&'a self, api_key_id: &'a str) -> Result<ApiKey, AdminError>;
46
47    async fn update_api_key<'a>(
48        &'a self,
49        api_key_id: &'a str,
50        params: &'a AdminUpdateApiKeyParams,
51    ) -> Result<ApiKey, AdminError>;
52
53    async fn list_users<'a>(
54        &'a self,
55        params: Option<&'a ListUsersParams>,
56    ) -> Result<ListUsersResponse, AdminError>;
57
58    async fn get_user<'a>(&'a self, user_id: &'a str) -> Result<crate::types::admin::users::OrganizationUser, AdminError>;
59
60    async fn update_user<'a>(
61        &'a self,
62        user_id: &'a str,
63        params: &'a crate::types::admin::users::AdminUpdateUserParams,
64    ) -> Result<crate::types::admin::users::OrganizationUser, AdminError>;
65
66    async fn delete_user<'a>(
67        &'a self,
68        user_id: &'a str,
69    ) -> Result<crate::types::admin::users::DeleteUserResponse, AdminError>;
70
71    async fn list_workspaces<'a>(
72        &'a self,
73        params: Option<&'a ListWorkspacesParams>,
74    ) -> Result<ListWorkspacesResponse, AdminError>;
75
76    async fn create_workspace<'a>(
77        &'a self,
78        params: &'a crate::types::admin::workspaces::AdminCreateWorkspaceParams,
79    ) -> Result<crate::types::admin::workspaces::CreateWorkspaceResponse, AdminError>;
80
81    async fn get_workspace<'a>(&'a self, workspace_id: &'a str) -> Result<GetWorkspaceResponse, AdminError>;
82
83    async fn update_workspace<'a>(
84        &'a self,
85        workspace_id: &'a str,
86        params: &'a crate::types::admin::workspaces::AdminUpdateWorkspaceParams,
87    ) -> Result<crate::types::admin::workspaces::Workspace, AdminError>;
88
89    async fn archive_workspace<'a>(
90        &'a self,
91        workspace_id: &'a str,
92    ) -> Result<crate::types::admin::workspaces::ArchiveWorkspaceResponse, AdminError>;
93
94    async fn list_workspace_members<'a>(
95        &'a self,
96        workspace_id: &'a str,
97        params: Option<&'a ListWorkspaceMembersParams>,
98    ) -> Result<ListWorkspaceMembersResponse, AdminError>;
99
100    async fn get_workspace_member<'a>(
101        &'a self,
102        workspace_id: &'a str,
103        user_id: &'a str,
104    ) -> Result<GetWorkspaceMemberResponse, AdminError>;
105
106    async fn add_workspace_member<'a>(
107        &'a self,
108        workspace_id: &'a str,
109        params: &'a crate::types::admin::workspace_members::AdminAddWorkspaceMemberParams,
110    ) -> Result<crate::types::admin::workspace_members::WorkspaceMember, AdminError>;
111
112    async fn update_workspace_member<'a>(
113        &'a self,
114        workspace_id: &'a str,
115        user_id: &'a str,
116        params: &'a crate::types::admin::workspace_members::AdminUpdateWorkspaceMemberParams,
117    ) -> Result<crate::types::admin::workspace_members::WorkspaceMember, AdminError>;
118
119    async fn delete_workspace_member<'a>(
120        &'a self,
121        workspace_id: &'a str,
122        user_id: &'a str,
123    ) -> Result<crate::types::admin::workspace_members::DeleteWorkspaceMemberResponse, AdminError>;
124
125    async fn list_invites<'a>(
126        &'a self,
127        params: Option<&'a ListInvitesParams>,
128    ) -> Result<ListInvitesResponse, AdminError>;
129
130    async fn create_invite<'a>(
131        &'a self,
132        params: &'a crate::types::admin::invites::CreateInviteParams,
133    ) -> Result<crate::types::admin::invites::Invite, AdminError>;
134
135    async fn get_invite<'a>(&'a self, invite_id: &'a str) -> Result<GetInviteResponse, AdminError>;
136
137    async fn delete_invite<'a>(
138        &'a self,
139        invite_id: &'a str,
140    ) -> Result<DeleteInviteResponse, AdminError>;
141}
142
143/// Parameters for listing API keys
144#[derive(Debug, Serialize, Default)]
145pub struct ListApiKeysParams {
146    /// Cursor for pagination (before)
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub before_id: Option<String>,
149    /// Cursor for pagination (after)
150    #[serde(skip_serializing_if = "Option::is_none")]
151    pub after_id: Option<String>,
152    /// Number of items per page (1-1000)
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub limit: Option<u16>,
155    /// Filter by API key status
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub status: Option<ApiKeyStatus>,
158    /// Filter by Workspace ID
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub workspace_id: Option<String>,
161    /// Filter by the ID of the User who created the object
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub created_by_user_id: Option<String>,
164}
165
166impl ListApiKeysParams {
167    /// Create a new ListApiKeysParams with default values
168    pub fn new() -> Self {
169        Self::default()
170    }
171
172    /// Set the before_id parameter
173    pub fn before_id(mut self, before_id: impl Into<String>) -> Self {
174        self.before_id = Some(before_id.into());
175        self
176    }
177
178    /// Set the after_id parameter
179    pub fn after_id(mut self, after_id: impl Into<String>) -> Self {
180        self.after_id = Some(after_id.into());
181        self
182    }
183
184    /// Set the limit parameter (1-1000)
185    pub fn limit(mut self, limit: u16) -> Self {
186        self.limit = Some(limit.clamp(1, 1000));
187        self
188    }
189
190    /// Set the status parameter
191    pub fn status(mut self, status: ApiKeyStatus) -> Self {
192        self.status = Some(status);
193        self
194    }
195
196    /// Set the workspace_id parameter
197    pub fn workspace_id(mut self, workspace_id: impl Into<String>) -> Self {
198        self.workspace_id = Some(workspace_id.into());
199        self
200    }
201
202    /// Set the created_by_user_id parameter
203    pub fn created_by_user_id(mut self, created_by_user_id: impl Into<String>) -> Self {
204        self.created_by_user_id = Some(created_by_user_id.into());
205        self
206    }
207}
208
209/// API key status
210#[derive(Debug, Serialize, Deserialize)]
211#[serde(rename_all = "lowercase")]
212pub enum ApiKeyStatus {
213    Active,
214    Inactive,
215    Archived,
216}
217
218/// Response structure for listing API keys
219#[derive(Debug, Deserialize)]
220pub struct ListApiKeysResponse {
221    /// List of API keys
222    pub data: Vec<ApiKey>,
223    /// First ID in the data list
224    pub first_id: Option<String>,
225    /// Indicates if there are more results
226    pub has_more: bool,
227    /// Last ID in the data list
228    pub last_id: Option<String>,
229}
230
231/// User information
232#[derive(Debug, Deserialize)]
233pub struct User {
234    /// Unique identifier for the user
235    pub id: String,
236    /// Type of the resource (always "user")
237    #[serde(rename = "type")]
238    pub type_: String,
239}
240
241/// Represents an API key
242#[derive(Debug, Deserialize)]
243pub struct ApiKey {
244    /// Unique identifier for the API key
245    pub id: String,
246    /// Type of the resource (always "api_key")
247    #[serde(rename = "type")]
248    pub type_: String,
249    /// Status of the API key
250    pub status: ApiKeyStatus,
251    /// Name of the API key
252    pub name: String,
253    /// Creation timestamp
254    #[serde(with = "rfc3339")]
255    pub created_at: OffsetDateTime,
256    /// Information about the user who created the API key
257    pub created_by: User,
258    /// ID of the workspace this API key belongs to
259    #[serde(default)]
260    pub workspace_id: Option<String>,
261    /// Partial key hint for display purposes
262    pub partial_key_hint: String,
263}
264
265/// Parameters for updating an API key
266#[derive(Debug, Serialize)]
267pub struct AdminUpdateApiKeyParams {
268    /// Name of the API key
269    #[serde(skip_serializing_if = "Option::is_none")]
270    pub name: Option<String>,
271    /// Status of the API key
272    #[serde(skip_serializing_if = "Option::is_none")]
273    pub status: Option<ApiKeyStatus>,
274}
275
276impl AdminUpdateApiKeyParams {
277    /// Create a new UpdateApiKeyParams with default values
278    pub fn new() -> Self {
279        Self {
280            name: None,
281            status: None,
282        }
283    }
284
285    /// Set the name of the API key
286    pub fn name(mut self, name: impl Into<String>) -> Self {
287        self.name = Some(name.into());
288        self
289    }
290
291    /// Set the status of the API key
292    pub fn status(mut self, status: ApiKeyStatus) -> Self {
293        self.status = Some(status);
294        self
295    }
296}