Skip to main content

novel_openai/admin/
project_api_keys.rs

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