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::{Container, CreateContainerRequest};
9
10/// Service for managing containers.
11pub struct ContainerService {
12    client: Arc<ClientInner>,
13}
14
15impl ContainerService {
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.containers().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<Container>> {
36        self.client.request(Method::GET, "/api/containers").await
37    }
38
39    /// Get a container by ID.
40    ///
41    /// # Arguments
42    ///
43    /// * `id` - Container ID
44    pub async fn get(&self, id: &str) -> Result<Container> {
45        self.client
46            .request(Method::GET, &format!("/api/containers/{}", id))
47            .await
48    }
49
50    /// Create a new container.
51    ///
52    /// # Example
53    ///
54    /// ```rust,no_run
55    /// # use rexec::{RexecClient, CreateContainerRequest};
56    /// # async fn example() -> Result<(), rexec::Error> {
57    /// let client = RexecClient::new("https://example.com", "token");
58    /// let container = client.containers()
59    ///     .create(CreateContainerRequest::new("ubuntu:24.04")
60    ///         .name("my-sandbox")
61    ///         .env("MY_VAR", "value"))
62    ///     .await?;
63    /// # Ok(())
64    /// # }
65    /// ```
66    pub async fn create(&self, request: CreateContainerRequest) -> Result<Container> {
67        self.client
68            .request_with_body(Method::POST, "/api/containers", &request)
69            .await
70    }
71
72    /// Delete a container.
73    ///
74    /// # Arguments
75    ///
76    /// * `id` - Container ID to delete
77    pub async fn delete(&self, id: &str) -> Result<()> {
78        self.client
79            .request_empty(Method::DELETE, &format!("/api/containers/{}", id))
80            .await
81    }
82
83    /// Start a stopped container.
84    ///
85    /// # Arguments
86    ///
87    /// * `id` - Container ID to start
88    pub async fn start(&self, id: &str) -> Result<()> {
89        self.client
90            .request_empty(Method::POST, &format!("/api/containers/{}/start", id))
91            .await
92    }
93
94    /// Stop a running container.
95    ///
96    /// # Arguments
97    ///
98    /// * `id` - Container ID to stop
99    pub async fn stop(&self, id: &str) -> Result<()> {
100        self.client
101            .request_empty(Method::POST, &format!("/api/containers/{}/stop", id))
102            .await
103    }
104}