Creating stubs and defining schemas
This commit is contained in:
23
back/schemas.py
Normal file
23
back/schemas.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class PeopleCreate(BaseModel):
|
||||||
|
name: str
|
||||||
|
age: int
|
||||||
|
gender: str
|
||||||
|
country: str
|
||||||
|
|
||||||
|
|
||||||
|
class AverageAgeByCountry(BaseModel):
|
||||||
|
country: str
|
||||||
|
average_age: float
|
||||||
|
|
||||||
|
|
||||||
|
class CountByCountry(BaseModel):
|
||||||
|
country: str
|
||||||
|
people_count: int
|
||||||
|
|
||||||
|
|
||||||
|
class GenderRepartition(BaseModel):
|
||||||
|
female_proportion: float
|
||||||
|
male_proportion: float
|
||||||
43
main.py
Normal file
43
main.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from back.schemas import PeopleCreate, AverageAgeByCountry, CountByCountry, GenderRepartition
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/people")
|
||||||
|
async def create_people(people_list: PeopleCreate) -> dict:
|
||||||
|
return {
|
||||||
|
"message": "People list has been succesfully imported",
|
||||||
|
"records_created": 12
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/average_age", response_model=List[AverageAgeByCountry])
|
||||||
|
async def read_average_age(name: str):
|
||||||
|
return [
|
||||||
|
{'country': 'FR', 'average_age': 22.7},
|
||||||
|
{'country': 'BE', 'average_age': 14.7},
|
||||||
|
{'country': 'DE', 'average_age': 27.7},
|
||||||
|
{'country': 'IT', 'average_age': 32.7},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/count_by_country", response_model=List[CountByCountry])
|
||||||
|
async def read_count_by_country():
|
||||||
|
return [
|
||||||
|
{'country': 'FR', 'people_count': 2},
|
||||||
|
{'country': 'BE', 'people_count': 4},
|
||||||
|
{'country': 'DE', 'people_count': 3},
|
||||||
|
{'country': 'IT', 'people_count': 7},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/count_by_gender/{country}", response_model=GenderRepartition)
|
||||||
|
async def read_count_by_gender(country: str):
|
||||||
|
return {
|
||||||
|
"female_proportion": 51.2,
|
||||||
|
"male_proportion": 48.8
|
||||||
|
}
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
fastapi==0.89.1
|
||||||
Reference in New Issue
Block a user