async_openai/admin/
project_roles.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::admin::roles::{
5        PublicCreateOrganizationRoleBody, PublicRoleListResource, PublicUpdateOrganizationRoleBody,
6        Role, RoleDeletedResource,
7    },
8    Client, RequestOptions,
9};
10
11/// Manage custom roles that can be assigned to groups and users at the project level.
12pub struct ProjectRoles<'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> ProjectRoles<'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    /// Lists the roles configured for the project.
28    #[crate::byot(R = serde::de::DeserializeOwned)]
29    pub async fn list(&self) -> Result<PublicRoleListResource, OpenAIError> {
30        self.client
31            .get(
32                format!("/projects/{}/roles", self.project_id).as_str(),
33                &self.request_options,
34            )
35            .await
36    }
37
38    /// Creates a custom role for the project.
39    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
40    pub async fn create(
41        &self,
42        request: PublicCreateOrganizationRoleBody,
43    ) -> Result<Role, OpenAIError> {
44        self.client
45            .post(
46                format!("/projects/{}/roles", self.project_id).as_str(),
47                request,
48                &self.request_options,
49            )
50            .await
51    }
52
53    /// Updates an existing project role.
54    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
55    pub async fn update(
56        &self,
57        role_id: &str,
58        request: PublicUpdateOrganizationRoleBody,
59    ) -> Result<Role, OpenAIError> {
60        self.client
61            .post(
62                format!("/projects/{}/roles/{}", self.project_id, role_id).as_str(),
63                request,
64                &self.request_options,
65            )
66            .await
67    }
68
69    /// Deletes a custom role from the project.
70    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
71    pub async fn delete(&self, role_id: &str) -> Result<RoleDeletedResource, OpenAIError> {
72        self.client
73            .delete(
74                format!("/projects/{}/roles/{}", self.project_id, role_id).as_str(),
75                &self.request_options,
76            )
77            .await
78    }
79}