I am working on this simple FastAPI example and I would like to replace the read_books
arguments with an equivalent TypedDict
, like the commented line.
Is it possible?
from fastapi import FastAPI, Query
from typing import Optional, TypedDict
# would like to define model for query parameter in separeted object
class GetModel(TypedDict):
test: Optional[str]
query_param = GetModel(test=Query('default value', max_length=10, title='titulo', description="description 123", regex="^[a-z]+"))
app = FastAPI()
@app.get("/books")
def read_books(test: Optional[str] = Query('default value', max_length=10, title='titulo', description="description 123", regex="^[a-z]+")):
#def read_books(query_param):
"""
comentario do endpoint
"""
results = {"books": [{"book_name": "The Great Hunt"}, {"book_name": "The Dragon Reborn"}]}
if test:
results.update({"test": {"name":test}})
return results