dynamo_async_openai/
project_users.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use serde::Serialize;
12
13use crate::{
14    config::Config,
15    error::OpenAIError,
16    types::{
17        ProjectUser, ProjectUserCreateRequest, ProjectUserDeleteResponse, ProjectUserListResponse,
18        ProjectUserUpdateRequest,
19    },
20    Client,
21};
22
23/// Manage users within a project, including adding, updating roles, and removing users.
24/// Users cannot be removed from the Default project, unless they are being removed from the organization.
25pub struct ProjectUsers<'c, C: Config> {
26    client: &'c Client<C>,
27    pub project_id: String,
28}
29
30impl<'c, C: Config> ProjectUsers<'c, C> {
31    pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
32        Self {
33            client,
34            project_id: project_id.into(),
35        }
36    }
37
38    /// Returns a list of users in the project.
39    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
40    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectUserListResponse, OpenAIError>
41    where
42        Q: Serialize + ?Sized,
43    {
44        self.client
45            .get_with_query(
46                format!("/organization/projects/{}/users", self.project_id).as_str(),
47                &query,
48            )
49            .await
50    }
51
52    /// Adds a user to the project. Users must already be members of the organization to be added to a project.
53    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
54    pub async fn create(
55        &self,
56        request: ProjectUserCreateRequest,
57    ) -> Result<ProjectUser, OpenAIError> {
58        self.client
59            .post(
60                format!("/organization/projects/{}/users", self.project_id).as_str(),
61                request,
62            )
63            .await
64    }
65
66    /// Retrieves a user in the project.
67    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
68    pub async fn retrieve(&self, user_id: &str) -> Result<ProjectUser, OpenAIError> {
69        self.client
70            .get(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str())
71            .await
72    }
73
74    /// Modifies a user's role in the project.
75    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
76    pub async fn modify(
77        &self,
78        user_id: &str,
79        request: ProjectUserUpdateRequest,
80    ) -> Result<ProjectUser, OpenAIError> {
81        self.client
82            .post(
83                format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str(),
84                request,
85            )
86            .await
87    }
88
89    /// Deletes a user from the project.
90    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
91    pub async fn delete(&self, user_id: &str) -> Result<ProjectUserDeleteResponse, OpenAIError> {
92        self.client
93            .delete(format!("/organization/projects/{}/users/{user_id}", self.project_id).as_str())
94            .await
95    }
96}