dynamo_async_openai/
project_service_accounts.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    Client,
15    config::Config,
16    error::OpenAIError,
17    types::{
18        ProjectServiceAccount, ProjectServiceAccountCreateRequest,
19        ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse,
20        ProjectServiceAccountListResponse,
21    },
22};
23
24/// Manage service accounts within a project. A service account is a bot user that is not
25/// associated with a user. If a user leaves an organization, their keys and membership in projects
26/// will no longer work. Service accounts do not have this limitation.
27/// However, service accounts can also be deleted from a project.
28pub struct ProjectServiceAccounts<'c, C: Config> {
29    client: &'c Client<C>,
30    pub project_id: String,
31}
32
33impl<'c, C: Config> ProjectServiceAccounts<'c, C> {
34    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
35        Self {
36            client,
37            project_id: project_id.into(),
38        }
39    }
40
41    /// Returns a list of service accounts in the project.
42    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
43    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectServiceAccountListResponse, OpenAIError>
44    where
45        Q: Serialize + ?Sized,
46    {
47        self.client
48            .get_with_query(
49                format!(
50                    "/organization/projects/{}/service_accounts",
51                    self.project_id
52                )
53                .as_str(),
54                &query,
55            )
56            .await
57    }
58
59    /// Creates a new service account in the project. This also returns an unredacted API key for the service account.
60    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
61    pub async fn create(
62        &self,
63        request: ProjectServiceAccountCreateRequest,
64    ) -> Result<ProjectServiceAccountCreateResponse, OpenAIError> {
65        self.client
66            .post(
67                format!(
68                    "/organization/projects/{}/service_accounts",
69                    self.project_id
70                )
71                .as_str(),
72                request,
73            )
74            .await
75    }
76
77    /// Retrieves a service account in the project.
78    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
79    pub async fn retrieve(
80        &self,
81        service_account_id: &str,
82    ) -> Result<ProjectServiceAccount, OpenAIError> {
83        self.client
84            .get(
85                format!(
86                    "/organization/projects/{}/service_accounts/{service_account_id}",
87                    self.project_id
88                )
89                .as_str(),
90            )
91            .await
92    }
93
94    /// Deletes a service account from the project.
95    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
96    pub async fn delete(
97        &self,
98        service_account_id: &str,
99    ) -> Result<ProjectServiceAccountDeleteResponse, OpenAIError> {
100        self.client
101            .delete(
102                format!(
103                    "/organization/projects/{}/service_accounts/{service_account_id}",
104                    self.project_id
105                )
106                .as_str(),
107            )
108            .await
109    }
110}