async_openai_compat/
project_users.rs

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