async_openai/
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 organization or project level.
12pub struct Roles<'c, C: Config> {
13    client: &'c Client<C>,
14    pub(crate) request_options: RequestOptions,
15}
16
17impl<'c, C: Config> Roles<'c, C> {
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self {
20            client,
21            request_options: RequestOptions::new(),
22        }
23    }
24
25    /// Lists the roles configured for the organization.
26    #[crate::byot(R = serde::de::DeserializeOwned)]
27    pub async fn list(&self) -> Result<PublicRoleListResource, OpenAIError> {
28        self.client
29            .get("/organization/roles", &self.request_options)
30            .await
31    }
32
33    /// Creates a custom role for the organization.
34    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
35    pub async fn create(
36        &self,
37        request: PublicCreateOrganizationRoleBody,
38    ) -> Result<Role, OpenAIError> {
39        self.client
40            .post("/organization/roles", request, &self.request_options)
41            .await
42    }
43
44    /// Updates an existing organization role.
45    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
46    pub async fn update(
47        &self,
48        role_id: &str,
49        request: PublicUpdateOrganizationRoleBody,
50    ) -> Result<Role, OpenAIError> {
51        self.client
52            .post(
53                format!("/organization/roles/{role_id}").as_str(),
54                request,
55                &self.request_options,
56            )
57            .await
58    }
59
60    /// Deletes a custom role from the organization.
61    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
62    pub async fn delete(&self, role_id: &str) -> Result<RoleDeletedResource, OpenAIError> {
63        self.client
64            .delete(
65                format!("/organization/roles/{role_id}").as_str(),
66                &self.request_options,
67            )
68            .await
69    }
70}