Skip to main content

openai_core/resources/
containers.rs

1//! Container namespace implementations.
2
3use http::Method;
4
5use crate::generated::endpoints;
6
7use super::{
8    BytesRequestBuilder, Container, ContainerFile, ContainerFilesContentResource,
9    ContainerFilesResource, ContainersResource, DeleteResponse, JsonRequestBuilder,
10    ListRequestBuilder, encode_path_segment,
11};
12
13impl ContainersResource {
14    /// 创建 container。
15    pub fn create(&self) -> JsonRequestBuilder<Container> {
16        let endpoint = endpoints::containers::CONTAINERS_CREATE;
17        JsonRequestBuilder::new(
18            self.client.clone(),
19            endpoint.id,
20            Method::POST,
21            endpoint.template,
22        )
23    }
24
25    /// 获取 container。
26    pub fn retrieve(&self, container_id: impl Into<String>) -> JsonRequestBuilder<Container> {
27        let container_id = encode_path_segment(container_id.into());
28        let endpoint = endpoints::containers::CONTAINERS_RETRIEVE;
29        JsonRequestBuilder::new(
30            self.client.clone(),
31            endpoint.id,
32            Method::GET,
33            endpoint.render(&[("container_id", &container_id)]),
34        )
35    }
36
37    /// 列出 containers。
38    pub fn list(&self) -> ListRequestBuilder<Container> {
39        let endpoint = endpoints::containers::CONTAINERS_LIST;
40        ListRequestBuilder::new(self.client.clone(), endpoint.id, endpoint.template)
41    }
42
43    /// 删除 container。
44    pub fn delete(&self, container_id: impl Into<String>) -> JsonRequestBuilder<DeleteResponse> {
45        let container_id = encode_path_segment(container_id.into());
46        let endpoint = endpoints::containers::CONTAINERS_DELETE;
47        JsonRequestBuilder::new(
48            self.client.clone(),
49            endpoint.id,
50            Method::DELETE,
51            endpoint.render(&[("container_id", &container_id)]),
52        )
53    }
54
55    /// 返回 files 子资源。
56    pub fn files(&self) -> ContainerFilesResource {
57        ContainerFilesResource::new(self.client.clone())
58    }
59}
60
61impl ContainerFilesResource {
62    /// 创建 container file。
63    pub fn create(&self, container_id: impl Into<String>) -> JsonRequestBuilder<ContainerFile> {
64        let container_id = encode_path_segment(container_id.into());
65        let endpoint = endpoints::containers::CONTAINERS_FILES_CREATE;
66        JsonRequestBuilder::new(
67            self.client.clone(),
68            endpoint.id,
69            Method::POST,
70            endpoint.render(&[("container_id", &container_id)]),
71        )
72    }
73
74    /// 获取 container file。
75    pub fn retrieve(
76        &self,
77        container_id: impl Into<String>,
78        file_id: impl Into<String>,
79    ) -> JsonRequestBuilder<ContainerFile> {
80        let container_id = encode_path_segment(container_id.into());
81        let file_id = encode_path_segment(file_id.into());
82        let endpoint = endpoints::containers::CONTAINERS_FILES_RETRIEVE;
83        JsonRequestBuilder::new(
84            self.client.clone(),
85            endpoint.id,
86            Method::GET,
87            endpoint.render(&[("container_id", &container_id), ("file_id", &file_id)]),
88        )
89    }
90
91    /// 列出 container files。
92    pub fn list(&self, container_id: impl Into<String>) -> ListRequestBuilder<ContainerFile> {
93        let container_id = encode_path_segment(container_id.into());
94        let endpoint = endpoints::containers::CONTAINERS_FILES_LIST;
95        ListRequestBuilder::new(
96            self.client.clone(),
97            endpoint.id,
98            endpoint.render(&[("container_id", &container_id)]),
99        )
100    }
101
102    /// 删除 container file。
103    pub fn delete(
104        &self,
105        container_id: impl Into<String>,
106        file_id: impl Into<String>,
107    ) -> JsonRequestBuilder<DeleteResponse> {
108        let container_id = encode_path_segment(container_id.into());
109        let file_id = encode_path_segment(file_id.into());
110        let endpoint = endpoints::containers::CONTAINERS_FILES_DELETE;
111        JsonRequestBuilder::new(
112            self.client.clone(),
113            endpoint.id,
114            Method::DELETE,
115            endpoint.render(&[("container_id", &container_id), ("file_id", &file_id)]),
116        )
117    }
118
119    /// 返回 content 子资源。
120    pub fn content(&self) -> ContainerFilesContentResource {
121        ContainerFilesContentResource::new(self.client.clone())
122    }
123}
124
125impl ContainerFilesContentResource {
126    /// 获取 container file 内容。
127    pub fn retrieve(
128        &self,
129        container_id: impl Into<String>,
130        file_id: impl Into<String>,
131    ) -> BytesRequestBuilder {
132        let container_id = encode_path_segment(container_id.into());
133        let file_id = encode_path_segment(file_id.into());
134        let endpoint = endpoints::containers::CONTAINERS_FILES_CONTENT_RETRIEVE;
135        BytesRequestBuilder::new(
136            self.client.clone(),
137            endpoint.id,
138            Method::GET,
139            endpoint.render(&[("container_id", &container_id), ("file_id", &file_id)]),
140        )
141    }
142}