Skip to content

Commit 2a5543f

Browse files
authored
Merge pull request #46 from NHSDigital/mm-mesh-1786-allow-transparent-compression
mesh-1786: allow transparent compression to be set on send message
2 parents 0872587 + 5ec30ff commit 2a5543f

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

mesh_client/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ def __init__( # noqa: C901
305305
retry_status_force_list: Tuple[int, ...] = (425, 429, 502, 503, 504),
306306
retry_methods: Tuple[str, ...] = ("HEAD", "GET", "PUT", "POST", "DELETE", "OPTIONS", "TRACE"),
307307
timeout: Union[int, float] = 10 * 60,
308+
application_name: Optional[str] = None,
308309
):
309310
"""
310311
Create a new MeshClient.
@@ -342,6 +343,8 @@ def __init__( # noqa: C901
342343
self._close_called = False
343344

344345
self._session = requests.Session()
346+
application_name = (application_name or "").strip()
347+
self._client_name = f"{application_name};mesh_client" if application_name else "mesh_client"
345348

346349
if isinstance(url, str):
347350
endpoint_config = try_get_endpoint_from_url(url)
@@ -396,7 +399,7 @@ def __init__( # noqa: C901
396399
self._session.headers = {
397400
"Accept": "application/vnd.mesh.v2+json",
398401
"User-Agent": (
399-
f"mesh_client;{__version__};N/A;{platform.processor() or platform.machine()};"
402+
f"{self._client_name};{__version__};N/A;{platform.processor() or platform.machine()};"
400403
f"{platform.system()};{platform.release()} {platform.version()}"
401404
),
402405
"Accept-Encoding": "gzip",
@@ -430,7 +433,7 @@ def handshake(self):
430433
https://digital.nhs.uk/developer/api-catalogue/message-exchange-for-social-care-and-health-api#post-/messageexchange/-mailbox_id-
431434
"""
432435
headers = {
433-
"mex-ClientVersion": f"mesh_client=={__version__}",
436+
"mex-ClientVersion": f"{self._client_name}=={__version__}",
434437
"mex-OSArchitecture": platform.processor() or platform.machine(),
435438
"mex-OSName": platform.system(),
436439
"mex-OSVersion": f"{platform.release()} {platform.version()}",
@@ -547,6 +550,7 @@ def send_message( # noqa: C901
547550
recipient: str,
548551
data,
549552
max_chunk_size: Optional[int] = None,
553+
transparent_compress: Optional[bool] = None,
550554
**kwargs,
551555
) -> str:
552556
"""
@@ -577,7 +581,7 @@ def send_message( # noqa: C901
577581
client will not attempt to compress or decompress data. Transparent
578582
compression for sending is enabled as a constructor option.
579583
"""
580-
transparent_compress = self._transparent_compress
584+
transparent_compress = self._transparent_compress if transparent_compress is None else transparent_compress
581585

582586
def maybe_compressed(maybe_compress: bytes):
583587
if not transparent_compress:

tests/mesh_sandbox_tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ def test_readlines(alice: MeshClient, bob: MeshClient):
191191

192192
def test_transparent_compression(alice: MeshClient, bob: MeshClient):
193193
print("Sending")
194-
alice._transparent_compress = True
195-
message_id = alice.send_message(bob_mailbox, b"Hello Bob Compressed", workflow_id=uuid4().hex)
194+
message_id = alice.send_message(
195+
bob_mailbox, b"Hello Bob Compressed", workflow_id=uuid4().hex, transparent_compress=True
196+
)
196197
assert bob.list_messages() == [message_id]
197198
print("Receiving")
198199
msg = bob.retrieve_message(message_id)
200+
assert msg.compressed
199201
assert msg.read() == b"Hello Bob Compressed"
200202
assert msg.mex_header("from") == "ALICE"
201203
msg.acknowledge()

0 commit comments

Comments
 (0)