async_openai/
project_user_roles.rs

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