BasicCalculator / MathEngine.py
Abhlash's picture
Update MathEngine.py
c74350c verified
raw
history blame
764 Bytes
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class CalculationRequest(BaseModel):
a: float
b: float
operation: str
@app.post("/calculate")
def calculate(request: CalculationRequest):
a = request.a
b = request.b
operation = request.operation.lower()
if operation == "add":
result = a + b
elif operation == "subtract":
result = a - b
elif operation == "multiply":
result = a * b
elif operation == "divide":
if b == 0:
raise HTTPException(status_code=400, detail="Division by zero is not allowed")
result = a / b
else:
raise HTTPException(status_code=400, detail="Invalid operation")
return {"result": result}