2

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
Chris
  • 18,724
  • 6
  • 46
  • 80
DevBush
  • 383
  • 1
  • 3
  • 10
  • Why don't you get the test from JSON body? I mean, your input is object; so why are you trying to get that object as query parameter? – stuck Feb 02 '22 at 15:55

0 Answers0