1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use ntex::channel::mpsc::Receiver;
use nanocl_error::http::HttpResult;
use nanocl_error::http_client::HttpClientResult;
use nanocl_stubs::system::{BinaryInfo, Event, EventCondition, HostInfo};
use super::http_client::NanocldClient;
impl NanocldClient {
/// Get the version of the daemon
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.get_version().await;
/// ```
pub async fn get_version(&self) -> HttpClientResult<BinaryInfo> {
let res = self.send_get("/version", None::<String>).await?;
Self::res_json(res).await
}
/// Watch daemon events
/// It will emit an event when the daemon state change
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let mut stream = client.watch_events(None).await?;
/// while let Some(event) = stream.next().await {
/// println!("{:?}", event);
/// }
/// ```
pub async fn watch_events(
&self,
conditions: Option<Vec<EventCondition>>,
) -> HttpClientResult<Receiver<HttpResult<Event>>> {
let res = self
.send_post("/events/watch", conditions, None::<String>)
.await?;
Ok(Self::res_stream(res).await)
}
/// Check if the daemon is running
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.ping().await;
/// ```
pub async fn ping(&self) -> HttpClientResult<()> {
self.send_head("/_ping", None::<String>).await?;
Ok(())
}
/// Get details about the host and docker daemon
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let info = client.info().await.unwrap();
/// ```
pub async fn info(&self) -> HttpClientResult<HostInfo> {
let res = self.send_get("/info", None::<String>).await?;
Self::res_json(res).await
}
}
#[cfg(test)]
mod tests {
use crate::ConnectOpts;
use super::*;
#[ntex::test]
async fn get_version() {
let client = NanocldClient::connect_to(&ConnectOpts {
url: "http://nanocl.internal:8585".into(),
..Default::default()
})
.expect("Failed to create a nanocl client");
let version = client.get_version().await;
assert!(version.is_ok());
}
#[ntex::test]
async fn watch_events() {
let client = NanocldClient::connect_to(&ConnectOpts {
url: "http://nanocl.internal:8585".into(),
..Default::default()
})
.expect("Failed to create a nanocl client");
let _stream = client.watch_events(None).await.unwrap();
// Todo : find a way to test this on CI because it's limited to 2 threads
// let _event = stream.next().await.unwrap();
}
#[ntex::test]
async fn info() {
let client = NanocldClient::connect_to(&ConnectOpts {
url: "http://nanocl.internal:8585".into(),
..Default::default()
})
.expect("Failed to create a nanocl client");
let info = client.info().await.unwrap();
assert!(info.docker.containers.unwrap() > 0);
}
}