async_openai/admin/
users.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::users::{User, UserDeleteResponse, UserListResponse, UserRoleUpdateRequest},
5    Client, RequestOptions, UserRoles,
6};
7
8/// Manage users and their role in an organization. Users will be automatically added to the Default project.
9pub struct Users<'c, C: Config> {
10    client: &'c Client<C>,
11    pub(crate) request_options: RequestOptions,
12}
13
14impl<'c, C: Config> Users<'c, C> {
15    pub fn new(client: &'c Client<C>) -> Self {
16        Self {
17            client,
18            request_options: RequestOptions::new(),
19        }
20    }
21
22    /// To call [UserRoles] group related APIs using this client.
23    pub fn roles(&self, user_id: &str) -> UserRoles<'_, C> {
24        UserRoles::new(self.client, user_id)
25    }
26
27    /// Lists all of the users in the organization.
28    #[crate::byot(R = serde::de::DeserializeOwned)]
29    pub async fn list(&self) -> Result<UserListResponse, OpenAIError> {
30        self.client
31            .get("/organization/users", &self.request_options)
32            .await
33    }
34
35    /// Modifies a user's role in the organization.
36    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
37    pub async fn modify(
38        &self,
39        user_id: &str,
40        request: UserRoleUpdateRequest,
41    ) -> Result<User, OpenAIError> {
42        self.client
43            .post(
44                format!("/organization/users/{user_id}").as_str(),
45                request,
46                &self.request_options,
47            )
48            .await
49    }
50
51    /// Retrieve a user by their identifier
52    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
53    pub async fn retrieve(&self, user_id: &str) -> Result<User, OpenAIError> {
54        self.client
55            .get(
56                format!("/organization/users/{user_id}").as_str(),
57                &self.request_options,
58            )
59            .await
60    }
61
62    /// Deletes a user from the organization.
63    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
64    pub async fn delete(&self, user_id: &str) -> Result<UserDeleteResponse, OpenAIError> {
65        self.client
66            .delete(
67                format!("/organization/users/{user_id}").as_str(),
68                &self.request_options,
69            )
70            .await
71    }
72}