Skip to main content

async_openai/admin/
project_api_keys.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::project_api_keys::{
5        ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse,
6    },
7    Client, RequestOptions,
8};
9
10/// Manage API keys for a given project. Supports listing and deleting keys for users.
11/// This API does not allow issuing keys for users, as users need to authorize themselves to generate keys.
12pub struct ProjectAPIKeys<'c, C: Config> {
13    client: &'c Client<C>,
14    pub project_id: String,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> ProjectAPIKeys<'c, C> {
19    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
20        Self {
21            client,
22            project_id: project_id.into(),
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// Returns a list of API keys in the project.
28    #[crate::byot(R = serde::de::DeserializeOwned)]
29    pub async fn list(&self) -> Result<ProjectApiKeyListResponse, OpenAIError> {
30        self.client
31            .get(
32                format!("/organization/projects/{}/api_keys", self.project_id).as_str(),
33                &self.request_options,
34            )
35            .await
36    }
37
38    /// Retrieves an API key in the project.
39    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
40    pub async fn retrieve(&self, api_key: &str) -> Result<ProjectApiKey, OpenAIError> {
41        self.client
42            .get(
43                format!(
44                    "/organization/projects/{}/api_keys/{api_key}",
45                    self.project_id
46                )
47                .as_str(),
48                &self.request_options,
49            )
50            .await
51    }
52
53    /// Deletes an API key from the project.
54    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
55    pub async fn delete(&self, api_key: &str) -> Result<ProjectApiKeyDeleteResponse, OpenAIError> {
56        self.client
57            .delete(
58                format!(
59                    "/organization/projects/{}/api_keys/{api_key}",
60                    self.project_id
61                )
62                .as_str(),
63                &self.request_options,
64            )
65            .await
66    }
67
68    /// Update an API key's scope restrictions or expiration.
69    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
70    pub async fn update(
71        &self,
72        api_key_id: &str,
73        request: crate::types::admin::project_api_keys::UpdateProjectApiKeyBody,
74    ) -> Result<ProjectApiKey, OpenAIError> {
75        self.client
76            .post(
77                &format!(
78                    "/organization/projects/{}/api_keys/{api_key_id}",
79                    self.project_id
80                ),
81                request,
82                &self.request_options,
83            )
84            .await
85    }
86}