Skip to main content

novel_openai/admin/
project_service_accounts.rs

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