async_openai/
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    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
30    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectUserListResponse, OpenAIError>
31    where
32        Q: Serialize + ?Sized,
33    {
34        self.client
35            .get_with_query(
36                format!("/organization/projects/{}/users", self.project_id).as_str(),
37                &query,
38            )
39            .await
40    }
41
42    /// Adds a user to the project. Users must already be members of the organization to be added to a project.
43    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
44    pub async fn create(
45        &self,
46        request: ProjectUserCreateRequest,
47    ) -> Result<ProjectUser, OpenAIError> {
48        self.client
49            .post(
50                format!("/organization/projects/{}/users", self.project_id).as_str(),
51                request,
52            )
53            .await
54    }
55
56    /// Retrieves a user in the project.
57    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
58    pub async fn retrieve(&self, user_id: &str) -> Result<ProjectUser, OpenAIError> {
59        self.client
60            .get(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str())
61            .await
62    }
63
64    /// Modifies a user's role in the project.
65    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
66    pub async fn modify(
67        &self,
68        user_id: &str,
69        request: ProjectUserUpdateRequest,
70    ) -> Result<ProjectUser, OpenAIError> {
71        self.client
72            .post(
73                format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(),
74                request,
75            )
76            .await
77    }
78
79    /// Deletes a user from the project.
80    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
81    pub async fn delete(&self, user_id: &str) -> Result<ProjectUserDeleteResponse, OpenAIError> {
82        self.client
83            .delete(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str())
84            .await
85    }
86}