Skip to main content

novel_openai/admin/
project_users.rs

1use crate::config::Config;
2use crate::error::OpenAIError;
3use crate::spec::admin::project_users::{
4    ProjectUser, ProjectUserCreateRequest, ProjectUserDeleteResponse, ProjectUserListResponse,
5    ProjectUserUpdateRequest,
6};
7use crate::{Client, RequestOptions};
8
9/// Manage users within a project, including adding, updating roles, and removing users.
10/// Users cannot be removed from the Default project, unless they are being removed from the
11/// organization.
12pub struct ProjectUsers<'c, C: Config> {
13    client: &'c Client<C>,
14    pub project_id: String,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> ProjectUsers<'c, C> {
19    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
20        Self {
21            client,
22            project_id: project_id.into(),
23            request_options: RequestOptions::new(),
24        }
25    }
26
27    /// Returns a list of users in the project.
28    #[crate::byot(R = serde::de::DeserializeOwned)]
29    pub async fn list(&self) -> Result<ProjectUserListResponse, OpenAIError> {
30        self.client
31            .get(
32                format!("/organization/projects/{}/users", self.project_id).as_str(),
33                &self.request_options,
34            )
35            .await
36    }
37
38    /// Adds a user to the project. Users must already be members of the organization to be added to
39    /// 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}