GalvaoFilho commited on
Commit
3aa94c4
·
verified ·
1 Parent(s): 79d623b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import random
4
+
5
+ app = FastAPI(title="PokeAula API")
6
+
7
+ # Liberando CORS para que o React (localhost ou netlify/vercel) consiga acessar
8
+ app.add_middleware(
9
+ CORSMiddleware,
10
+ allow_origins=["*"],
11
+ allow_methods=["*"],
12
+ allow_headers=["*"],
13
+ )
14
+
15
+ # Simulando um Banco de Dados de Pokémons
16
+ # Estruturado para praticar acesso a propriedades: pokemon.stats.hp
17
+ pokedex = [
18
+ {
19
+ "id": 1,
20
+ "name": "Bulbasaur",
21
+ "type": ["Gramas", "Veneno"],
22
+ "image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png",
23
+ "stats": {"hp": 45, "attack": 49, "defense": 49}
24
+ },
25
+ {
26
+ "id": 4,
27
+ "name": "Charmander",
28
+ "type": ["Fogo"],
29
+ "image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png",
30
+ "stats": {"hp": 39, "attack": 52, "defense": 43}
31
+ },
32
+ {
33
+ "id": 7,
34
+ "name": "Squirtle",
35
+ "type": ["Água"],
36
+ "image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/7.png",
37
+ "stats": {"hp": 44, "attack": 48, "defense": 65}
38
+ },
39
+ {
40
+ "id": 25,
41
+ "name": "Pikachu",
42
+ "type": ["Elétrico"],
43
+ "image": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/25.png",
44
+ "stats": {"hp": 35, "attack": 55, "defense": 40}
45
+ }
46
+ ]
47
+
48
+ @app.get("/pokemon")
49
+ def list_all():
50
+ """Retorna todos para o primeiro fetch da aula."""
51
+ return pokedex
52
+
53
+ @app.get("/pokemon/{id}")
54
+ def get_one(id: int):
55
+ """Para testar async/await com parâmetros e erro 404."""
56
+ pokemon = next((p for p in pokedex if p["id"] == id), None)
57
+ if not pokemon:
58
+ raise HTTPException(status_code=404, detail="Pokémon não encontrado na base de dados da aula!")
59
+ return pokemon
60
+
61
+ @app.get("/battle-check")
62
+ def battle():
63
+ """Rota instável para praticar try/catch (erro 500 aleatório)."""
64
+ if random.random() < 0.5:
65
+ raise HTTPException(status_code=500, detail="A batalha caiu! Tente novamente.")
66
+ return {"status": "Vitória!", "xp_ganho": 150}