diff --git a/.gitignore b/.gitignore index 86f3773..434000a 100644 --- a/.gitignore +++ b/.gitignore @@ -163,3 +163,6 @@ cython_debug/ # PyPI configuration file .pypirc + +# Jules agent files +.jules/ diff --git a/templates/api/handler.py b/templates/api/handler.py index c26925a..e729fe3 100644 --- a/templates/api/handler.py +++ b/templates/api/handler.py @@ -51,10 +51,8 @@ def create_item() -> Response: Returns: 201 with the created item, 422 on validation error, or 500 on error. """ - body = app.current_event.json_body - try: - item = Item.model_validate(body) + item = Item.model_validate_json(app.current_event.body) except ValidationError as exc: return Response(status_code=422, content_type="application/json", body=dumps({"errors": loads(exc.json())})) @@ -66,7 +64,7 @@ def create_item() -> Response: status_code=500, content_type="application/json", body=dumps({"message": "Internal server error"}) ) - return Response(status_code=201, content_type="application/json", body=dumps(item.dump())) + return Response(status_code=201, content_type="application/json", body=item.dump_json()) @logger.inject_lambda_context diff --git a/templates/api/models.py b/templates/api/models.py index e28060b..a13e591 100644 --- a/templates/api/models.py +++ b/templates/api/models.py @@ -13,3 +13,8 @@ def dump(self, **kwargs: Any) -> dict: kwargs.setdefault("by_alias", True) kwargs.setdefault("exclude_none", True) return self.model_dump(**kwargs) + + def dump_json(self, **kwargs: Any) -> str: + kwargs.setdefault("by_alias", True) + kwargs.setdefault("exclude_none", True) + return self.model_dump_json(**kwargs) diff --git a/templates/sqs/handler.py b/templates/sqs/handler.py index 09ab195..1bff9b4 100644 --- a/templates/sqs/handler.py +++ b/templates/sqs/handler.py @@ -41,7 +41,7 @@ def handle_record(self, record: SQSRecord) -> None: ValueError: If the message body cannot be parsed or processed. """ try: - message = SqsMessage.model_validate(record.json_body) + message = SqsMessage.model_validate_json(record.body) processed = ProcessedItem(id=message.id, content=message.content, status="PROCESSED") self._repository.put_item(processed.model_dump()) logger.info("Successfully processed and stored message", extra={"messageId": message.id}) diff --git a/tests/sqs/test_handler.py b/tests/sqs/test_handler.py index 0df5d0b..d1336d4 100644 --- a/tests/sqs/test_handler.py +++ b/tests/sqs/test_handler.py @@ -15,7 +15,7 @@ def test_handler_handle_record(repository): handler = Handler(repository) record = MagicMock() - record.json_body = {"id": "123", "content": "test content"} + record.body = dumps({"id": "123", "content": "test content"}) handler.handle_record(record)