Skip to content

Commit 8f7d5a7

Browse files
authored
Merge pull request #66 from febus982/fastapi-0.103
Fastapi 0.103
2 parents 0f003bb + 07c4c95 commit 8f7d5a7

File tree

3 files changed

+61
-61
lines changed

3 files changed

+61
-61
lines changed

http_app/routes/events.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import typing
2-
from typing import Any, Dict, List
2+
from typing import Dict, List
33

44
from fastapi import APIRouter, Body, Header, HTTPException
5+
from fastapi.openapi.models import Example
56
from pydantic import BaseModel
67

78
from domains.books.events import BookCreatedV1
9+
from domains.books.service import BookService
810

911
router = APIRouter(prefix="/events")
1012

@@ -22,7 +24,7 @@ def _event_registry() -> Dict[str, typing.Type[BaseModel]]:
2224
}
2325

2426

25-
def _event_schema_examples() -> dict[str, dict[str, Any]]:
27+
def _event_schema_examples() -> dict[str, Example]:
2628
missing_example_message = (
2729
"No example has been added to this event but you can"
2830
" still explore the event schema. (Ask the developer"
@@ -31,11 +33,11 @@ def _event_schema_examples() -> dict[str, dict[str, Any]]:
3133
)
3234

3335
return {
34-
k: {
35-
"value": getattr(v, "model_config", {})
36+
k: Example(
37+
value=getattr(v, "model_config", {})
3638
.get("json_schema_extra", {})
3739
.get("examples", [missing_example_message])[0]
38-
}
40+
)
3941
for k, v in _event_registry().items()
4042
}
4143

@@ -67,15 +69,6 @@ async def event_schema_list() -> List[str]:
6769

6870
@router.post(
6971
"",
70-
openapi_extra={
71-
"requestBody": {
72-
"content": {
73-
"application/cloudevents+json; charset=UTF-8": {
74-
"examples": _event_schema_examples(),
75-
}
76-
},
77-
},
78-
},
7972
status_code=204,
8073
description="""
8174
Entrypoint for CloudEvent processing, it supports only single events.
@@ -86,10 +79,12 @@ async def event_schema_list() -> List[str]:
8679
async def submit_event(
8780
event_data: _EVENTS_UNION_TYPE = Body(
8881
media_type="application/cloudevents+json; charset=UTF-8",
82+
openapi_examples=_event_schema_examples(),
8983
discriminator="type",
9084
),
9185
content_type: typing.Literal[
9286
"application/cloudevents+json; charset=UTF-8"
9387
] = Header(),
9488
) -> None:
95-
pass
89+
# Some routing will be necessary when multiple event types will be supported
90+
await BookService().book_created_event_handler(event_data.data.book_id)

poetry.lock

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/http_app/routes/test_events.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from unittest.mock import patch
1+
from unittest.mock import AsyncMock, MagicMock, patch
22

33
from httpx import AsyncClient
44

5+
from domains.books import service
56
from domains.books.events import BookCreatedV1
67
from domains.common.cloudevent_base import BaseEvent
78

@@ -39,12 +40,16 @@ async def test_event_returns_204(testapp):
3940
fake_event = BookCreatedV1(
4041
data={"book_id": 0, "title": "string", "author_name": "string"},
4142
)
42-
async with AsyncClient(app=testapp, base_url="http://test") as ac:
43-
response = await ac.post(
44-
"/events",
45-
headers={"content-type": "application/cloudevents+json; charset=UTF-8"},
46-
content=fake_event.model_dump_json(),
47-
)
43+
svc = MagicMock(autospec=service.BookService)
44+
svc.book_created_event_handler = AsyncMock(return_value=None)
45+
with patch("domains.books.service.BookService.__new__", return_value=svc):
46+
async with AsyncClient(app=testapp, base_url="http://test") as ac:
47+
response = await ac.post(
48+
"/events",
49+
headers={"content-type": "application/cloudevents+json; charset=UTF-8"},
50+
content=fake_event.model_dump_json(),
51+
)
52+
svc.book_created_event_handler.assert_called_once()
4853
assert response.status_code == 204
4954

5055

0 commit comments

Comments
 (0)