Skip to content

Commit 847c08c

Browse files
committed
converter
Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent e73a936 commit 847c08c

File tree

8 files changed

+56
-58
lines changed

8 files changed

+56
-58
lines changed

.ci/ubuntu/gha-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -o xtrace
77
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
88
readonly script_dir
99
echo "[INFO] script_dir: '$script_dir'"
10-
readonly rabbitmq_image=rabbitmq:4.1.0-alpine
10+
readonly rabbitmq_image=rabbitmq:4.1.0-beta.4-management-alpine
1111

1212

1313
readonly docker_name_prefix='rabbitmq-amqp-python-client'

examples/tls/tls_example.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# type: ignore
2+
import os
23
import sys
34
from traceback import print_exception
45

@@ -79,15 +80,14 @@ def create_connection(environment: Environment) -> Connection:
7980

8081

8182
def main() -> None:
82-
8383
exchange_name = "test-exchange"
8484
queue_name = "example-queue"
8585
routing_key = "routing-key"
8686
ca_p12_store = ".ci/certs/ca.p12"
8787
ca_cert_file = ".ci/certs/ca_certificate.pem"
88-
client_cert = ".ci/certs/client_certificate.pem"
89-
client_key = ".ci/certs/client_key.pem"
90-
client_p12_store = ".ci/certs/client.p12"
88+
client_cert = ".ci/certs/client_localhost_certificate.pem"
89+
client_key = ".ci/certs/client_localhost_key.pem"
90+
client_p12_store = ".ci/certs/client_localhost.p12"
9191
uri = "amqps://guest:guest@localhost:5671/"
9292

9393
if sys.platform == "win32":
@@ -138,6 +138,9 @@ def main() -> None:
138138
"connection failed. working directory should be project root"
139139
)
140140
else:
141+
print(" ca_cert_file {}".format(os.path.isfile(ca_cert_file)))
142+
print(" client_cert {}".format(os.path.isfile(client_cert)))
143+
print(" client_key {}".format(os.path.isfile(client_key)))
141144
environment = Environment(
142145
uri,
143146
ssl_context=PosixSslConfigurationContext(

rabbitmq_amqp_python_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .common import ExchangeType, QueueType
66
from .connection import Connection
77
from .consumer import Consumer
8+
from .converter import Converter
89
from .entities import (
910
ExchangeCustomSpecification,
1011
ExchangeSpecification,
@@ -91,4 +92,5 @@
9192
"ExchangeCustomSpecification",
9293
"RecoveryConfiguration",
9394
"OAuth2Options",
95+
"Converter",
9496
]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Converter:
2+
3+
@staticmethod
4+
def bytes_to_string(body: bytes) -> str:
5+
"""
6+
Convert the body of a message to a string.
7+
8+
Args:
9+
body: The body of the message
10+
11+
Returns:
12+
str: The string representation of the body
13+
"""
14+
return "".join(map(chr, body))
15+
16+
@staticmethod
17+
def string_to_bytes(body: str) -> bytes:
18+
"""
19+
Convert a string to the body of a message.
20+
21+
Args:
22+
body: The string to convert
23+
24+
Returns:
25+
bytes: The byte representation of the string
26+
"""
27+
return str.encode(body)

rabbitmq_amqp_python_client/utils.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,3 @@ def validate_annotations(annotations: []) -> bool: # type: ignore
77
validated = False
88
return validated
99
return validated
10-
11-
12-
def bytes_to_string(body: bytes) -> str:
13-
"""
14-
Convert the body of a message to a string.
15-
16-
Args:
17-
body: The body of the message
18-
19-
Returns:
20-
str: The string representation of the body
21-
"""
22-
return "".join(map(chr, body))
23-
24-
25-
def string_to_bytes(body: str) -> bytes:
26-
"""
27-
Convert a string to the body of a message.
28-
29-
Args:
30-
body: The string to convert
31-
32-
Returns:
33-
bytes: The byte representation of the string
34-
"""
35-
return str.encode(body)

tests/test_consumer.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
ArgumentOutOfRangeException,
44
Connection,
55
Environment,
6-
QuorumQueueSpecification,
7-
)
8-
from rabbitmq_amqp_python_client.utils import (
9-
bytes_to_string,
6+
QuorumQueueSpecification, Converter,
107
)
118

129
from .conftest import (
@@ -45,7 +42,7 @@ def test_consumer_sync_queue_accept(connection: Connection) -> None:
4542
# consumer synchronously without handler
4643
for i in range(messages_to_send):
4744
message = consumer.consume()
48-
if bytes_to_string(message.body) == "test{}".format(i):
45+
if Converter.bytes_to_string(message.body) == "test{}".format(i):
4946
consumed = consumed + 1
5047

5148
consumer.close()

tests/test_publisher.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
QuorumQueueSpecification,
1212
RecoveryConfiguration,
1313
StreamSpecification,
14-
ValidationCodeException,
15-
)
16-
from rabbitmq_amqp_python_client.utils import (
17-
string_to_bytes,
14+
ValidationCodeException, Converter,
1815
)
1916

2017
from .http_requests import delete_all_connections
@@ -37,7 +34,7 @@ def test_publish_queue(connection: Connection) -> None:
3734
publisher = connection.publisher(
3835
destination=AddressHelper.queue_address(queue_name)
3936
)
40-
status = publisher.publish(Message(body=string_to_bytes("test")))
37+
status = publisher.publish(Message(body=Converter.string_to_bytes("test")))
4138
if status.remote_state == OutcomeState.ACCEPTED:
4239
accepted = True
4340
except Exception:
@@ -112,7 +109,7 @@ def test_publish_ssl(connection_ssl: Connection) -> None:
112109
publisher = connection_ssl.publisher(
113110
destination=AddressHelper.queue_address(queue_name)
114111
)
115-
publisher.publish(Message(body="test"))
112+
publisher.publish(Message(body=Converter.string_to_bytes("test")))
116113
except Exception:
117114
raised = True
118115

@@ -150,7 +147,7 @@ def test_publish_per_message_to_invalid_destination(connection: Connection) -> N
150147
queue_name = "test-queue-1"
151148
raised = False
152149

153-
message = Message(body=string_to_bytes("test"))
150+
message = Message(body=Converter.string_to_bytes("test"))
154151
message = AddressHelper.message_to_address_helper(
155152
message, "/invalid_destination/" + queue_name
156153
)
@@ -182,7 +179,7 @@ def test_publish_per_message_both_address(connection: Connection) -> None:
182179
)
183180

