async_openai_alt/
project_api_keys.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use serde::Serialize;

use crate::{
    config::Config,
    error::OpenAIError,
    types::{ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse},
    Client,
};

/// Manage API keys for a given project. Supports listing and deleting keys for users.
/// This API does not allow issuing keys for users, as users need to authorize themselves to generate keys.
pub struct ProjectAPIKeys<'c, C: Config> {
    client: &'c Client<C>,
    pub project_id: String,
}

impl<'c, C: Config> ProjectAPIKeys<'c, C> {
    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
        Self {
            client,
            project_id: project_id.into(),
        }
    }

    /// Returns a list of API keys in the project.
    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectApiKeyListResponse, OpenAIError>
    where
        Q: Serialize + ?Sized,
    {
        self.client
            .get_with_query(
                format!("/organization/projects/{}/api_keys", self.project_id).as_str(),
                query,
            )
            .await
    }

    /// Retrieves an API key in the project.
    pub async fn retrieve(&self, api_key: &str) -> Result<ProjectApiKey, OpenAIError> {
        self.client
            .get(
                format!(
                    "/organization/projects/{}/api_keys/{api_key}",
                    self.project_id
                )
                .as_str(),
            )
            .await
    }

    /// Deletes an API key from the project.
    pub async fn delete(&self, api_key: &str) -> Result<ProjectApiKeyDeleteResponse, OpenAIError> {
        self.client
            .delete(
                format!(
                    "/organization/projects/{}/api_keys/{api_key}",
                    self.project_id
                )
                .as_str(),
            )
            .await
    }
}