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