Skip to main content

opencode_sdk/http/
pty.rs

1//! PTY API for OpenCode.
2//!
3//! Endpoints for pseudo-terminal management.
4
5use crate::error::Result;
6use crate::http::HttpClient;
7use crate::types::pty::{CreatePtyRequest, Pty, UpdatePtyRequest};
8use reqwest::Method;
9
10/// PTY API client.
11#[derive(Clone)]
12pub struct PtyApi {
13    http: HttpClient,
14}
15
16impl PtyApi {
17    /// Create a new PTY API client.
18    pub fn new(http: HttpClient) -> Self {
19        Self { http }
20    }
21
22    /// List PTYs.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the request fails.
27    pub async fn list(&self) -> Result<Vec<Pty>> {
28        self.http.request_json(Method::GET, "/pty", None).await
29    }
30
31    /// Create a new PTY.
32    ///
33    /// # Errors
34    ///
35    /// Returns an error if the request fails.
36    pub async fn create(&self, req: &CreatePtyRequest) -> Result<Pty> {
37        let body = serde_json::to_value(req)?;
38        self.http
39            .request_json(Method::POST, "/pty", Some(body))
40            .await
41    }
42
43    /// Get a PTY by ID.
44    ///
45    /// # Errors
46    ///
47    /// Returns an error if the request fails.
48    pub async fn get(&self, id: &str) -> Result<Pty> {
49        self.http
50            .request_json(Method::GET, &format!("/pty/{}", id), None)
51            .await
52    }
53
54    /// Update a PTY.
55    ///
56    /// # Errors
57    ///
58    /// Returns an error if the request fails.
59    pub async fn update(&self, id: &str, req: &UpdatePtyRequest) -> Result<Pty> {
60        let body = serde_json::to_value(req)?;
61        self.http
62            .request_json(Method::PUT, &format!("/pty/{}", id), Some(body))
63            .await
64    }
65
66    /// Delete a PTY.
67    ///
68    /// # Errors
69    ///
70    /// Returns an error if the request fails.
71    pub async fn delete(&self, id: &str) -> Result<()> {
72        self.http
73            .request_empty(Method::DELETE, &format!("/pty/{}", id), None)
74            .await
75    }
76}