Skip to main content

async_openai/admin/
project_service_accounts.rs

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