dynamo_async_openai/
projects.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    Client, ProjectServiceAccounts, ProjectUsers,
15    config::Config,
16    error::OpenAIError,
17    project_api_keys::ProjectAPIKeys,
18    types::{Project, ProjectCreateRequest, ProjectListResponse, ProjectUpdateRequest},
19};
20
21/// Manage the projects within an organization includes creation, updating, and archiving or projects.
22/// The Default project cannot be modified or archived.
23pub struct Projects<'c, C: Config> {
24    client: &'c Client<C>,
25}
26
27impl<'c, C: Config> Projects<'c, C> {
28    pub fn new(client: &'c Client<C>) -> Self {
29        Self { client }
30    }
31
32    // call [ProjectUsers] group APIs
33    pub fn users(&self, project_id: &str) -> ProjectUsers<C> {
34        ProjectUsers::new(self.client, project_id)
35    }
36
37    // call [ProjectServiceAccounts] group APIs
38    pub fn service_accounts(&self, project_id: &str) -> ProjectServiceAccounts<C> {
39        ProjectServiceAccounts::new(self.client, project_id)
40    }
41
42    // call [ProjectAPIKeys] group APIs
43    pub fn api_keys(&self, project_id: &str) -> ProjectAPIKeys<C> {
44        ProjectAPIKeys::new(self.client, project_id)
45    }
46
47    /// Returns a list of projects.
48    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
49    pub async fn list<Q>(&self, query: &Q) -> Result<ProjectListResponse, OpenAIError>
50    where
51        Q: Serialize + ?Sized,
52    {
53        self.client
54            .get_with_query("/organization/projects", &query)
55            .await
56    }
57
58    /// Create a new project in the organization. Projects can be created and archived, but cannot be deleted.
59    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
60    pub async fn create(&self, request: ProjectCreateRequest) -> Result<Project, OpenAIError> {
61        self.client.post("/organization/projects", request).await
62    }
63
64    /// Retrieves a project.
65    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
66    pub async fn retrieve(&self, project_id: String) -> Result<Project, OpenAIError> {
67        self.client
68            .get(format!("/organization/projects/{project_id}").as_str())
69            .await
70    }
71
72    /// Modifies a project in the organization.
73    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
74    pub async fn modify(
75        &self,
76        project_id: String,
77        request: ProjectUpdateRequest,
78    ) -> Result<Project, OpenAIError> {
79        self.client
80            .post(
81                format!("/organization/projects/{project_id}").as_str(),
82                request,
83            )
84            .await
85    }
86
87    /// Archives a project in the organization. Archived projects cannot be used or updated.
88    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
89    pub async fn archive(&self, project_id: String) -> Result<Project, OpenAIError> {
90        self.client
91            .post(
92                format!("/organization/projects/{project_id}/archive").as_str(),
93                (),
94            )
95            .await
96    }
97}