๐ง-๐¶
๐ ๐ช โ ๐ ๐ โ๏ธ ๐ง-๐.
๐ซ ๐ช โฌ ๐ ๐ช ๐ซ.
FastAPI ๐ โ ๐ ๐ฌ ๐ซ.
๐ฅ ๐ "โ"¶
๐ ๐ช โ ๐ฅ ๐ ("โ") ๐:
from typing import Union
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: Union[str, None] = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor),
last_query: Union[str, None] = 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}
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: str | None = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor), last_query: str | None = 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}
โซ๏ธ ๐ฃ ๐ฆ ๐ข ๐ข q
str
, & โคด๏ธ โซ๏ธ ๐จ โซ๏ธ.
๐ ๐ (๐ซ ๐ถ โ ), โ๏ธ ๐ โน ๐ฅ ๐ฏ ๐ โ ๐ง-๐ ๐ท.
๐ฅ ๐, "โ" & "โ๏ธ"¶
โคด๏ธ ๐ ๐ช โ โ1๏ธโฃ ๐ ๐ข ("โ") ๐ ๐ ๐ฐ ๐ฃ ๐ ๐ฎ ๐ (โซ๏ธ "โ๏ธ" ๐โโ๏ธ):
from typing import Union
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: Union[str, None] = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor),
last_query: Union[str, None] = 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}
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: str | None = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor), last_query: str | None = 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}
โก๏ธ ๐ฏ ๐ ๐ข ๐ฃ:
- โ๏ธ ๐ ๐ข ๐ ("โ") โซ๏ธ, โซ๏ธ ๐ฃ โ1๏ธโฃ ๐ (โซ๏ธ "๐ช" ๐ ๐ณ ๐).
- โซ๏ธ ๐ช ๐
query_extractor
, & ๐ ๏ธ ๐ฒ ๐จ โซ๏ธ ๐ขq
.
- โซ๏ธ ๐ช ๐
- โซ๏ธ ๐ฃ ๐ฆ
last_query
๐ช,str
.- ๐ฅ ๐ฉโ๐ป ๐ซ ๐ ๐ ๐ข
q
, ๐ฅ โ๏ธ ๐ ๐ข โ๏ธ, โ ๐ฅ ๐ ๐ช โญ.
- ๐ฅ ๐ฉโ๐ป ๐ซ ๐ ๐ ๐ข
โ๏ธ ๐¶
โคด๏ธ ๐ฅ ๐ช โ๏ธ ๐ โฎ๏ธ:
from typing import Union
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: Union[str, None] = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor),
last_query: Union[str, None] = 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}
from fastapi import Cookie, Depends, FastAPI
app = FastAPI()
def query_extractor(q: str | None = None):
return q
def query_or_cookie_extractor(
q: str = Depends(query_extractor), last_query: str | None = 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}
Info
๐ ๐ ๐ฅ ๐ด ๐ฃ 1๏ธโฃ ๐ โก ๐ ๏ธ ๐ข, query_or_cookie_extractor
.
โ๏ธ FastAPI ๐ ๐ญ ๐ โซ๏ธ โ๏ธ โ query_extractor
๐ฅ, ๐ถโโ๏ธ ๐ ๐ query_or_cookie_extractor
โช ๐ค โซ๏ธ.
graph TB
query_extractor(["query_extractor"])
query_or_cookie_extractor(["query_or_cookie_extractor"])
read_query["/items/"]
query_extractor --> query_or_cookie_extractor --> read_query
โ๏ธ ๐ ๐ ๐ ๐ฐ¶
๐ฅ 1๏ธโฃ ๐ ๐ ๐ฃ ๐ ๐ฐ ๐ โก ๐ ๏ธ, ๐ผ, ๐ ๐ โ๏ธ โ ๐ง-๐, FastAPI ๐ ๐ญ ๐ค ๐ ๐ง-๐ ๐ด ๐ ๐ ๐จ.
& โซ๏ธ ๐ ๐ ๐จ ๐ฒ "๐พ" & ๐ถโโ๏ธ โซ๏ธ ๐ "โ๏ธ" ๐ ๐ช โซ๏ธ ๐ ๐ฏ ๐จ, โฉ๏ธ ๐ค ๐ ๐ ๐ฐ ๐ ๐จ.
๐ง ๐ ๐โ ๐ ๐ญ ๐ ๐ช ๐ ๐ค ๐ ๐ (๐ฒ ๐ ๐ฐ) ๐ ๐จ โฉ๏ธ โ๏ธ "๐พ" ๐ฒ, ๐ ๐ช โ ๐ข use_cache=False
๐โ โ๏ธ Depends
:
async def needy_dependency(fresh_value: str = Depends(get_value, use_cache=False)):
return {"fresh_value": fresh_value}
๐¶
โ๏ธ โช๏ธโก๏ธ ๐ ๐ ๐ค โ๏ธ ๐ฅ, ๐ ๐ โ๏ธ ๐ .
๐ข ๐ ๐ ๐ โก ๐ ๏ธ ๐ข.
โ๏ธ, โซ๏ธ ๐ถ ๐๏ธ, & โ ๐ ๐ฃ ๐ฒ ๐ ๐ฆ ๐ "๐" (๐ฒ).
Tip
๐ ๐ ๐ช ๐ซ ๐ โ โฎ๏ธ ๐ซ ๐ ๐ผ.
โ๏ธ ๐ ๐ ๐ โ โ โซ๏ธ ๐ ๐ ๐โโ.
& ๐ ๐ ๐ ๐ธ ๐ โซ๏ธ ๐ ๐ ๐.