|
| 1 | +use serde::Serialize; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + config::Config, |
| 5 | + error::OpenAIError, |
| 6 | + types::{ |
| 7 | + ProjectServiceAccount, ProjectServiceAccountCreateRequest, |
| 8 | + ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse, |
| 9 | + ProjectServiceAccountListResponse, |
| 10 | + }, |
| 11 | + Client, |
| 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. |
| 18 | +pub struct ProjectServiceAccounts<'c, C: Config> { |
| 19 | + client: &'c Client<C>, |
| 20 | + pub project_id: String, |
| 21 | +} |
| 22 | + |
| 23 | +impl<'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 | + pub async fn list<Q>(&self, query: &Q) -> Result<ProjectServiceAccountListResponse, OpenAIError> |
| 33 | + where |
| 34 | + Q: Serialize + ?Sized, |
| 35 | + { |
| 36 | + self.client |
| 37 | + .get_with_query( |
| 38 | + format!( |
| 39 | + "/organization/projects/{}/service_accounts", |
| 40 | + self.project_id |
| 41 | + ) |
| 42 | + .as_str(), |
| 43 | + query, |
| 44 | + ) |
| 45 | + .await |
| 46 | + } |
| 47 | + |
| 48 | + /// Creates a new service account in the project. This also returns an unredacted API key for the service account. |
| 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 | + ) |
| 62 | + .await |
| 63 | + } |
| 64 | + |
| 65 | + /// Retrieves a service account in the project. |
| 66 | + pub async fn retrieve( |
| 67 | + &self, |
| 68 | + service_account_id: &str, |
| 69 | + ) -> Result<ProjectServiceAccount, OpenAIError> { |
| 70 | + self.client |
| 71 | + .get( |
| 72 | + format!( |
| 73 | + "/organization/projects/{}/service_accounts/{service_account_id}", |
| 74 | + self.project_id |
| 75 | + ) |
| 76 | + .as_str(), |
| 77 | + ) |
| 78 | + .await |
| 79 | + } |
| 80 | + |
| 81 | + /// Deletes a service account from the project. |
| 82 | + pub async fn delete( |
| 83 | + &self, |
| 84 | + service_account_id: &str, |
| 85 | + ) -> Result<ProjectServiceAccountDeleteResponse, OpenAIError> { |
| 86 | + self.client |
| 87 | + .delete( |
| 88 | + format!( |
| 89 | + "/organization/projects/{}/service_accounts/{service_account_id}", |
| 90 | + self.project_id |
| 91 | + ) |
| 92 | + .as_str(), |
| 93 | + ) |
| 94 | + .await |
| 95 | + } |
| 96 | +} |
0 commit comments