|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import json |
5 | 6 | import logging |
6 | 7 | from abc import ABC, abstractmethod |
7 | 8 | from typing import TYPE_CHECKING, Any, cast |
|
27 | 28 | SendDurableExecutionCallbackFailureResponse, |
28 | 29 | SendDurableExecutionCallbackHeartbeatRequest, |
29 | 30 | SendDurableExecutionCallbackHeartbeatResponse, |
30 | | - SendDurableExecutionCallbackSuccessRequest, |
31 | 31 | SendDurableExecutionCallbackSuccessResponse, |
32 | 32 | StartDurableExecutionInput, |
33 | 33 | StartDurableExecutionOutput, |
|
37 | 37 | from aws_durable_execution_sdk_python_testing.web.models import ( |
38 | 38 | HTTPRequest, |
39 | 39 | HTTPResponse, |
40 | | - parse_json_body, |
41 | 40 | ) |
42 | 41 | from aws_durable_execution_sdk_python_testing.web.routes import ( |
43 | 42 | CallbackFailureRoute, |
@@ -92,9 +91,21 @@ def _parse_json_body(self, request: HTTPRequest) -> dict[str, Any]: |
92 | 91 | dict: The parsed JSON data |
93 | 92 |
|
94 | 93 | Raises: |
95 | | - ValueError: If the request body is empty or invalid JSON |
| 94 | + InvalidParameterValueException: If the request body is empty or invalid JSON |
96 | 95 | """ |
97 | | - return parse_json_body(request) |
| 96 | + if not request.body: |
| 97 | + msg = "Request body is required" |
| 98 | + raise InvalidParameterValueException(msg) |
| 99 | + |
| 100 | + # Handle both dict and bytes body types |
| 101 | + if isinstance(request.body, dict): |
| 102 | + return request.body |
| 103 | + |
| 104 | + try: |
| 105 | + return json.loads(request.body.decode("utf-8")) |
| 106 | + except (json.JSONDecodeError, UnicodeDecodeError) as e: |
| 107 | + msg = f"Invalid JSON in request body: {e}" |
| 108 | + raise InvalidParameterValueException(msg) from e |
98 | 109 |
|
99 | 110 | def _json_response( |
100 | 111 | self, |
@@ -631,20 +642,24 @@ def handle(self, parsed_route: Route, request: HTTPRequest) -> HTTPResponse: |
631 | 642 | HTTPResponse: The HTTP response to send to the client |
632 | 643 | """ |
633 | 644 | try: |
634 | | - body_data: dict[str, Any] = self._parse_json_body(request) |
635 | | - callback_request: SendDurableExecutionCallbackSuccessRequest = ( |
636 | | - SendDurableExecutionCallbackSuccessRequest.from_dict(body_data) |
637 | | - ) |
638 | | - |
639 | 645 | callback_route = cast(CallbackSuccessRoute, parsed_route) |
640 | 646 | callback_id: str = callback_route.callback_id |
641 | 647 |
|
| 648 | + # For binary payload operations, body is raw bytes |
| 649 | + result_bytes = request.body if isinstance(request.body, bytes) else b"" |
| 650 | + |
642 | 651 | callback_response: SendDurableExecutionCallbackSuccessResponse = ( # noqa: F841 |
643 | 652 | self.executor.send_callback_success( |
644 | | - callback_id=callback_id, result=callback_request.result |
| 653 | + callback_id=callback_id, result=result_bytes |
645 | 654 | ) |
646 | 655 | ) |
647 | 656 |
|
| 657 | + logger.debug( |
| 658 | + "Callback %s succeeded with result: %s", |
| 659 | + callback_id, |
| 660 | + result_bytes.decode("utf-8", errors="replace"), |
| 661 | + ) |
| 662 | + |
648 | 663 | # Callback success response is empty |
649 | 664 | return self._success_response({}) |
650 | 665 |
|
@@ -672,20 +687,26 @@ def handle(self, parsed_route: Route, request: HTTPRequest) -> HTTPResponse: |
672 | 687 | HTTPResponse: The HTTP response to send to the client |
673 | 688 | """ |
674 | 689 | try: |
| 690 | + callback_route = cast(CallbackFailureRoute, parsed_route) |
| 691 | + callback_id: str = callback_route.callback_id |
| 692 | + |
675 | 693 | body_data: dict[str, Any] = self._parse_json_body(request) |
676 | 694 | callback_request: SendDurableExecutionCallbackFailureRequest = ( |
677 | | - SendDurableExecutionCallbackFailureRequest.from_dict(body_data) |
| 695 | + SendDurableExecutionCallbackFailureRequest.from_dict( |
| 696 | + body_data, callback_id |
| 697 | + ) |
678 | 698 | ) |
679 | 699 |
|
680 | | - callback_route = cast(CallbackFailureRoute, parsed_route) |
681 | | - callback_id: str = callback_route.callback_id |
682 | | - |
683 | 700 | callback_response: SendDurableExecutionCallbackFailureResponse = ( # noqa: F841 |
684 | 701 | self.executor.send_callback_failure( |
685 | 702 | callback_id=callback_id, error=callback_request.error |
686 | 703 | ) |
687 | 704 | ) |
688 | 705 |
|
| 706 | + logger.debug( |
| 707 | + "Callback %s failed with error: %s", callback_id, callback_request.error |
| 708 | + ) |
| 709 | + |
689 | 710 | # Callback failure response is empty |
690 | 711 | return self._success_response({}) |
691 | 712 |
|
|
0 commit comments