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
// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{commons::PaginationParams, path, requests::ExchangeParams, responses};
use super::client::{Client, Result};
use std::fmt::Display;
impl<E, U, P> Client<E, U, P>
where
E: Display,
U: Display,
P: Display,
{
/// Lists all exchanges across the cluster.
/// See [Exchanges Guide](https://www.rabbitmq.com/docs/exchanges) to learn more.
///
/// Requires the `management` user tag and have `read` permissions on the exchanges. Does not modify state.
pub fn list_exchanges(&self) -> Result<Vec<responses::ExchangeInfo>> {
self.get_api_request("exchanges")
}
/// Lists exchanges with pagination.
///
/// Requires the `management` user tag and have `read` permissions on the exchanges. Does not modify state.
pub fn list_exchanges_paged(
&self,
params: &PaginationParams,
) -> Result<Vec<responses::ExchangeInfo>> {
match params.to_query_string() {
Some(query) => self.get_paginated_api_request("exchanges", &query),
None => self.list_exchanges(),
}
}
/// Lists all exchanges in the given virtual host.
/// See [Exchanges Guide](https://www.rabbitmq.com/docs/exchanges) to learn more.
///
/// Requires the `management` user tag and have `read` permissions on the exchanges. Does not modify state.
pub fn list_exchanges_in(&self, virtual_host: &str) -> Result<Vec<responses::ExchangeInfo>> {
self.get_api_request(path!("exchanges", virtual_host))
}
/// Lists exchanges in the given virtual host with pagination.
///
/// Requires the `management` user tag and have `read` permissions on the exchanges. Does not modify state.
pub fn list_exchanges_in_paged(
&self,
virtual_host: &str,
params: &PaginationParams,
) -> Result<Vec<responses::ExchangeInfo>> {
match params.to_query_string() {
Some(query) => self.get_paginated_api_request(path!("exchanges", virtual_host), &query),
None => self.list_exchanges_in(virtual_host),
}
}
/// Returns information about an exchange.
/// See [Exchanges Guide](https://www.rabbitmq.com/docs/exchanges) to learn more.
///
/// Requires the `management` user tag and have `read` permissions on the exchange. Does not modify state.
pub fn get_exchange_info(
&self,
virtual_host: &str,
name: &str,
) -> Result<responses::ExchangeInfo> {
let response = self.http_get(path!("exchanges", virtual_host, name), None, None)?;
let response = response.json()?;
Ok(response)
}
/// Declares an [exchange](https://www.rabbitmq.com/docs/exchanges).
///
/// If the exchange already exists with different parameters, this operation may fail
/// unless the parameters are equivalent.
///
/// Requires the `management` user tag and have `configure` permissions on the exchange.
pub fn declare_exchange(&self, vhost: &str, params: &ExchangeParams<'_>) -> Result<()> {
self.put_api_request(path!("exchanges", vhost, params.name), params)
}
/// Deletes an exchange in a specified virtual host.
///
/// Unless `idempotently` is set to `true`, an attempt to delete a non-existent exchange
/// will fail.
///
/// Requires the `management` user tag and have `configure` permissions on the exchange.
pub fn delete_exchange(&self, vhost: &str, name: &str, idempotently: bool) -> Result<()> {
self.delete_api_request_with_optional_not_found(
path!("exchanges", vhost, name),
idempotently,
)
}
/// Deletes multiple exchanges in a specified virtual host.
///
/// When `idempotently` is true, non-existent exchanges are silently skipped.
/// When `idempotently` is false, the operation fails on the first non-existent exchange.
///
/// Requires the `management` user tag and have `configure` permissions on the exchanges.
pub fn delete_exchanges(&self, vhost: &str, names: &[&str], idempotently: bool) -> Result<()> {
for name in names {
self.delete_exchange(vhost, name, idempotently)?;
}
Ok(())
}
}