Skip to main content

async_openai/admin/
admin_api_keys.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::api_keys::{
5        AdminApiKey, AdminApiKeyCreateResponse, AdminApiKeyDeleteResponse, ApiKeyList,
6        CreateAdminApiKeyRequest,
7    },
8    Client, RequestOptions,
9};
10
11/// Admin API keys enable Organization Owners to programmatically manage various aspects of their
12/// organization, including users, projects, and API keys. These keys provide administrative capabilities,
13/// allowing you to automate organization management tasks.
14pub struct AdminAPIKeys<'c, C: Config> {
15    client: &'c Client<C>,
16    pub(crate) request_options: RequestOptions,
17}
18
19impl<'c, C: Config> AdminAPIKeys<'c, C> {
20    pub fn new(client: &'c Client<C>) -> Self {
21        Self {
22            client,
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// List all organization and project API keys.
28    #[crate::byot(R = serde::de::DeserializeOwned)]
29    pub async fn list(&self) -> Result<ApiKeyList, OpenAIError> {
30        self.client
31            .get("/organization/admin_api_keys", &self.request_options)
32            .await
33    }
34
35    /// Create an organization admin API key.
36    pub async fn create(
37        &self,
38        request: CreateAdminApiKeyRequest,
39    ) -> Result<AdminApiKeyCreateResponse, OpenAIError> {
40        self.client
41            .post(
42                "/organization/admin_api_keys",
43                request,
44                &self.request_options,
45            )
46            .await
47    }
48
49    /// Retrieve a single organization API key.
50    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
51    pub async fn retrieve(&self, key_id: &str) -> Result<AdminApiKey, OpenAIError> {
52        self.client
53            .get(
54                format!("/organization/admin_api_keys/{key_id}").as_str(),
55                &self.request_options,
56            )
57            .await
58    }
59
60    /// Delete an organization admin API key.
61    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
62    pub async fn delete(&self, key_id: &str) -> Result<AdminApiKeyDeleteResponse, OpenAIError> {
63        self.client
64            .delete(
65                format!("/organization/admin_api_keys/{key_id}").as_str(),
66                &self.request_options,
67            )
68            .await
69    }
70}