async_openai/
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    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
39    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectListResponse, OpenAIError>
40    where
41        Q: Serialize + ?Sized,
42    {
43        self.client
44            .get_with_query("/organization/projects", &query)
45            .await
46    }
47
48    /// Create a new project in the organization. Projects can be created and archived, but cannot be deleted.
49    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
50    pub async fn create(&self, request: ProjectCreateRequest) -> Result<Project, OpenAIError> {
51        self.client.post("/organization/projects", request).await
52    }
53
54    /// Retrieves a project.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn retrieve(&self, project_id: String) -> Result<Project, OpenAIError> {
57        self.client
58            .get(format!("/organization/projects/{project_id}").as_str())
59            .await
60    }
61
62    /// Modifies a project in the organization.
63    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
64    pub async fn modify(
65        &self,
66        project_id: String,
67        request: ProjectUpdateRequest,
68    ) -> Result<Project, OpenAIError> {
69        self.client
70            .post(
71                format!("/organization/projects/{project_id}").as_str(),
72                request,
73            )
74            .await
75    }
76
77    /// Archives a project in the organization. Archived projects cannot be used or updated.
78    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
79    pub async fn archive(&self, project_id: String) -> Result<Project, OpenAIError> {
80        self.client
81            .post(
82                format!("/organization/projects/{project_id}/archive").as_str(),
83                (),
84            )
85            .await
86    }
87}