2022-05-14 06:59:59 -05:00
|
|
|
from typing import Union
|
2020-06-28 20:13:30 +02:00
|
|
|
|
2018-12-18 21:59:06 +04:00
|
|
|
from fastapi import Depends, FastAPI
|
|
|
|
|
|
2018-12-15 18:15:42 +04:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
|
|
|
|
|
|
|
|
|
|
|
2018-12-23 21:21:37 +04:00
|
|
|
class CommonQueryParams:
|
2022-05-14 06:59:59 -05:00
|
|
|
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
|
2018-12-23 21:21:37 +04:00
|
|
|
self.q = q
|
|
|
|
|
self.skip = skip
|
|
|
|
|
self.limit = limit
|
2018-12-15 18:15:42 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/items/")
|
2018-12-23 21:21:37 +04:00
|
|
|
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
|
2018-12-15 18:15:42 +04:00
|
|
|
response = {}
|
|
|
|
|
if commons.q:
|
|
|
|
|
response.update({"q": commons.q})
|
2018-12-28 16:10:29 +04:00
|
|
|
items = fake_items_db[commons.skip : commons.skip + commons.limit]
|
2018-12-15 18:15:42 +04:00
|
|
|
response.update({"items": items})
|
|
|
|
|
return response
|