Skip to main content

rexec/
containers.rs

1//! Container service for managing sandboxed environments.
2
3use reqwest::Method;
4use std::sync::Arc;
5
6use crate::client::ClientInner;
7use crate::error::Result;
8use crate::types::{CreateSandboxRequest, Sandbox};
9
10/// Service for managing sandboxes.
11pub struct SandboxService {
12    client: Arc<ClientInner>,
13}
14
15impl SandboxService {
16    pub(crate) fn new(client: Arc<ClientInner>) -> Self {
17        Self { client }
18    }
19
20    /// List all containers for the authenticated user.
21    ///
22    /// # Example
23    ///
24    /// ```rust,no_run
25    /// # use rexec::RexecClient;
26    /// # async fn example() -> Result<(), rexec::Error> {
27    /// let client = RexecClient::new("https://example.com", "token");
28    /// let containers = client.sandboxes().list().await?;
29    /// for c in containers {
30    ///     println!("{}: {}", c.name, c.status);
31    /// }
32    /// # Ok(())
33    /// # }
34    /// ```
35    pub async fn list(&self) -> Result<Vec<Sandbox>> {
36        // Production API: { "containers": [...], "count": N, "limit": M }
37        // `containers` may be null when empty.
38        #[derive(serde::Deserialize)]
39        struct ListResponse {
40            #[serde(default)]
41            containers: Option<Vec<Sandbox>>,
42        }
43        let resp: ListResponse = self.client.request(Method::GET, "/api/containers").await?;
44        Ok(resp.containers.unwrap_or_default())
45    }
46
47    /// Get a container by ID.
48    ///
49    /// # Arguments
50    ///
51    /// * `id` - Container ID
52    pub async fn get(&self, id: &str) -> Result<Sandbox> {
53        self.client
54            .request(Method::GET, &format!("/api/containers/{}", id))
55            .await
56    }
57
58    /// Create a new container.
59    ///
60    /// # Example
61    ///
62    /// ```rust,no_run
63    /// # use rexec::{RexecClient, CreateSandboxRequest};
64    /// # async fn example() -> Result<(), rexec::Error> {
65    /// let client = RexecClient::new("https://example.com", "token");
66    /// let container = client.sandboxes()
67    ///     .create(CreateSandboxRequest::new("ubuntu")
68    ///         .name("my-sandbox")
69    ///         .env("MY_VAR", "value"))
70    ///     .await?;
71    /// # Ok(())
72    /// # }
73    /// ```
74    pub async fn create(&self, request: CreateSandboxRequest) -> Result<Sandbox> {
75        self.client
76            .request_with_body(Method::POST, "/api/containers", &request)
77            .await
78    }
79
80    /// Delete a container.
81    ///
82    /// # Arguments
83    ///
84    /// * `id` - Container ID to delete
85    pub async fn delete(&self, id: &str) -> Result<()> {
86        self.client
87            .request_empty(Method::DELETE, &format!("/api/containers/{}", id))
88            .await
89    }
90
91    /// Start a stopped container.
92    ///
93    /// # Arguments
94    ///
95    /// * `id` - Container ID to start
96    pub async fn start(&self, id: &str) -> Result<()> {
97        self.client
98            .request_empty(Method::POST, &format!("/api/containers/{}/start", id))
99            .await
100    }
101
102    /// Stop a running container.
103    ///
104    /// # Arguments
105    ///
106    /// * `id` - Container ID to stop
107    pub async fn stop(&self, id: &str) -> Result<()> {
108        self.client
109            .request_empty(Method::POST, &format!("/api/containers/{}/stop", id))
110            .await
111    }
112}
113
114/// Deprecated alias for [`SandboxService`].
115#[deprecated(since = "1.1.0", note = "use SandboxService")]
116pub type ContainerService = SandboxService;