aws-durable-execution-sdk-python-testing 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. aws_durable_execution_sdk_python_testing/__about__.py +4 -0
  2. aws_durable_execution_sdk_python_testing/__init__.py +20 -0
  3. aws_durable_execution_sdk_python_testing/checkpoint/__init__.py +1 -0
  4. aws_durable_execution_sdk_python_testing/checkpoint/processor.py +101 -0
  5. aws_durable_execution_sdk_python_testing/checkpoint/processors/__init__.py +1 -0
  6. aws_durable_execution_sdk_python_testing/checkpoint/processors/base.py +199 -0
  7. aws_durable_execution_sdk_python_testing/checkpoint/processors/callback.py +89 -0
  8. aws_durable_execution_sdk_python_testing/checkpoint/processors/context.py +59 -0
  9. aws_durable_execution_sdk_python_testing/checkpoint/processors/execution.py +52 -0
  10. aws_durable_execution_sdk_python_testing/checkpoint/processors/step.py +124 -0
  11. aws_durable_execution_sdk_python_testing/checkpoint/processors/wait.py +95 -0
  12. aws_durable_execution_sdk_python_testing/checkpoint/transformer.py +104 -0
  13. aws_durable_execution_sdk_python_testing/checkpoint/validators/__init__.py +1 -0
  14. aws_durable_execution_sdk_python_testing/checkpoint/validators/checkpoint.py +242 -0
  15. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/__init__.py +1 -0
  16. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/callback.py +45 -0
  17. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/context.py +73 -0
  18. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/execution.py +47 -0
  19. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/invoke.py +56 -0
  20. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/step.py +106 -0
  21. aws_durable_execution_sdk_python_testing/checkpoint/validators/operations/wait.py +54 -0
  22. aws_durable_execution_sdk_python_testing/checkpoint/validators/transitions.py +66 -0
  23. aws_durable_execution_sdk_python_testing/cli.py +501 -0
  24. aws_durable_execution_sdk_python_testing/client.py +50 -0
  25. aws_durable_execution_sdk_python_testing/exceptions.py +288 -0
  26. aws_durable_execution_sdk_python_testing/execution.py +440 -0
  27. aws_durable_execution_sdk_python_testing/executor.py +1214 -0
  28. aws_durable_execution_sdk_python_testing/invoker.py +327 -0
  29. aws_durable_execution_sdk_python_testing/model.py +3267 -0
  30. aws_durable_execution_sdk_python_testing/observer.py +144 -0
  31. aws_durable_execution_sdk_python_testing/py.typed +1 -0
  32. aws_durable_execution_sdk_python_testing/runner.py +1185 -0
  33. aws_durable_execution_sdk_python_testing/scheduler.py +246 -0
  34. aws_durable_execution_sdk_python_testing/stores/__init__.py +1 -0
  35. aws_durable_execution_sdk_python_testing/stores/base.py +147 -0
  36. aws_durable_execution_sdk_python_testing/stores/filesystem.py +104 -0
  37. aws_durable_execution_sdk_python_testing/stores/memory.py +38 -0
  38. aws_durable_execution_sdk_python_testing/stores/sqlite.py +283 -0
  39. aws_durable_execution_sdk_python_testing/token.py +49 -0
  40. aws_durable_execution_sdk_python_testing/web/__init__.py +1 -0
  41. aws_durable_execution_sdk_python_testing/web/errors.py +8 -0
  42. aws_durable_execution_sdk_python_testing/web/handlers.py +807 -0
  43. aws_durable_execution_sdk_python_testing/web/models.py +266 -0
  44. aws_durable_execution_sdk_python_testing/web/routes.py +684 -0
  45. aws_durable_execution_sdk_python_testing/web/serialization.py +245 -0
  46. aws_durable_execution_sdk_python_testing/web/server.py +243 -0
  47. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/METADATA +221 -0
  48. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/RECORD +52 -0
  49. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/WHEEL +4 -0
  50. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/entry_points.txt +2 -0
  51. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/licenses/LICENSE +175 -0
  52. aws_durable_execution_sdk_python_testing-1.0.0.dist-info/licenses/NOTICE +1 -0
