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
@@ -23,7 +24,7 @@ def _event_registry() -> Dict[str, typing.Type[BaseModel]]:
23
24
}
24
25
25
26
26
- def _event_schema_examples () -> dict [str , dict [ str , Any ] ]:
27
+ def _event_schema_examples () -> dict [str , Example ]:
27
28
missing_example_message = (
28
29
"No example has been added to this event but you can"
29
30
" still explore the event schema. (Ask the developer"
@@ -32,11 +33,11 @@ def _event_schema_examples() -> dict[str, dict[str, Any]]:
32
33
)
33
34
34
35
return {
35
- k : {
36
- " value" : getattr (v , "model_config" , {})
36
+ k : Example (
37
+ value = getattr (v , "model_config" , {})
37
38
.get ("json_schema_extra" , {})
38
39
.get ("examples" , [missing_example_message ])[0 ]
39
- }
40
+ )
40
41
for k , v in _event_registry ().items ()
41
42
}
42
43
@@ -85,6 +86,5 @@ async def submit_event(
85
86
"application/cloudevents+json; charset=UTF-8"
86
87
] = Header (),
87
88
) -> None :
88
- # A better approach than if/else should be used when we have multiple event types
89
- if isinstance (event_data , BookCreatedV1 ):
90
- await BookService ().book_created_event_handler (event_data .data .book_id )
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