-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
52 lines (40 loc) · 962 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import (
FastAPI,
status,
)
from pydantic import (
BaseModel,
ValidationError,
)
from models import (
Pokemon,
PokemonType,
)
class PokemonSchema(BaseModel):
pokemon_type: int
national_index: int
name: str
app = FastAPI()
@app.get("/")
async def index():
return {"message": "welcome to my awesome pokedex"}
@app.post(
"/pokemon",
response_model=PokemonSchema,
status_code=status.HTTP_201_CREATED
)
async def pokemon(pokemon: PokemonSchema):
try:
new_pokemon = Pokemon(
pokemon_type=PokemonType(pokemon.pokemon_type),
national_index=pokemon.national_index,
name=pokemon.name
)
except ValidationError as e:
print(e)
response_pokemon = PokemonSchema(
pokemon_type=new_pokemon.pokemon_type,
national_index=new_pokemon.national_index,
name=new_pokemon.name
)
return response_pokemon