-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
exchange.py
187 lines (157 loc) · 6.9 KB
/
exchange.py
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
from amqpstorm.compatibility import json
from amqpstorm.compatibility import quote
from amqpstorm.management.base import ManagementHandler
API_EXCHANGE = 'exchanges/%s/%s'
API_EXCHANGES = 'exchanges'
API_EXCHANGES_VIRTUAL_HOST = 'exchanges/%s'
API_EXCHANGE_BINDINGS = 'exchanges/%s/%s/bindings/source'
API_EXCHANGE_BIND = 'bindings/%s/e/%s/e/%s'
API_EXCHANGE_UNBIND = 'bindings/%s/e/%s/e/%s/%s'
class Exchange(ManagementHandler):
def get(self, exchange, virtual_host='/'):
"""Get Exchange details.
:param str exchange: Exchange name
:param str virtual_host: Virtual host name
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: dict
"""
virtual_host = quote(virtual_host, '')
return self.http_client.get(
API_EXCHANGE
% (
virtual_host,
exchange)
)
def list(self, virtual_host='/', show_all=False):
"""List Exchanges.
:param str virtual_host: Virtual host name
:param bool show_all: List all Exchanges
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: list
"""
if show_all:
return self.http_client.get(API_EXCHANGES)
virtual_host = quote(virtual_host, '')
return self.http_client.get(
API_EXCHANGES_VIRTUAL_HOST % virtual_host
)
def declare(self, exchange='', exchange_type='direct', virtual_host='/',
passive=False, durable=False, auto_delete=False,
internal=False, arguments=None):
"""Declare an Exchange.
:param str exchange: Exchange name
:param str exchange_type: Exchange type
:param str virtual_host: Virtual host name
:param bool passive: Do not create
:param bool durable: Durable exchange
:param bool auto_delete: Automatically delete when not in use
:param bool internal: Is the exchange for use by the broker only.
:param dict|None arguments: Exchange key/value arguments
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: None
"""
if passive:
return self.get(exchange, virtual_host=virtual_host)
exchange_payload = json.dumps(
{
'durable': durable,
'auto_delete': auto_delete,
'internal': internal,
'type': exchange_type,
'arguments': arguments or {},
'vhost': virtual_host
}
)
return self.http_client.put(API_EXCHANGE %
(
quote(virtual_host, ''),
exchange
),
payload=exchange_payload)
def delete(self, exchange, virtual_host='/'):
"""Delete an Exchange.
:param str exchange: Exchange name
:param str virtual_host: Virtual host name
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: dict
"""
virtual_host = quote(virtual_host, '')
return self.http_client.delete(API_EXCHANGE %
(
virtual_host,
exchange
))
def bindings(self, exchange, virtual_host='/'):
"""Get Exchange bindings.
:param str exchange: Exchange name
:param str virtual_host: Virtual host name
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: list
"""
virtual_host = quote(virtual_host, '')
return self.http_client.get(API_EXCHANGE_BINDINGS %
(
virtual_host,
exchange
))
def bind(self, destination='', source='', routing_key='', virtual_host='/',
arguments=None):
"""Bind an Exchange.
:param str source: Source Exchange name
:param str destination: Destination Exchange name
:param str routing_key: The routing key to use
:param str virtual_host: Virtual host name
:param dict|None arguments: Bind key/value arguments
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: None
"""
bind_payload = json.dumps({
'destination': destination,
'destination_type': 'e',
'routing_key': routing_key,
'source': source,
'arguments': arguments or {},
'vhost': virtual_host
})
virtual_host = quote(virtual_host, '')
return self.http_client.post(API_EXCHANGE_BIND %
(
virtual_host,
source,
destination
),
payload=bind_payload)
def unbind(self, destination='', source='', routing_key='',
virtual_host='/', properties_key=None):
"""Unbind an Exchange.
:param str source: Source Exchange name
:param str destination: Destination Exchange name
:param str routing_key: The routing key to use
:param str virtual_host: Virtual host name
:param str properties_key:
:raises ApiError: Raises if the remote server encountered an error.
:raises ApiConnectionError: Raises if there was a connectivity issue.
:rtype: None
"""
unbind_payload = json.dumps({
'destination': destination,
'destination_type': 'e',
'properties_key': properties_key or routing_key,
'source': source,
'vhost': virtual_host
})
virtual_host = quote(virtual_host, '')
return self.http_client.delete(API_EXCHANGE_UNBIND %
(
virtual_host,
source,
destination,
properties_key or routing_key
),
payload=unbind_payload)