|
52 | 52 | MinosActionNotFoundException, |
53 | 53 | ) |
54 | 54 | from ...requests import ( |
55 | | - USER_CONTEXT_VAR, |
| 55 | + REQUEST_USER_CONTEXT_VAR, |
56 | 56 | Response, |
57 | 57 | ResponseException, |
58 | 58 | ) |
59 | 59 | from ...utils import ( |
60 | 60 | consume_queue, |
61 | 61 | ) |
62 | 62 | from ..messages import ( |
| 63 | + REQUEST_HEADERS_CONTEXT_VAR, |
63 | 64 | BrokerMessage, |
64 | 65 | BrokerMessageStatus, |
65 | 66 | ) |
@@ -304,42 +305,49 @@ async def dispatch_one(self, entry: BrokerHandlerEntry) -> None: |
304 | 305 |
|
305 | 306 | fn = self.get_callback(entry.callback) |
306 | 307 | message = entry.data |
307 | | - data, status = await fn(message) |
| 308 | + data, status, headers = await fn(message) |
308 | 309 |
|
309 | 310 | if message.reply_topic is not None: |
310 | 311 | await self.publisher.send( |
311 | | - data, topic=message.reply_topic, saga=message.saga, status=status, user=message.user |
| 312 | + data, |
| 313 | + topic=message.reply_topic, |
| 314 | + identifier=message.identifier, |
| 315 | + status=status, |
| 316 | + user=message.user, |
| 317 | + headers=headers, |
312 | 318 | ) |
313 | 319 |
|
314 | 320 | @staticmethod |
315 | 321 | def get_callback( |
316 | 322 | fn: Callable[[BrokerRequest], Union[Optional[BrokerRequest], Awaitable[Optional[BrokerRequest]]]] |
317 | | - ) -> Callable[[BrokerMessage], Awaitable[tuple[Any, BrokerMessageStatus]]]: |
| 323 | + ) -> Callable[[BrokerMessage], Awaitable[tuple[Any, BrokerMessageStatus, dict[str, str]]]]: |
318 | 324 | """Get the handler function to be used by the Broker Handler. |
319 | 325 |
|
320 | 326 | :param fn: The action function. |
321 | 327 | :return: A wrapper function around the given one that is compatible with the Broker Handler API. |
322 | 328 | """ |
323 | 329 |
|
324 | | - async def _fn(raw: BrokerMessage) -> tuple[Any, BrokerMessageStatus]: |
| 330 | + async def _fn(raw: BrokerMessage) -> tuple[Any, BrokerMessageStatus, dict[str, str]]: |
325 | 331 | request = BrokerRequest(raw) |
326 | | - token = USER_CONTEXT_VAR.set(request.user) |
| 332 | + user_token = REQUEST_USER_CONTEXT_VAR.set(request.user) |
| 333 | + headers_token = REQUEST_HEADERS_CONTEXT_VAR.set(raw.headers) |
327 | 334 |
|
328 | 335 | try: |
329 | 336 | response = fn(request) |
330 | 337 | if isawaitable(response): |
331 | 338 | response = await response |
332 | 339 | if isinstance(response, Response): |
333 | 340 | response = await response.content() |
334 | | - return response, BrokerMessageStatus.SUCCESS |
| 341 | + return response, BrokerMessageStatus.SUCCESS, REQUEST_HEADERS_CONTEXT_VAR.get() |
335 | 342 | except ResponseException as exc: |
336 | 343 | logger.warning(f"Raised an application exception: {exc!s}") |
337 | | - return repr(exc), BrokerMessageStatus.ERROR |
| 344 | + return repr(exc), BrokerMessageStatus.ERROR, REQUEST_HEADERS_CONTEXT_VAR.get() |
338 | 345 | except Exception as exc: |
339 | 346 | logger.exception(f"Raised a system exception: {exc!r}") |
340 | | - return repr(exc), BrokerMessageStatus.SYSTEM_ERROR |
| 347 | + return repr(exc), BrokerMessageStatus.SYSTEM_ERROR, REQUEST_HEADERS_CONTEXT_VAR.get() |
341 | 348 | finally: |
342 | | - USER_CONTEXT_VAR.reset(token) |
| 349 | + REQUEST_USER_CONTEXT_VAR.reset(user_token) |
| 350 | + REQUEST_HEADERS_CONTEXT_VAR.reset(headers_token) |
343 | 351 |
|
344 | 352 | return _fn |
345 | 353 |
|
|
0 commit comments