| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
from fastapi import Depends, FastAPI |
| 4 |
|
| 5 |
app = FastAPI() |
| 6 |
|
| 7 |
|
| 8 |
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] |
| 9 |
|
| 10 |
|
| 11 |
class CommonQueryParams: |
| 12 |
def __init__(self, q: str | None = None, skip: int = 0, limit: int = 100): |
| 13 |
self.q = q |
| 14 |
self.skip = skip |
| 15 |
self.limit = limit |
| 16 |
|
| 17 |
|
| 18 |
@app.get("/items/") |
| 19 |
async def read_items(commons: Annotated[CommonQueryParams, Depends(CommonQueryParams)]): |
| 20 |
response = {} |
| 21 |
if commons.q: |
| 22 |
response.update({"q": commons.q}) |
| 23 |
items = fake_items_db[commons.skip : commons.skip + commons.limit] |
| 24 |
response.update({"items": items}) |
| 25 |
return response |