1use std::error::Error;
7
8use hyper::{body, Body, Method, Request};
9use hyper_socket::UnixSocketConnector;
10
11static DOCKERSOCKET : &'static str = "/var/run/docker.sock";
13
14async fn make_http_request(endpoint: String, body_str: String) -> Result<String, Box<dyn Error + Send + Sync>> {
16 let socket = UnixSocketConnector::new(DOCKERSOCKET);
17 let client = hyper::Client::builder()
18 .build::<_, Body>(socket);
19
20 let req = Request::builder()
22 .method(Method::POST)
23 .header("content-type", "application/json")
24 .uri(endpoint)
25 .body(Body::from(body_str))?;
26
27 let mut response_str: String = "".to_string();
28
29 match client.request(req).await {
31 Ok(res) => {
32 let response_bytes = body::to_bytes(res.into_body()).await?;
33 response_str = String::from_utf8(response_bytes.to_vec()).expect("response was not valid utf-8");
34 }
35 Err(err) => eprintln!("shipshape Error: {}", err),
36 }
37
38 Ok(response_str)
39}
40
41
42pub async fn create_container(body_str: String) -> Result<String, Box<dyn Error + Send + Sync>> {
47 make_http_request("http://localhost/containers/create".to_string(), body_str).await
48}
49
50pub async fn start_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
52 make_http_request(format!("http://localhost/containers/{}/start", container_id), "".to_string()).await
53}
54
55pub async fn stop_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
57 make_http_request(format!("http://localhost/containers/{}/stop", container_id), "".to_string()).await
58}
59
60pub async fn pause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
62 make_http_request(format!("http://localhost/containers/{}/pause", container_id), "".to_string()).await
63}
64
65
66pub async fn unpause_container(container_id: String) -> Result<String, Box<dyn Error + Send + Sync>> {
68 make_http_request(format!("http://localhost/containers/{}/unpause", container_id), "".to_string()).await
69}