anthropic_ai_sdk/types/admin/
api_keys.rs1use 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#[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#[derive(Debug, Serialize, Default)]
145pub struct ListApiKeysParams {
146 #[serde(skip_serializing_if = "Option::is_none")]
148 pub before_id: Option<String>,
149 #[serde(skip_serializing_if = "Option::is_none")]
151 pub after_id: Option<String>,
152 #[serde(skip_serializing_if = "Option::is_none")]
154 pub limit: Option<u16>,
155 #[serde(skip_serializing_if = "Option::is_none")]
157 pub status: Option<ApiKeyStatus>,
158 #[serde(skip_serializing_if = "Option::is_none")]
160 pub workspace_id: Option<String>,
161 #[serde(skip_serializing_if = "Option::is_none")]
163 pub created_by_user_id: Option<String>,
164}
165
166impl ListApiKeysParams {
167 pub fn new() -> Self {
169 Self::default()
170 }
171
172 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 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 pub fn limit(mut self, limit: u16) -> Self {
186 self.limit = Some(limit.clamp(1, 1000));
187 self
188 }
189
190 pub fn status(mut self, status: ApiKeyStatus) -> Self {
192 self.status = Some(status);
193 self
194 }
195
196 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 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#[derive(Debug, Serialize, Deserialize)]
211#[serde(rename_all = "lowercase")]
212pub enum ApiKeyStatus {
213 Active,
214 Inactive,
215 Archived,
216}
217
218#[derive(Debug, Deserialize)]
220pub struct ListApiKeysResponse {
221 pub data: Vec<ApiKey>,
223 pub first_id: Option<String>,
225 pub has_more: bool,
227 pub last_id: Option<String>,
229}
230
231#[derive(Debug, Deserialize)]
233pub struct User {
234 pub id: String,
236 #[serde(rename = "type")]
238 pub type_: String,
239}
240
241#[derive(Debug, Deserialize)]
243pub struct ApiKey {
244 pub id: String,
246 #[serde(rename = "type")]
248 pub type_: String,
249 pub status: ApiKeyStatus,
251 pub name: String,
253 #[serde(with = "rfc3339")]
255 pub created_at: OffsetDateTime,
256 pub created_by: User,
258 #[serde(default)]
260 pub workspace_id: Option<String>,
261 pub partial_key_hint: String,
263}
264
265#[derive(Debug, Serialize)]
267pub struct AdminUpdateApiKeyParams {
268 #[serde(skip_serializing_if = "Option::is_none")]
270 pub name: Option<String>,
271 #[serde(skip_serializing_if = "Option::is_none")]
273 pub status: Option<ApiKeyStatus>,
274}
275
276impl AdminUpdateApiKeyParams {
277 pub fn new() -> Self {
279 Self {
280 name: None,
281 status: None,
282 }
283 }
284
285 pub fn name(mut self, name: impl Into<String>) -> Self {
287 self.name = Some(name.into());
288 self
289 }
290
291 pub fn status(mut self, status: ApiKeyStatus) -> Self {
293 self.status = Some(status);
294 self
295 }
296}