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
//! `GET /api/exchanges` and related exchange endpoints.
use crate::api::encode_segment;
use crate::types::exchange::{Exchange, ExchangeDeclareOptions, PublishMessage, PublishResult};
use crate::{Client, Paginated, PaginationQuery, Result};
impl Client {
/// `GET /api/exchanges` — lists all exchanges across all vhosts.
pub async fn list_exchanges(&self) -> Result<Vec<Exchange>> {
self.get("exchanges", None).await
}
/// `GET /api/exchanges` with pagination parameters — returns a
/// [`Paginated`] page of exchanges.
pub async fn list_exchanges_paged(&self, q: &PaginationQuery) -> Result<Paginated<Exchange>> {
self.get("exchanges", Some(q)).await
}
/// `GET /api/exchanges/{vhost}` — lists all exchanges in a single vhost.
/// The vhost is percent-encoded (e.g. `/` becomes `%2F`).
pub async fn list_exchanges_in_vhost(&self, vhost: &str) -> Result<Vec<Exchange>> {
self.get(&format!("exchanges/{}", encode_segment(vhost)), None)
.await
}
/// `GET /api/exchanges/{vhost}/{name}` — returns details of a single
/// exchange. Both segments are percent-encoded.
pub async fn get_exchange(&self, vhost: &str, name: &str) -> Result<Exchange> {
self.get_ctx(
&exchange_path(vhost, name),
None,
&format!("exchange '{name}' in vhost '{vhost}'"),
)
.await
}
/// `PUT /api/exchanges/{vhost}/{name}` — declares an exchange.
pub async fn declare_exchange(
&self,
vhost: &str,
name: &str,
opts: &ExchangeDeclareOptions,
) -> Result<()> {
self.put(&exchange_path(vhost, name), opts).await
}
/// `DELETE /api/exchanges/{vhost}/{name}` — deletes an exchange.
pub async fn delete_exchange(&self, vhost: &str, name: &str) -> Result<()> {
self.delete_ctx(
&exchange_path(vhost, name),
&format!("exchange '{name}' in vhost '{vhost}'"),
)
.await
}
/// `POST /api/exchanges/{vhost}/{name}/publish` — publishes a message
/// to an exchange over the HTTP API. Returns whether the message was
/// routed to at least one queue.
pub async fn publish_to_exchange(
&self,
vhost: &str,
name: &str,
msg: &PublishMessage,
) -> Result<PublishResult> {
self.post_json(&format!("{}/publish", exchange_path(vhost, name)), msg)
.await
}
/// `GET /api/exchanges/{vhost}/{name}/bindings/source` — lists the
/// bindings for which this exchange is the source.
pub async fn list_exchange_bindings_source(
&self,
vhost: &str,
name: &str,
) -> Result<Vec<crate::types::binding::Binding>> {
self.get(
&format!("{}/bindings/source", exchange_path(vhost, name)),
None,
)
.await
}
}
/// Percent-encode a vhost and exchange name into a
/// `exchanges/{vhost}/{name}` relative path segment prefix.
fn exchange_path(vhost: &str, name: &str) -> String {
format!(
"exchanges/{}/{}",
encode_segment(vhost),
encode_segment(name)
)
}