Skip to content

๐ŸŽง-๐Ÿ”—

๐Ÿ‘† ๐Ÿ’ช โœ ๐Ÿ”— ๐Ÿ‘ˆ โœ”๏ธ ๐ŸŽง-๐Ÿ”—.

๐Ÿ‘ซ ๐Ÿ’ช โฌ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘ซ.

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

๐ŸŒ ๐Ÿ‘‰ ๐Ÿ’ช ๐Ÿšซ ๐Ÿ˜‘ โš  โฎ๏ธ ๐Ÿ‘ซ ๐Ÿ™… ๐Ÿ–ผ.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โ” โš  โšซ๏ธ ๐Ÿ“ƒ ๐Ÿ”ƒ ๐Ÿ’‚โ€โ™‚.

& ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ’ธ ๐Ÿ“Ÿ โšซ๏ธ ๐Ÿ”œ ๐Ÿ–Š ๐Ÿ‘†.