Skip to content

Commit

Permalink
Update documentation to describe multi-part response behavior
Browse files Browse the repository at this point in the history
Add test for `stream_callback` final response
  • Loading branch information
NeonDaniel committed Dec 10, 2024
1 parent bc321bb commit 71d1685
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,28 @@ A response may be sent via:
properties=pika.BasicProperties(expiration='1000')
)
```
Where `<queue>` is the queue to which the response will be published, and `data` is a `bytes` response (generally a `base64`-encoded `dict`).
Where `<queue>` is the queue to which the response will be published, and `data`
is a `bytes` response (generally a `base64`-encoded `dict`).

#### Multi-part Responses
A callback function may choose to publish multiple response messages so the client
may receive partial responses as they are being generated. If multiple responses
will be returned, the following requirements must be met:
- Each response must be a dict with `_part` and `_is_final` keys.
- The final response must specify `_is_final=True`
- The final response *MUST NOT* require the client to handle partial responses

## Client Requests
Most client applications will interact with services via `send_mq_request`. This
function will return a `dict` response to the input message.

### Multi-part Responses
A caller may optionally include a `stream_callback` argument which may receive
partial responses if supported by the service generating the response. The
`stream_callback` will always be called with the final result that is returned
by `send_mq_request`. Keep in mind that the `timeout` param passed to
`send_mq_request` applies to the full response, so it may be desirable to increase
the timeout if

### [BETA] Asynchronous Consumers

Expand Down
4 changes: 3 additions & 1 deletion neon_mq_connector/utils/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def send_mq_request(vhost: str, request_data: dict, target_queue: str,
:param target_queue: queue to post request to
:param response_queue: optional queue to monitor for a response.
Generally should be blank
:param timeout: time in seconds to wait for a response before timing out
:param timeout: time in seconds to wait for a complete response before
timing out. Note that in the event of a timeout, a partial response may
have been handled by `stream_callback`.
:param expect_response: boolean indicating whether a response is expected
:param stream_callback: Optional function to pass partial responses to
:return: response to request
Expand Down
3 changes: 3 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def test_multi_part_mq_response(self):
self.assertEqual(len(response['response'].split()), request['num_parts'])
self.assertTrue(response['_is_final'])

# Last callback is the same as the standard response
self.assertEqual(response, stream_callback.call_args[0][0])

def test_multiple_mq_requests(self):
from neon_mq_connector.utils.client_utils import send_mq_request
responses = dict()
Expand Down

0 comments on commit 71d1685

Please sign in to comment.