Skip to main content

novel_openai/admin/
admin_api_keys.rs

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