pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
478 B
22 lines
478 B
from typing import Optional
|
|
|
|
from fastapi import Cookie, Depends, FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def query_extractor(q: Optional[str] = None):
|
|
return q
|
|
|
|
|
|
def query_or_cookie_extractor(
|
|
q: str = Depends(query_extractor), last_query: Optional[str] = Cookie(default=None)
|
|
):
|
|
if not q:
|
|
return last_query
|
|
return q
|
|
|
|
|
|
@app.get("/items/")
|
|
async def read_query(query_or_default: str = Depends(query_or_cookie_extractor)):
|
|
return {"q_or_cookie": query_or_default}
|
|
|