async_openai/admin/
project_users.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::project_users::{
5        ProjectUser, ProjectUserCreateRequest, ProjectUserDeleteResponse, ProjectUserListResponse,
6        ProjectUserUpdateRequest,
7    },
8    Client, RequestOptions,
9};
10
11/// Manage users within a project, including adding, updating roles, and removing users.
12/// Users cannot be removed from the Default project, unless they are being removed from the organization.
13pub struct ProjectUsers<'c, C: Config> {
14    client: &'c Client<C>,
15    pub project_id: String,
16    pub(crate) request_options: RequestOptions,
17}
18
19impl<'c, C: Config> ProjectUsers<'c, C> {
20    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
21        Self {
22            client,
23            project_id: project_id.into(),
24            request_options: RequestOptions::new(),
25        }
26    }
27
28    /// Returns a list of users in the project.
29    #[crate::byot(R = serde::de::DeserializeOwned)]
30    pub async fn list(&self) -> Result<ProjectUserListResponse, OpenAIError> {
31        self.client
32            .get(
33                format!("/organization/projects/{}/users", self.project_id).as_str(),
34                &self.request_options,
35            )
36            .await
37    }
38
39    /// Adds a user to the project. Users must already be members of the organization to be added to a project.
40    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
41    pub async fn create(
42        &self,
43        request: ProjectUserCreateRequest,
44    ) -> Result<ProjectUser, OpenAIError> {
45        self.client
46            .post(
47                format!("/organization/projects/{}/users", self.project_id).as_str(),
48                request,
49                &self.request_options,
50            )
51            .await
52    }
53
54    /// Retrieves a user in the project.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn retrieve(&self, user_id: &str) -> Result<ProjectUser, OpenAIError> {
57        self.client
58            .get(
59                format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(),
60                &self.request_options,
61            )
62            .await
63    }
64
65    /// Modifies a user's role in the project.
66    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
67    pub async fn modify(
68        &self,
69        user_id: &str,
70        request: ProjectUserUpdateRequest,
71    ) -> Result<ProjectUser, OpenAIError> {
72        self.client
73            .post(
74                format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(),
75                request,
76                &self.request_options,
77            )
78            .await
79    }
80
81    /// Deletes a user from the project.
82    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
83    pub async fn delete(&self, user_id: &str) -> Result<ProjectUserDeleteResponse, OpenAIError> {
84        self.client
85            .delete(
86                format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(),
87                &self.request_options,
88            )
89            .await
90    }
91}