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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use crate::api::_generic::{handle_empty_response, handle_response};
use crate::api::binding::RabbitMqBinding;
use crate::errors::RabbitMqClientError;
use crate::RabbitMqClient;
use async_trait::async_trait;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[async_trait]
pub trait QueueApi {
    async fn list_queues(
        &self,
        vhost: Option<String>,
    ) -> Result<Vec<RabbitMqQueue>, RabbitMqClientError>;

    async fn get_queue(
        &self,
        vhost: String,
        name: String,
    ) -> Result<RabbitMqQueue, RabbitMqClientError>;

    async fn get_queue_bindings(
        &self,
        vhost: String,
        name: String,
    ) -> Result<Vec<RabbitMqBinding>, RabbitMqClientError>;

    async fn create_queue(
        &self,
        vhost: String,
        queue: String,
        request: RabbitMqQueueRequest,
    ) -> Result<(), RabbitMqClientError>;

    async fn update_queue(
        &self,
        vhost: String,
        queue: String,
        request: RabbitMqQueueRequest,
    ) -> Result<(), RabbitMqClientError>;

    async fn delete_queue(&self, vhost: String, name: String) -> Result<(), RabbitMqClientError>;

    async fn purge_queue(&self, vhost: String, name: String) -> Result<(), RabbitMqClientError>;

    async fn set_queue_actions(
        &self,
        vhost: String,
        queue: String,
        action: RabbitMqQueueAction,
    ) -> Result<(), RabbitMqClientError>;
}

#[async_trait]
impl QueueApi for RabbitMqClient {
    #[tracing::instrument(skip(self))]
    async fn list_queues(
        &self,
        vhost: Option<String>,
    ) -> Result<Vec<RabbitMqQueue>, RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::GET,
                format!("{}/api/queues/{}", self.api_url, vhost.unwrap_or_default()),
            )
            .send()
            .await?;

        handle_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn get_queue(
        &self,
        vhost: String,
        name: String,
    ) -> Result<RabbitMqQueue, RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::GET,
                format!("{}/api/queues/{}/{}", self.api_url, vhost, name),
            )
            .send()
            .await?;

        handle_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn get_queue_bindings(
        &self,
        vhost: String,
        name: String,
    ) -> Result<Vec<RabbitMqBinding>, RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::GET,
                format!("{}/api/queues/{}/{}/bindings", self.api_url, vhost, name),
            )
            .send()
            .await?;

        handle_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn create_queue(
        &self,
        vhost: String,
        queue: String,
        request: RabbitMqQueueRequest,
    ) -> Result<(), RabbitMqClientError> {
        match self.get_queue(vhost.clone(), queue.clone()).await {
            Ok(_) => Err(RabbitMqClientError::AlreadyExists(format!(
                "{} queue",
                queue
            ))),
            Err(e) => match e {
                RabbitMqClientError::NotFound(_) => self.update_queue(vhost, queue, request).await,
                _ => Err(e),
            },
        }
    }

    #[tracing::instrument(skip(self))]
    async fn update_queue(
        &self,
        vhost: String,
        queue: String,
        request: RabbitMqQueueRequest,
    ) -> Result<(), RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::PUT,
                format!("{}/api/queues/{}/{}", self.api_url, vhost, queue),
            )
            .json(&request)
            .send()
            .await?;

        handle_empty_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn delete_queue(&self, vhost: String, name: String) -> Result<(), RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::DELETE,
                format!("{}/api/queues/{}/{}", self.api_url, vhost, name),
            )
            .send()
            .await?;

        handle_empty_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn purge_queue(&self, vhost: String, name: String) -> Result<(), RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::DELETE,
                format!("{}/api/queues/{}/{}/contents", self.api_url, vhost, name),
            )
            .send()
            .await?;

        handle_empty_response(response).await
    }

    #[tracing::instrument(skip(self))]
    async fn set_queue_actions(
        &self,
        vhost: String,
        queue: String,
        action: RabbitMqQueueAction,
    ) -> Result<(), RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::POST,
                format!("{}/api/queues/{}/{}/actions", self.api_url, vhost, queue),
            )
            .json(&RabbitMqQueueActionRequest { action })
            .send()
            .await?;

        handle_empty_response(response).await
    }
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqQueue {
    pub name: String,
    pub node: String,
    pub arguments: HashMap<String, RabbitMqArgument>,
    pub state: String,
    #[serde(rename = "type")]
    pub kind: String,
    pub vhost: String,
    pub auto_delete: bool,
    pub durable: bool,
    pub exclusive: bool,
    #[serde(default)]
    pub consumer_capacity: Decimal,
    #[serde(default)]
    pub consumer_utilisation: Decimal,
    #[serde(default)]
    pub consumers: i64,
    #[serde(default)]
    pub messages: i64,
    #[serde(default)]
    pub messages_ready: i64,
    #[serde(default)]
    pub messages_unacknowledged: i64,
    pub garbage_collection: Option<RabbitMqQueueGarbageCollection>,
    pub message_stats: Option<RabbitMqQueueMessageStats>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum RabbitMqArgument {
    String(String),
    Decimal(Decimal),
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqQueueMessageStats {
    #[serde(default)]
    pub ack: i64,
    #[serde(default)]
    pub deliver: i64,
    #[serde(default)]
    pub deliver_get: i64,
    #[serde(default)]
    pub deliver_no_ack: i64,
    #[serde(default)]
    pub get: i64,
    #[serde(default)]
    pub get_empty: i64,
    #[serde(default)]
    pub get_no_ack: i64,
    #[serde(default)]
    pub publish: i64,
    #[serde(default)]
    pub redeliver: i64,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqQueueGarbageCollection {
    pub fullsweep_after: i64,
    pub max_heap_size: i64,
    pub min_bin_vheap_size: i64,
    pub min_heap_size: i64,
    pub minor_gcs: i64,
}

#[derive(Debug, Serialize)]
pub struct RabbitMqQueueRequest {
    pub auto_delete: bool,
    pub durable: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct RabbitMqQueueActionRequest {
    pub action: RabbitMqQueueAction,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RabbitMqQueueAction {
    Sync,
    CancelSync,
}