portkey_sdk/service/
threads.rs

1use std::future::Future;
2
3use crate::model::{CreateThreadRequest, DeleteThreadResponse, ModifyThreadRequest, Thread};
4use crate::{PortkeyClient, Result};
5
6/// Service for managing threads.
7///
8/// # Example
9///
10/// ```no_run
11/// use portkey_sdk::{PortkeyConfig, PortkeyClient, Result};
12/// use portkey_sdk::service::ThreadsService;
13/// use portkey_sdk::model::CreateThreadRequest;
14///
15/// # async fn example() -> Result<()> {
16/// let config = PortkeyConfig::builder()
17///     .with_api_key("your-api-key")
18///     .build()?;
19/// let client = PortkeyClient::new(config)?;
20///
21///     let thread = client.create_thread(
22///         CreateThreadRequest::default()
23///     ).await?;
24///
25///     println!("Created thread: {}", thread.id);
26///     Ok(())
27/// # }
28/// ```
29pub trait ThreadsService {
30    /// Create a thread.
31    fn create_thread(&self, request: CreateThreadRequest) -> impl Future<Output = Result<Thread>>;
32
33    /// Retrieves a thread.
34    fn retrieve_thread(&self, thread_id: &str) -> impl Future<Output = Result<Thread>>;
35
36    /// Modifies a thread.
37    fn modify_thread(
38        &self,
39        thread_id: &str,
40        request: ModifyThreadRequest,
41    ) -> impl Future<Output = Result<Thread>>;
42
43    /// Delete a thread.
44    fn delete_thread(&self, thread_id: &str) -> impl Future<Output = Result<DeleteThreadResponse>>;
45}
46
47impl ThreadsService for PortkeyClient {
48    async fn create_thread(&self, request: CreateThreadRequest) -> Result<Thread> {
49        #[cfg(feature = "tracing")]
50        tracing::debug!(
51            target: crate::TRACING_TARGET_SERVICE,
52            "Creating thread"
53        );
54
55        let response = self
56            .send_json(reqwest::Method::POST, "/threads", &request)
57            .await?;
58        let response = response.error_for_status()?;
59        let thread: Thread = response.json().await?;
60
61        #[cfg(feature = "tracing")]
62        tracing::debug!(
63            target: crate::TRACING_TARGET_SERVICE,
64            "Thread created successfully"
65        );
66
67        Ok(thread)
68    }
69
70    async fn retrieve_thread(&self, thread_id: &str) -> Result<Thread> {
71        #[cfg(feature = "tracing")]
72        tracing::debug!(
73            target: crate::TRACING_TARGET_SERVICE,
74            thread_id = %thread_id,
75            "Retrieving thread"
76        );
77
78        let response = self
79            .send(reqwest::Method::GET, &format!("/threads/{}", thread_id))
80            .await?;
81        let response = response.error_for_status()?;
82        let thread: Thread = response.json().await?;
83
84        #[cfg(feature = "tracing")]
85        tracing::debug!(
86            target: crate::TRACING_TARGET_SERVICE,
87            "Thread retrieved successfully"
88        );
89
90        Ok(thread)
91    }
92
93    async fn modify_thread(&self, thread_id: &str, request: ModifyThreadRequest) -> Result<Thread> {
94        #[cfg(feature = "tracing")]
95        tracing::debug!(
96            target: crate::TRACING_TARGET_SERVICE,
97            thread_id = %thread_id,
98            "Modifying thread"
99        );
100
101        let response = self
102            .send_json(
103                reqwest::Method::POST,
104                &format!("/threads/{}", thread_id),
105                &request,
106            )
107            .await?;
108        let response = response.error_for_status()?;
109        let thread: Thread = response.json().await?;
110
111        #[cfg(feature = "tracing")]
112        tracing::debug!(
113            target: crate::TRACING_TARGET_SERVICE,
114            "Thread modified successfully"
115        );
116
117        Ok(thread)
118    }
119
120    async fn delete_thread(&self, thread_id: &str) -> Result<DeleteThreadResponse> {
121        #[cfg(feature = "tracing")]
122        tracing::debug!(
123            target: crate::TRACING_TARGET_SERVICE,
124            thread_id = %thread_id,
125            "Deleting thread"
126        );
127
128        let response = self
129            .send(reqwest::Method::DELETE, &format!("/threads/{}", thread_id))
130            .await?;
131        let response = response.error_for_status()?;
132        let delete_response: DeleteThreadResponse = response.json().await?;
133
134        #[cfg(feature = "tracing")]
135        tracing::debug!(
136            target: crate::TRACING_TARGET_SERVICE,
137            "Thread deleted successfully"
138        );
139
140        Ok(delete_response)
141    }
142}