async_openai/
users.rs

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