async_openai/
project_service_accounts.rs

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