Skip to content

api

API of fair-python-cookiecutter-demo.

calc

calc(op: CalcOperation, x: int, y: int = 0)

Return result of calculation on two integers.

Source code in src/fair_python_cookiecutter_demo/api.py
10
11
12
13
14
15
16
17
18
19
20
21
@app.get("/calculate/{op}")
def calc(op: CalcOperation, x: int, y: int = 0):
    """Return result of calculation on two integers."""
    try:
        return calculate(op, x, y)

    except (ZeroDivisionError, ValueError, NotImplementedError) as e:
        if isinstance(e, ZeroDivisionError):
            err = f"Cannot divide x={x} by y=0!"
        else:
            err = str(e)
        raise HTTPException(status_code=422, detail=err)