@@ -0,0 +1,266 @@
1
+ """HTTP request/response data models and utilities for the web runner."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import logging
7
+ from dataclasses import dataclass
8
+ from typing import Any, Protocol
9
+
10
+ from aws_durable_execution_sdk_python_testing.exceptions import (
11
+ AwsApiException,
12
+ InvalidParameterValueException,
13
+ )
14
+
15
+ # Removed deprecated imports from web.errors
16
+ from aws_durable_execution_sdk_python_testing.web.routes import Route
17
+ from aws_durable_execution_sdk_python_testing.web.serialization import (
18
+ AwsRestJsonDeserializer,
19
+ JSONSerializer,
20
+ Serializer,
21
+ )
22
+
23
+
24
+ logger = logging.getLogger(__name__)
25
+
26
+
27
+ @dataclass(frozen=True)
28
+ class HTTPRequest:
29
+ """HTTP request data model with dict or bytes body for handler logic."""
30
+
31
+ method: str
32
+ path: Route
33
+ headers: dict[str, str]
34
+ query_params: dict[str, list[str]]
35
+ body: dict[str, Any] | bytes
36
+
37
+ @classmethod
38
+ def from_raw_bytes(
39
+ cls,
40
+ body_bytes: bytes,
41
+ method: str = "POST",
42
+ path: Route | None = None,
43
+ headers: dict[str, str] | None = None,
44
+ query_params: dict[str, list[str]] | None = None,
45
+ ) -> HTTPRequest:
46
+ """Create HTTPRequest with raw bytes body (no parsing)."""
47
+ if headers is None:
48
+ headers = {}
49
+ if query_params is None:
50
+ query_params = {}
51
+ if path is None:
52
+ path = Route.from_string("")
53
+
54
+ return cls(
55
+ method=method,
56
+ path=path,
57
+ headers=headers,
58
+ query_params=query_params,
59
+ body=body_bytes,
60
+ )
61
+
62
+ @classmethod
63
+ def from_bytes(
64
+ cls,
65
+ body_bytes: bytes,
66
+ operation_name: str | None = None,
67
+ method: str = "POST",
68
+ path: Route | None = None,
69
+ headers: dict[str, str] | None = None,
70
+ query_params: dict[str, list[str]] | None = None,
71
+ ) -> HTTPRequest:
72
+ """Create HTTPRequest from raw bytes, deserializing to dict body.
73
+
74
+ Args:
75
+ body_bytes: Raw bytes to deserialize
76
+ operation_name: Optional AWS operation name for boto deserialization
77
+ method: HTTP method (default: POST)
78
+ path: Route object (required for actual usage)
79
+ headers: HTTP headers (default: empty dict)
80
+ query_params: Query parameters (default: empty dict)
81
+
82
+ Returns:
83
+ HTTPRequest: Request with deserialized dict body
84
+
85
+ Raises:
86
+ InvalidParameterValueException: If deserialization fails with both AWS and JSON methods
87
+ """
88
+ if headers is None:
89
+ headers = {}
90
+ if query_params is None:
91
+ query_params = {}
92
+
93
+ # Skip body parsing for GET requests
94
+ if method == "GET":
95
+ body_dict = {}
96
+ logger.debug("GET request, skipping body parsing")
97
+ # Try AWS deserialization first if operation_name provided
98
+ elif operation_name:
99
+ try:
100
+ deserializer = AwsRestJsonDeserializer.create(operation_name)
101
+ body_dict = deserializer.from_bytes(body_bytes)
102
+ logger.debug(
103
+ "Successfully deserialized request using AWS deserializer for %s",
104
+ operation_name,
105
+ )
106
+ except InvalidParameterValueException as e:
107
+ logger.warning(
108
+ "AWS deserialization failed for %s, falling back to JSON: %s",
109
+ operation_name,
110
+ e,
111
+ )
112
+ # Fall back to standard JSON
113
+ try:
114
+ body_dict = json.loads(body_bytes.decode("utf-8"))
115
+ logger.debug(
116
+ "Successfully deserialized request using JSON fallback"
117
+ )
118
+ except (json.JSONDecodeError, UnicodeDecodeError) as json_error:
119
+ msg = f"Both AWS and JSON deserialization failed: AWS error: {e}, JSON error: {json_error}"
120
+ raise InvalidParameterValueException(msg) from json_error
121
+ else:
122
+ # Use standard JSON deserialization
123
+ try:
124
+ if body_bytes == b"":
125
+ body_dict = {}
126
+ else:
127
+ body_dict = json.loads(body_bytes.decode("utf-8"))
128
+ logger.debug("Successfully deserialized request using standard JSON")
129
+ except (json.JSONDecodeError, UnicodeDecodeError) as e:
130
+ msg = f"JSON deserialization failed: {e}"
131
+ raise InvalidParameterValueException(msg) from e
132
+
133
+ # Handle case where path is None for testing
134
+ if path is None:
135
+ path = Route.from_string("")
136
+
137
+ return cls(
138
+ method=method,
139
+ path=path,
140
+ headers=headers,
141
+ query_params=query_params,
142
+ body=body_dict,
143
+ )
144
+
145
+
146
+ @dataclass(frozen=True)
147
+ class HTTPResponse:
148
+ """HTTP response data model with dict body and serialization capabilities."""
149
+
150
+ status_code: int
151
+ headers: dict[str, str]
152
+ body: dict[str, Any]
153
+ serializer: Serializer = JSONSerializer()
154
+
155
+ def body_to_bytes(self) -> bytes:
156
+ """Convert response dict body to bytes for HTTP transmission.
157
+
158
+ Returns:
159
+ bytes: Serialized response body
160
+
161
+ Raises:
162
+ InvalidParameterValueException: If serialization fails with both AWS and JSON methods
163
+ """
164
+ result = self.serializer.to_bytes(data=self.body)
165
+ logger.debug("Serialized result - before: %s, after: %s", self.body, result)
166
+ return result
167
+
168
+ @classmethod
169
+ def from_dict(
170
+ cls,
171
+ data: dict[str, Any],
172
+ status_code: int = 200,
173
+ headers: dict[str, str] | None = None,
174
+ ) -> HTTPResponse:
175
+ """Create HTTPResponse from dict data.
176
+
177
+ Args:
178
+ data: Response data as dictionary
179
+ status_code: HTTP status code (default: 200)
180
+ headers: HTTP headers (default: empty dict)
181
+
182
+ Returns:
183
+ HTTPResponse: Response with dict body
184
+ """
185
+ if headers is None:
186
+ headers = {}
187
+
188
+ return cls(status_code=status_code, headers=headers, body=data)
189
+
190
+ @staticmethod
191
+ def create_json(
192
+ status_code: int,
193
+ data: dict[str, Any],
194
+ additional_headers: dict[str, str] | None = None,
195
+ ) -> HTTPResponse:
196
+ """Create a JSON HTTP response.
197
+
198
+ Args:
199
+ status_code: HTTP status code
200
+ data: Data to serialize as JSON
201
+ additional_headers: Optional additional headers to include
202
+
203
+ Returns:
204
+ HTTPResponse: The HTTP response with dict body
205
+ """
206
+ headers = {"Content-Type": "application/json"}
207
+ if additional_headers:
208
+ headers.update(additional_headers)
209
+
210
+ return HTTPResponse(status_code=status_code, headers=headers, body=data)
211
+
212
+ # Removed deprecated create_error method - use create_error_from_exception instead
213
+
214
+ @staticmethod
215
+ def create_error_from_exception(aws_exception: AwsApiException) -> HTTPResponse:
216
+ """Create AWS-compliant error response from AwsApiException.
217
+
218
+ Args:
219
+ aws_exception: The AWS API exception to convert to HTTP response
220
+
221
+ Returns:
222
+ HTTPResponse: The HTTP error response with AWS-compliant format
223
+ """
224
+ if not isinstance(aws_exception, AwsApiException):
225
+ msg = f"Expected AwsApiException, got {type(aws_exception)}"
226
+ raise TypeError(msg)
227
+
228
+ # Use exception's http_status_code and to_dict() method
229
+ # This removes the wrapper "error" object to match AWS format
230
+ error_data = aws_exception.to_dict()
231
+ return HTTPResponse.create_json(aws_exception.http_status_code, error_data)
232
+
233
+ @staticmethod
234
+ def create_empty(
235
+ status_code: int, additional_headers: dict[str, str] | None = None
236
+ ) -> HTTPResponse:
237
+ """Create an empty HTTP response.
238
+
239
+ Args:
240
+ status_code: HTTP status code
241
+ additional_headers: Optional additional headers to include
242
+
243
+ Returns:
244
+ HTTPResponse: The HTTP response with empty dict body
245
+ """
246
+ headers = {}
247
+ if additional_headers:
248
+ headers.update(additional_headers)
249
+
250
+ return HTTPResponse(status_code=status_code, headers=headers, body={})
251
+
252
+
253
+ class OperationHandler(Protocol):
254
+ """Protocol for handling HTTP operations with strongly-typed paths."""
255
+
256
+ def handle(self, parsed_route: Route, request: HTTPRequest) -> HTTPResponse:
257
+ """Handle an HTTP request and return an HTTP response.
258
+
259
+ Args:
260
+ parsed_route: The strongly-typed route object
261
+ request: The HTTP request data
262
+
263
+ Returns:
264
+ HTTPResponse: The HTTP response to send to the client
265
+ """
266
+ ... # pragma: no cover