1
1
import typing
2
- from typing import Any , Dict , List
2
+ from typing import Dict , List
3
3
4
4
from fastapi import APIRouter , Body , Header , HTTPException
5
+ from fastapi .openapi .models import Example
5
6
from pydantic import BaseModel
6
7
7
8
from domains .books .events import BookCreatedV1
9
+ from domains .books .service import BookService
8
10
9
11
router = APIRouter (prefix = "/events" )
10
12
@@ -22,7 +24,7 @@ def _event_registry() -> Dict[str, typing.Type[BaseModel]]:
22
24
}
23
25
24
26
25
- def _event_schema_examples () -> dict [str , dict [ str , Any ] ]:
27
+ def _event_schema_examples () -> dict [str , Example ]:
26
28
missing_example_message = (
27
29
"No example has been added to this event but you can"
28
30
" still explore the event schema. (Ask the developer"
@@ -31,11 +33,11 @@ def _event_schema_examples() -> dict[str, dict[str, Any]]:
31
33
)
32
34
33
35
return {
34
- k : {
35
- " value" : getattr (v , "model_config" , {})
36
+ k : Example (
37
+ value = getattr (v , "model_config" , {})
36
38
.get ("json_schema_extra" , {})
37
39
.get ("examples" , [missing_example_message ])[0 ]
38
- }
40
+ )
39
41
for k , v in _event_registry ().items ()
40
42
}
41
43
@@ -67,15 +69,6 @@ async def event_schema_list() -> List[str]:
67
69
68
70
@router .post (
69
71
"" ,
70
- openapi_extra = {
71
- "requestBody" : {
72
- "content" : {
73
- "application/cloudevents+json; charset=UTF-8" : {
74
- "examples" : _event_schema_examples (),
75
- }
76
- },
77
- },
78
- },
79
72
status_code = 204 ,
80
73
description = """
81
74
Entrypoint for CloudEvent processing, it supports only single events.
@@ -86,10 +79,12 @@ async def event_schema_list() -> List[str]:
86
79
async def submit_event (
87
80
event_data : _EVENTS_UNION_TYPE = Body (
88
81
media_type = "application/cloudevents+json; charset=UTF-8" ,
82
+ openapi_examples = _event_schema_examples (),
89
83
discriminator = "type" ,
90
84
),
91
85
content_type : typing .Literal [
92
86
"application/cloudevents+json; charset=UTF-8"
93
87
] = Header (),
94
88
) -> 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 )
0 commit comments