-
-
Save crakernano/11812a4df3b31e9d744ef5bec14018c3 to your computer and use it in GitHub Desktop.
Crear un endpoint con paginado y filtros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"age": 17, | |
"name": "Christopher Whitaker", | |
"location": "Antarctica (the territory South of 60 deg S)", | |
"birth": "1962-12-13", | |
"empid": 53 | |
}, | |
{ | |
"age": 11, | |
"name": "Tony Good", | |
"location": "Liberia", | |
"birth": "1930-08-02", | |
"empid": 77 | |
}, | |
{ | |
"age": 17, | |
"name": "Stephen Reed", | |
"location": "Hong Kong", | |
"birth": "1970-10-02", | |
"empid": 136 | |
}, | |
{ | |
"age": 13, | |
"name": "David Duncan", | |
"location": "Costa Rica", | |
"birth": "1971-10-05", | |
"empid": 138 | |
}, | |
{ | |
"age": 16, | |
"name": "Richard Morales", | |
"location": "Hong Kong", | |
"birth": "1958-03-29", | |
"empid": 308 | |
}, | |
{ | |
"age": 20, | |
"name": "Christopher Ramirez", | |
"location": "Burkina Faso", | |
"birth": "1909-11-10", | |
"empid": 362 | |
}, | |
{ | |
"age": 11, | |
"name": "Laura Peck", | |
"location": "Jordan", | |
"birth": "2018-06-15", | |
"empid": 402 | |
}, | |
{ | |
"age": 15, | |
"name": "Raymond Pierce", | |
"location": "Congo", | |
"birth": "1909-10-03", | |
"empid": 408 | |
}, | |
{ | |
"age": 13, | |
"name": "Jillian Clark", | |
"location": "Burundi", | |
"birth": "1950-04-30", | |
"empid": 421 | |
}, | |
{ | |
"age": 19, | |
"name": "Jill Jackson", | |
"location": "Argentina", | |
"birth": "1940-04-10", | |
"empid": 691 | |
}, | |
{ | |
"age": 16, | |
"name": "Hector Adams", | |
"location": "China", | |
"birth": "1947-03-20", | |
"empid": 825 | |
}, | |
{ | |
"age": 11, | |
"name": "Jessica Robinson", | |
"location": "Mauritius", | |
"birth": "2005-01-12", | |
"empid": 867 | |
}, | |
{ | |
"age": 13, | |
"name": "Nathaniel Parker", | |
"location": "Faroe Islands", | |
"birth": "1957-04-18", | |
"empid": 1006 | |
}, | |
{ | |
"age": 10, | |
"name": "Erin Bishop", | |
"location": "Lithuania", | |
"birth": "1981-10-29", | |
"empid": 1064 | |
}, | |
{ | |
"age": 18, | |
"name": "Vernon Wade", | |
"location": "Congo", | |
"birth": "1968-12-29", | |
"empid": 1118 | |
}, | |
{ | |
"age": 17, | |
"name": "Gerald Robinson", | |
"location": "Holy See (Vatican City State)", | |
"birth": "1962-05-31", | |
"empid": 1155 | |
}, | |
{ | |
"age": 13, | |
"name": "Michael Chandler", | |
"location": "Malawi", | |
"birth": "1922-06-06", | |
"empid": 1255 | |
}, | |
{ | |
"age": 18, | |
"name": "David Miller", | |
"location": "Estonia", | |
"birth": "1993-08-24", | |
"empid": 1285 | |
}, | |
{ | |
"age": 15, | |
"name": "Christopher Garcia", | |
"location": "Netherlands Antilles", | |
"birth": "1944-10-21", | |
"empid": 1383 | |
}, | |
{ | |
"age": 18, | |
"name": "Donna Valdez", | |
"location": "Rwanda", | |
"birth": "1956-08-26", | |
"empid": 1473 | |
} | |
] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.3' | |
services: | |
filter-api: | |
container_name: filters | |
build: . | |
ports: | |
- 80:8000 | |
volumes: | |
- .:/app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 | |
EXPOSE 8000 | |
COPY . /app | |
RUN pip install --no-cache-dir --upgrade -r requirements.txt | |
CMD ["uvicorn", "app.main:app","--reload", "--host", "0.0.0.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Union, Optional | |
from fastapi import FastAPI | |
from fastapi_pagination import Page, paginate, add_pagination | |
from pydantic import BaseModel, Field | |
import json | |
app = FastAPI() | |
f = open('data.json') | |
data = json.load(f) | |
class UserOut(BaseModel): # define your model | |
name: str = Field(..., example="Steve") | |
location: str = Field(..., example="Madrid") | |
birth: str = Field(..., example="1 January 1970") | |
age: int = Field() | |
def filter_user(query, data): | |
print(f"Filtrado : {query}") | |
results = [] | |
for i in data: | |
if(str(i["age"]) == query): | |
results.append(i) | |
return paginate(results) | |
@app.get("/") | |
def read_root(): | |
return {"Hello": "World"} | |
@app.get("/data", response_model=Page[UserOut]) | |
async def get_all_data(age: Union[str, None] = None): | |
if age: | |
return filter_user(age, data) | |
return paginate(data) | |
add_pagination(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastapi | |
uvicorn | |
requests | |
pytest | |
python-multipart | |
sqlmodel==0.0.4 | |
psycopg2 | |
SQLAlchemy==1.4.46 | |
gunicorn==20.0.4 | |
starlette==0.14.2 | |
fastapi-versioning==0.10.0 | |
python-semantic-release==7.32.2 | |
aiofiles==22.1.0 | |
fastapi-pagination |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment