Coverage for src/fair_python_cookiecutter_demo/api.py: 87%
15 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-01 13:35 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2024-08-01 13:35 +0000
1"""API of fair-python-cookiecutter-demo."""
3from fastapi import FastAPI, HTTPException
5from fair_python_cookiecutter_demo.lib import CalcOperation, calculate
7app = FastAPI()
10@app.get("/calculate/{op}")
11def calc(op: CalcOperation, x: int, y: int = 0):
12 """Return result of calculation on two integers."""
13 try:
14 return calculate(op, x, y)
16 except (ZeroDivisionError, ValueError, NotImplementedError) as e:
17 if isinstance(e, ZeroDivisionError):
18 err = f"Cannot divide x={x} by y=0!"
19 else:
20 err = str(e)
21 raise HTTPException(status_code=422, detail=err)
24def run():
25 import uvicorn
27 uvicorn.run(app, host="127.0.0.1", port=8000)