Skip to main content

openai_core/resources/
conversations.rs

1//! Conversation namespace implementations.
2
3use http::Method;
4
5use crate::generated::endpoints;
6
7use super::{
8    Conversation, ConversationItem, ConversationItemsResource, ConversationsResource,
9    DeleteResponse, JsonRequestBuilder, ListRequestBuilder, encode_path_segment,
10};
11
12impl ConversationsResource {
13    /// 创建 conversation。
14    pub fn create(&self) -> JsonRequestBuilder<Conversation> {
15        let endpoint = endpoints::conversations::CONVERSATIONS_CREATE;
16        JsonRequestBuilder::new(
17            self.client.clone(),
18            endpoint.id,
19            Method::POST,
20            endpoint.template,
21        )
22    }
23
24    /// 获取 conversation。
25    pub fn retrieve(&self, conversation_id: impl Into<String>) -> JsonRequestBuilder<Conversation> {
26        let conversation_id = encode_path_segment(conversation_id.into());
27        let endpoint = endpoints::conversations::CONVERSATIONS_RETRIEVE;
28        JsonRequestBuilder::new(
29            self.client.clone(),
30            endpoint.id,
31            Method::GET,
32            endpoint.render(&[("conversation_id", &conversation_id)]),
33        )
34    }
35
36    /// 更新 conversation。
37    pub fn update(&self, conversation_id: impl Into<String>) -> JsonRequestBuilder<Conversation> {
38        let conversation_id = encode_path_segment(conversation_id.into());
39        let endpoint = endpoints::conversations::CONVERSATIONS_UPDATE;
40        JsonRequestBuilder::new(
41            self.client.clone(),
42            endpoint.id,
43            Method::POST,
44            endpoint.render(&[("conversation_id", &conversation_id)]),
45        )
46    }
47
48    /// 删除 conversation。
49    pub fn delete(&self, conversation_id: impl Into<String>) -> JsonRequestBuilder<DeleteResponse> {
50        let conversation_id = encode_path_segment(conversation_id.into());
51        let endpoint = endpoints::conversations::CONVERSATIONS_DELETE;
52        JsonRequestBuilder::new(
53            self.client.clone(),
54            endpoint.id,
55            Method::DELETE,
56            endpoint.render(&[("conversation_id", &conversation_id)]),
57        )
58    }
59
60    /// 返回 items 子资源。
61    pub fn items(&self) -> ConversationItemsResource {
62        ConversationItemsResource::new(self.client.clone())
63    }
64}
65
66impl ConversationItemsResource {
67    /// 创建 conversation item。
68    pub fn create(
69        &self,
70        conversation_id: impl Into<String>,
71    ) -> JsonRequestBuilder<ConversationItem> {
72        let conversation_id = encode_path_segment(conversation_id.into());
73        let endpoint = endpoints::conversations::CONVERSATIONS_ITEMS_CREATE;
74        JsonRequestBuilder::new(
75            self.client.clone(),
76            endpoint.id,
77            Method::POST,
78            endpoint.render(&[("conversation_id", &conversation_id)]),
79        )
80    }
81
82    /// 获取 conversation item。
83    pub fn retrieve(
84        &self,
85        conversation_id: impl Into<String>,
86        item_id: impl Into<String>,
87    ) -> JsonRequestBuilder<ConversationItem> {
88        let conversation_id = encode_path_segment(conversation_id.into());
89        let item_id = encode_path_segment(item_id.into());
90        let endpoint = endpoints::conversations::CONVERSATIONS_ITEMS_RETRIEVE;
91        JsonRequestBuilder::new(
92            self.client.clone(),
93            endpoint.id,
94            Method::GET,
95            endpoint.render(&[("conversation_id", &conversation_id), ("item_id", &item_id)]),
96        )
97    }
98
99    /// 列出 conversation items。
100    pub fn list(&self, conversation_id: impl Into<String>) -> ListRequestBuilder<ConversationItem> {
101        let conversation_id = encode_path_segment(conversation_id.into());
102        let endpoint = endpoints::conversations::CONVERSATIONS_ITEMS_LIST;
103        ListRequestBuilder::new(
104            self.client.clone(),
105            endpoint.id,
106            endpoint.render(&[("conversation_id", &conversation_id)]),
107        )
108    }
109
110    /// 删除 conversation item。
111    pub fn delete(
112        &self,
113        conversation_id: impl Into<String>,
114        item_id: impl Into<String>,
115    ) -> JsonRequestBuilder<DeleteResponse> {
116        let conversation_id = encode_path_segment(conversation_id.into());
117        let item_id = encode_path_segment(item_id.into());
118        let endpoint = endpoints::conversations::CONVERSATIONS_ITEMS_DELETE;
119        JsonRequestBuilder::new(
120            self.client.clone(),
121            endpoint.id,
122            Method::DELETE,
123            endpoint.render(&[("conversation_id", &conversation_id), ("item_id", &item_id)]),
124        )
125    }
126}