forked from luizalabs/tutorial-python-brasil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
39 lines (27 loc) · 945 Bytes
/
main.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
import json
import os
from pathlib import Path
from random import random
from fastapi import FastAPI, HTTPException
from fastapi import status
from fastapi.responses import JSONResponse
DATA_DIR = Path(os.path.dirname(__file__)) / "data"
FAIL_RATE = int(os.getenv("FAIL_RATE", 0))
app = FastAPI()
@app.middleware("http")
async def controlled_fail_middleware(request, call_next):
if FAIL_RATE / 100 > random():
return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
return await call_next(request)
def read_data(file_):
return json.load(open(file_))
@app.get("/catalogs")
async def catalog_list():
files = DATA_DIR.glob("*.json")
return [read_data(f_) for f_ in files]
@app.get("/catalogs/{code}")
async def catalog_retrieve(code):
try:
return read_data(DATA_DIR / f"{code}.json")
except FileNotFoundError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)