async_openai/
project_api_keys.rs

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