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
//! `GET /api/connections` and related connection endpoints.
use crate::api::{encode_segment, nf};
use crate::types::channel::Channel;
use crate::types::connection::Connection;
use crate::{Client, Paginated, PaginationQuery, Result};
impl Client {
/// `GET /api/connections` — lists all connections to the cluster.
pub async fn list_connections(&self) -> Result<Vec<Connection>> {
self.get("connections", None).await
}
/// `GET /api/connections` with pagination parameters — returns a
/// [`Paginated`] page of connections.
pub async fn list_connections_paged(
&self,
q: &PaginationQuery,
) -> Result<Paginated<Connection>> {
self.get("connections", Some(q)).await
}
/// `GET /api/connections/{name}` — returns a single connection. The
/// name is percent-encoded (connection names commonly contain spaces,
/// colons, and `->`, e.g. `"127.0.0.1:52341 -> 127.0.0.1:5672"`).
pub async fn get_connection(&self, name: &str) -> Result<Connection> {
self.get_ctx(
&format!("connections/{}", encode_segment(name)),
None,
&format!("connection '{name}'"),
)
.await
}
/// `DELETE /api/connections/{name}` — closes a connection. When
/// `reason` is `Some`, it is sent to the server in the `X-Reason`
/// request header (recorded in the server log). The name is
/// percent-encoded.
pub async fn close_connection(&self, name: &str, reason: Option<&str>) -> Result<()> {
let headers: &[(&str, &str)] = match reason {
Some(r) => &[("X-Reason", r)],
None => &[],
};
self.delete_with_headers(&format!("connections/{}", encode_segment(name)), headers)
.await
.map_err(|e| nf(e, &format!("connection '{name}'")))
}
/// `GET /api/connections/{name}/channels` — lists all channels open
/// on a single connection. The name is percent-encoded.
pub async fn list_connection_channels(&self, name: &str) -> Result<Vec<Channel>> {
self.get(
&format!("connections/{}/channels", encode_segment(name)),
None,
)
.await
}
}