dynamo_async_openai/
project_api_keys.rs

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