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