async_openai_alt/
projects.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    project_api_keys::ProjectAPIKeys,
7    types::{Project, ProjectCreateRequest, ProjectListResponse, ProjectUpdateRequest},
8    Client, ProjectServiceAccounts, ProjectUsers,
9};
10
11/// Manage the projects within an organization includes creation, updating, and archiving or projects.
12/// The Default project cannot be modified or archived.
13pub struct Projects<'c, C: Config> {
14    client: &'c Client<C>,
15}
16
17impl<'c, C: Config> Projects<'c, C> {
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self { client }
20    }
21
22    // call [ProjectUsers] group APIs
23    pub fn users(&self, project_id: &str) -> ProjectUsers<C> {
24        ProjectUsers::new(self.client, project_id)
25    }
26
27    // call [ProjectServiceAccounts] group APIs
28    pub fn service_accounts(&self, project_id: &str) -> ProjectServiceAccounts<C> {
29        ProjectServiceAccounts::new(self.client, project_id)
30    }
31
32    // call [ProjectAPIKeys] group APIs
33    pub fn api_keys(&self, project_id: &str) -> ProjectAPIKeys<C> {
34        ProjectAPIKeys::new(self.client, project_id)
35    }
36
37    /// Returns a list of projects.
38    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectListResponse, OpenAIError>
39    where
40        Q: Serialize + ?Sized,
41    {
42        self.client
43            .get_with_query("/organization/projects", query)
44            .await
45    }
46
47    /// Create a new project in the organization. Projects can be created and archived, but cannot be deleted.
48    pub async fn create(&self, request: ProjectCreateRequest) -> Result<Project, OpenAIError> {
49        self.client.post("/organization/projects", request).await
50    }
51
52    /// Retrieves a project.
53    pub async fn retrieve(&self, project_id: String) -> Result<Project, OpenAIError> {
54        self.client
55            .get(format!("/organization/projects/{project_id}").as_str())
56            .await
57    }
58
59    /// Modifies a project in the organization.
60    pub async fn modify(
61        &self,
62        project_id: String,
63        request: ProjectUpdateRequest,
64    ) -> Result<Project, OpenAIError> {
65        self.client
66            .post(
67                format!("/organization/projects/{project_id}").as_str(),
68                request,
69            )
70            .await
71    }
72
73    /// Archives a project in the organization. Archived projects cannot be used or updated.
74    pub async fn archive(&self, project_id: String) -> Result<Project, OpenAIError> {
75        self.client
76            .post(
77                format!("/organization/projects/{project_id}/archive").as_str(),
78                (),
79            )
80            .await
81    }
82}