async_openai_alt/
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    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectApiKeyListResponse, OpenAIError>
27    where
28        Q: Serialize + ?Sized,
29    {
30        self.client
31            .get_with_query(
32                format!("/organization/projects/{}/api_keys", self.project_id).as_str(),
33                query,
34            )
35            .await
36    }
37
38    /// Retrieves an API key in the project.
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            )
48            .await
49    }
50
51    /// Deletes an API key from the project.
52    pub async fn delete(&self, api_key: &str) -> Result<ProjectApiKeyDeleteResponse, OpenAIError> {
53        self.client
54            .delete(
55                format!(
56                    "/organization/projects/{}/api_keys/{api_key}",
57                    self.project_id
58                )
59                .as_str(),
60            )
61            .await
62    }
63}