Skip to content

Commit e7dc1d5

Browse files
committed
Fixed nats.request method.
1 parent 1a2a053 commit e7dc1d5

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

python/natsrpy/_natsrpy_rs/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Nats:
175175
headers: dict[str, Any] | None = None,
176176
inbox: str | None = None,
177177
timeout: float | timedelta | None = None,
178-
) -> None:
178+
) -> Message:
179179
"""Send a request and discard the response.
180180
181181
:param subject: subject to send the request to.
@@ -185,6 +185,7 @@ class Nats:
185185
if None.
186186
:param timeout: maximum time to wait for a response in seconds
187187
or as a timedelta, defaults to the client request_timeout.
188+
:return: response message.
188189
"""
189190

190191
async def drain(self) -> None:

python/tests/test_request_reply.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,71 @@
11
import asyncio
22
import uuid
33

4-
from natsrpy import Nats
4+
from natsrpy import Message, Nats
55

66

77
async def test_request_sends_with_reply(nats: Nats) -> None:
88
subj = uuid.uuid4().hex
99

10-
received_payload: list[bytes] = []
11-
received_reply: list[str | None] = []
10+
received_msgs: list[Message] = []
1211

1312
async def responder() -> None:
1413
sub = await nats.subscribe(subject=subj)
1514
msg = await anext(sub)
16-
received_payload.append(msg.payload)
17-
received_reply.append(msg.reply)
15+
received_msgs.append(msg)
1816
if msg.reply:
1917
await nats.publish(msg.reply, b"reply-data")
2018

2119
task = asyncio.create_task(responder())
2220
await asyncio.sleep(0.1)
2321

24-
# request() sends a message and waits for a reply (though the response
25-
# is not returned by the current implementation)
26-
await nats.request(subj, b"request-payload")
22+
response = await nats.request(subj, b"request-payload")
2723
await task
2824

29-
assert received_payload == [b"request-payload"]
30-
# request() should set a reply subject automatically
31-
assert received_reply[0] is not None
25+
assert response.payload == b"reply-data"
26+
assert received_msgs
27+
assert received_msgs[0].payload == b"request-payload"
28+
assert received_msgs[0].reply is not None
3229

3330

3431
async def test_request_with_headers(nats: Nats) -> None:
3532
subj = uuid.uuid4().hex
3633

37-
received_headers: list[dict[str, str]] = []
34+
received_msgs: list[Message] = []
3835

3936
async def responder() -> None:
4037
sub = await nats.subscribe(subject=subj)
4138
msg = await anext(sub)
42-
received_headers.append(msg.headers)
39+
received_msgs.append(msg)
4340
if msg.reply:
4441
await nats.publish(msg.reply, b"reply")
4542

4643
task = asyncio.create_task(responder())
4744
await asyncio.sleep(0.1)
4845

49-
await nats.request(subj, b"data", headers={"x-custom": "value"})
46+
resp = await nats.request(subj, b"data", headers={"x-custom": "value"})
5047
await task
51-
52-
assert received_headers[0] == {"x-custom": "value"}
48+
assert resp.payload == b"reply"
49+
assert received_msgs[0].headers == {"x-custom": "value"}
5350

5451

5552
async def test_request_none_payload(nats: Nats) -> None:
5653
subj = uuid.uuid4().hex
5754

58-
received_payload: list[bytes] = []
55+
received_msgs: list[Message] = []
5956

6057
async def responder() -> None:
6158
sub = await nats.subscribe(subject=subj)
6259
msg = await anext(sub)
63-
received_payload.append(msg.payload)
60+
received_msgs.append(msg)
6461
if msg.reply:
6562
await nats.publish(msg.reply, b"reply")
6663

6764
task = asyncio.create_task(responder())
6865
await asyncio.sleep(0.1)
6966

70-
await nats.request(subj, b"")
67+
response = await nats.request(subj, b"")
7168
await task
69+
assert response.payload == b"reply"
7270

73-
assert received_payload[0] == b""
71+
assert received_msgs[0].payload == b""

src/nats_cls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ impl NatsCls {
176176
inbox,
177177
timeout: timeout.map(Into::into).map(Some),
178178
};
179-
session.send_request(subject, request).await?;
180-
Ok(())
179+
crate::message::Message::try_from(session.send_request(subject, request).await?)
181180
} else {
182181
Err(NatsrpyError::NotInitialized)
183182
}

0 commit comments

Comments
 (0)