async_openai_wasm/
project_service_accounts.rs

1use serde::Serialize;
2
3use crate::{
4    Client,
5    config::Config,
6    error::OpenAIError,
7    types::{
8        ProjectServiceAccount, ProjectServiceAccountCreateRequest,
9        ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse,
10        ProjectServiceAccountListResponse,
11    },
12};
13
14/// 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>,
20    pub project_id: String,
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        }
29    }
30
31    /// Returns a list of service accounts in the project.
32    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
33    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectServiceAccountListResponse, OpenAIError>
34    where
35        Q: Serialize + ?Sized,
36    {
37        self.client
38            .get_with_query(
39                format!(
40                    "/organization/projects/{}/service_accounts",
41                    self.project_id
42                )
43                .as_str(),
44                &query,
45            )
46            .await
47    }
48
49    /// 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)]
51    pub async fn create(
52        &self,
53        request: ProjectServiceAccountCreateRequest,
54    ) -> Result<ProjectServiceAccountCreateResponse, OpenAIError> {
55        self.client
56            .post(
57                format!(
58                    "/organization/projects/{}/service_accounts",
59                    self.project_id
60                )
61                .as_str(),
62                request,
63            )
64            .await
65    }
66
67    /// Retrieves a service account in the project.
68    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
69    pub async fn retrieve(
70        &self,
71        service_account_id: &str,
72    ) -> Result<ProjectServiceAccount, OpenAIError> {
73        self.client
74            .get(
75                format!(
76                    "/organization/projects/{}/service_accounts/{service_account_id}",
77                    self.project_id
78                )
79                .as_str(),
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            )
98            .await
99    }
100}