Skip to main content

async_openai/admin/
project_user_roles.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::{
5        groups::PublicAssignOrganizationGroupRoleBody,
6        roles::{AssignedRoleDetails, DeletedRoleAssignmentResource, RoleListResource},
7        users::UserRoleAssignment,
8    },
9    Client, RequestOptions,
10};
11
12/// Manage role assignments for users in a project.
13pub struct ProjectUserRoles<'c, C: Config> {
14    client: &'c Client<C>,
15    pub project_id: String,
16    pub user_id: String,
17    pub(crate) request_options: RequestOptions,
18}
19
20impl<'c, C: Config> ProjectUserRoles<'c, C> {
21    pub fn new(client: &'c Client<C>, project_id: &str, user_id: &str) -> Self {
22        Self {
23            client,
24            project_id: project_id.into(),
25            user_id: user_id.into(),
26            request_options: RequestOptions::new(),
27        }
28    }
29
30    /// Lists all role assignments for a user in the project.
31    #[crate::byot(R = serde::de::DeserializeOwned)]
32    pub async fn list(&self) -> Result<RoleListResource, OpenAIError> {
33        self.client
34            .get(
35                format!("/projects/{}/users/{}/roles", self.project_id, self.user_id).as_str(),
36                &self.request_options,
37            )
38            .await
39    }
40
41    /// Assigns a role to a user in the project.
42    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
43    pub async fn assign(
44        &self,
45        request: PublicAssignOrganizationGroupRoleBody,
46    ) -> Result<UserRoleAssignment, OpenAIError> {
47        self.client
48            .post(
49                format!("/projects/{}/users/{}/roles", self.project_id, self.user_id).as_str(),
50                request,
51                &self.request_options,
52            )
53            .await
54    }
55
56    /// Unassigns a role from a user in the project.
57    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
58    pub async fn unassign(
59        &self,
60        role_id: &str,
61    ) -> Result<DeletedRoleAssignmentResource, OpenAIError> {
62        self.client
63            .delete(
64                format!(
65                    "/projects/{}/users/{}/roles/{}",
66                    self.project_id, self.user_id, role_id
67                )
68                .as_str(),
69                &self.request_options,
70            )
71            .await
72    }
73
74    /// Retrieves a project role assigned to a user.
75    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
76    pub async fn retrieve(&self, role_id: &str) -> Result<AssignedRoleDetails, OpenAIError> {
77        self.client
78            .get(
79                &format!(
80                    "/projects/{project_id}/users/{user_id}/roles/{role_id}",
81                    project_id = self.project_id,
82                    user_id = self.user_id
83                ),
84                &self.request_options,
85            )
86            .await
87    }
88}