docker_api/api/
system.rs

1use crate::{
2    models,
3    opts::{EventsOpts, SystemDataUsageOpts},
4    Docker, Error, Result,
5};
6use containers_api::url::construct_ep;
7use futures_util::{Stream, TryStreamExt};
8
9use std::{convert::TryFrom, io};
10
11impl Docker {
12    api_doc! { System => Version
13    |
14    /// Returns the version of Docker that is running and various information about the system that Docker is running on.
15    pub async fn version(&self) -> Result<models::SystemVersion> {
16        self.get_json("/version").await
17    }}
18
19    api_doc! { System => Info
20    |
21    /// Returns system information about Docker instance that is running
22    pub async fn info(&self) -> Result<models::SystemInfo> {
23        self.get_json("/info").await
24    }}
25
26    api_doc! { System => Ping
27    |
28    /// This is a dummy endpoint you can use to test if the server is accessible
29    pub async fn ping(&self) -> Result<models::PingInfo> {
30        self.get("/_ping")
31            .await
32            .and_then(|resp| models::PingInfo::try_from(resp.headers()))
33    }}
34
35    api_doc! { System => Events
36    |
37    /// Returns a stream of Docker events
38    pub fn events<'docker>(
39        &'docker self,
40        opts: &EventsOpts,
41    ) -> impl Stream<Item = Result<models::EventMessage>> + Unpin + 'docker {
42        let ep = construct_ep("/events", opts.serialize());
43        let reader = Box::pin(
44            self.get_stream(ep)
45                .map_err(|e| io::Error::new(io::ErrorKind::Other, e)),
46        )
47        .into_async_read();
48
49        Box::pin(
50            asynchronous_codec::FramedRead::new(reader, asynchronous_codec::LinesCodec)
51                .map_err(Error::IO)
52                .and_then(|s: String| async move {
53                    serde_json::from_str(&s).map_err(Error::SerdeJsonError)
54                }),
55        )
56    }}
57
58    api_doc! { System => DataUsage
59    |
60    /// Returns data usage of this Docker instance
61    pub async fn data_usage(&self, opts: &SystemDataUsageOpts) -> Result<models::SystemDataUsage200Response> {
62        let ep = construct_ep("/system/df", opts.serialize());
63        self.get_json(&ep).await
64    }}
65}