async_openai/
project_api_keys.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse},
7    Client,
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}
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        }
23    }
24
25    /// Returns a list of API keys in the project.
26    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
27    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectApiKeyListResponse, OpenAIError>
28    where
29        Q: Serialize + ?Sized,
30    {
31        self.client
32            .get_with_query(
33                format!("/organization/projects/{}/api_keys", self.project_id).as_str(),
34                &query,
35            )
36            .await
37    }
38
39    /// Retrieves an API key in the project.
40    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
41    pub async fn retrieve(&self, api_key: &str) -> Result<ProjectApiKey, OpenAIError> {
42        self.client
43            .get(
44                format!(
45                    "/organization/projects/{}/api_keys/{api_key}",
46                    self.project_id
47                )
48                .as_str(),
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            )
64            .await
65    }
66}