184181
try:
185-
message = Message(body=string_to_bytes("test"))
182+
message = Message(body=Converter.string_to_bytes("test"))
186183
message = AddressHelper.message_to_address_helper(
187184
message, AddressHelper.queue_address(queue_name)
188185
)
@@ -215,7 +212,7 @@ def test_publish_exchange(connection: Connection) -> None:
215212

216213
try:
217214
publisher = connection.publisher(addr)
218-
status = publisher.publish(Message(body=string_to_bytes("test")))
215+
status = publisher.publish(Message(body=Converter.string_to_bytes("test")))
219216
if status.ACCEPTED:
220217
accepted = True
221218
except Exception:
@@ -247,7 +244,7 @@ def test_publish_purge(connection: Connection) -> None:
247244
destination=AddressHelper.queue_address(queue_name)
248245
)
249246
for i in range(messages_to_publish):
250-
publisher.publish(Message(body=string_to_bytes("test")))
247+
publisher.publish(Message(body=Converter.string_to_bytes("test")))
251248
except Exception:
252249
raised = True
253250

@@ -292,7 +289,7 @@ def test_disconnection_reconnection() -> None:
292289
# simulate a disconnection
293290
delete_all_connections()
294291
try:
295-
publisher.publish(Message(body=string_to_bytes("test")))
292+
publisher.publish(Message(body=Converter.string_to_bytes("test")))
296293

297294
except ConnectionClosed:
298295
disconnected = True
@@ -334,7 +331,7 @@ def test_queue_info_for_stream_with_validations(connection: Connection) -> None:
334331
)
335332

336333
for i in range(messages_to_send):
337-
publisher.publish(Message(body=string_to_bytes("test")))
334+
publisher.publish(Message(body=Converter.string_to_bytes("test")))
338335

339336

340337
def test_publish_per_message_exchange(connection: Connection) -> None:

tests/utils.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@
1515
Message,
1616
Publisher,
1717
QuorumQueueSpecification,
18-
)
19-
from rabbitmq_amqp_python_client.utils import (
20-
string_to_bytes,
18+
Converter,
2119
)
2220

2321

2422
def publish_messages(
25-
connection: Connection,
26-
messages_to_send: int,
27-
queue_name,
28-
filters: Optional[list[str]] = None,
23+
connection: Connection,
24+
messages_to_send: int,
25+
queue_name,
26+
filters: Optional[list[str]] = None,
2927
) -> None:
3028
annotations = {}
3129
if filters is not None:
@@ -36,13 +34,13 @@ def publish_messages(
3634
# publish messages_to_send messages
3735
for i in range(messages_to_send):
3836
publisher.publish(
39-
Message(body=string_to_bytes("test{}".format(i)), annotations=annotations)
37+
Message(body=Converter.string_to_bytes("test{}".format(i)), annotations=annotations)
4038
)
4139
publisher.close()
4240

4341

4442
def publish_per_message(publisher: Publisher, addr: str) -> Delivery:
45-
message = Message(body=string_to_bytes("test"))
43+
message = Message(body=Converter.string_to_bytes("test"))
4644
message = AddressHelper.message_to_address_helper(message, addr)
4745
status = publisher.publish(message)
4846
return status
@@ -74,7 +72,7 @@ def setup_dead_lettering(management: Management) -> str:
7472

7573

7674
def create_binding(
77-
management: Management, exchange_name: str, queue_name: str, routing_key: str
75+
management: Management, exchange_name: str, queue_name: str, routing_key: str
7876
) -> str:
7977
management.declare_exchange(ExchangeSpecification(name=exchange_name))
8078

0 commit comments

Comments
 (0)