Skip to content

Commit

Permalink
feat: add application in app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
johschmidt42 committed Sep 25, 2022
1 parent 81e73e7 commit 3f07683
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/example_app/app.py
@@ -0,0 +1,31 @@
import httpx
import uvicorn
from fastapi import FastAPI, HTTPException, Path, status
from httpx import Response

# app
app: FastAPI = FastAPI()


@app.get(path="/{number}", status_code=status.HTTP_200_OK)
def get_pokemon(
number: int = Path(title="The Pokémon to get (based on number)", ge=1, le=151)
) -> dict:
pokemon_url: str = f"https://pokeapi.co/api/v2/pokemon/{number}"

try:
response: Response = httpx.get(url=pokemon_url)
except Exception:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Could not send a request to {pokemon_url}",
)

if response.status_code == status.HTTP_200_OK:
return response.json()
else:
raise HTTPException(status_code=response.status_code, detail=response.text)


if __name__ == "__main__":
uvicorn.run(app="app:app", host="127.0.0.1", port=9000, reload=True)

0 comments on commit 3f07683

Please sign in to comment.