docker_engine_api/
request.rs1use hyper::body::Bytes;
2use hyper::{Client as HyperClient, Method, Request, Body, body::HttpBody};
3use hyperlocal::{UnixConnector, Uri};
4use tokio::runtime::Runtime;
5
6pub struct SimpleResponse {
7 pub status: u16,
8 pub body: Bytes,
9}
10
11impl Default for SimpleResponse {
12 fn default() -> Self {
13 SimpleResponse {
14 status: 0,
15 body: Bytes::new(),
16 }
17 }
18}
19
20pub fn request(client: &HyperClient<UnixConnector>, socket: String, url: String, method: Method, body: String, runtime: &Runtime) -> Result<SimpleResponse, Box<dyn std::error::Error + Send + Sync>> {
21 runtime.block_on(async {
22 let uri = Uri::new(&socket, &url);
23 let body = Body::from(body);
24
25 let req = Request::builder()
26 .method(method)
27 .uri(uri)
28 .header("Content-Type", "application/json")
29 .body(body)?
30 .into();
31
32 let mut response = client.request(req).await?;
33 while let Some(next) = response.data().await {
34 let chunk = next?;
35
36 let simple = SimpleResponse {
37 status: response.status().as_u16(),
38 body: chunk,
39 };
40
41 return Ok(simple);
42 }
43
44 Ok(SimpleResponse {
45 status: response.status().as_u16().into(),
46 body: Bytes::new(),
47 })
48 })
49}