-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
26 lines (21 loc) · 862 Bytes
/
main.py
File metadata and controls
26 lines (21 loc) · 862 Bytes
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
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Calculator(BaseModel):
first_num: float
operation: str
second_num: float
@app.post("/calculate")
def calculate(arguments: Calculator):
if arguments.operation == "+":
return {"Addition result": arguments.first_num + arguments.second_num}
if arguments.operation == "-":
return {"Subtraction result": arguments.first_num - arguments.second_num}
if arguments.operation == "*":
return {"Multiplication result": arguments.first_num * arguments.second_num}
if arguments.operation == "/":
if arguments.second_num == 0:
return {"Can't divide by 0"}
else:
return {"Division result": arguments.first_num / arguments.second_num}
return {"You used the wrong operator, expected: +, -, * or /"}