forked from charles-42/flask_API_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_demo.py
More file actions
34 lines (23 loc) · 808 Bytes
/
api_demo.py
File metadata and controls
34 lines (23 loc) · 808 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
from flask import Flask, request, json
from constant.http_status_codes import *
api = Flask(__name__)
@api.route("/helloworld/")
def hello_get():
return {"data":"Hello Charles"}
@api.route("/helloworld/", methods=["POST"])
def hello_post():
return {"data":"posted"}
@api.route("/hellouser/<string:user>")
def hello_user(user):
return {"data":f"hello {user}"}
@api.route("/square", methods=["POST"])
def square():
number = request.form["number"]
return {"number": number, "square": int(number)**2}
@api.route("/hellouser_validator/<string:user>")
def hello_user_validator(user):
if not isinstance(user,str):
return {"error":"wrong user type"}, HTTP_400_BAD_REQUEST
return {"data":f"hello {user}"}, HTTP_200_OK
if __name__ == "__main__":
api.run(debug=True)