forked from talkpython/100daysofweb-with-python-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
37 lines (29 loc) · 873 Bytes
/
Copy pathmain.py
File metadata and controls
37 lines (29 loc) · 873 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
27
28
29
30
31
32
33
34
35
36
37
import fastapi
import uvicorn
from typing import Optional
api = fastapi.FastAPI()
@api.get("/")
def index():
body="<html>"\
"<body style='padding: 10px;'>"\
"<h1>Welcome to the API </h1>"\
"<div>"\
"Try it: <a href='/api/calculate?x=8&y=9&z=10'>/api/calculate?x=8&y=9&z=10</a>"\
"</div>"\
"</body>"\
"</html>"
return fastapi.responses.HTMLResponse(content=body)
@api.get('/api/calculate')
def calculate(x:int, y:int, z: Optional[int] = None):
if z==0:
return fastapi.responses.JSONResponse(content={"Error": "Z cannot be zero"},
status_code=400)
value = (x + y)
if z is not None:
value /= z
return {
'x': x,
'y': y,
'z': z,
'value': value}
uvicorn.run(api, host='0.0.0.0', port=8000)