-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresfuldemo.py
42 lines (32 loc) · 1 KB
/
resfuldemo.py
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
38
39
40
41
from flask import Flask,request,jsonify
from flask_restful import Resource, Api
myapp=Flask(__name__)
myapi=Api(myapp)
#making class for a particular resource
#get and post mthods correpond to get and pot requests
# automaticall mapped by flask_restful
class Hello(Resource):
def get(self):
return jsonify({'message':"hello word"})
def post(self):
data=request.get_json()
return jsonify({'data':data})
class Square(Resource):
def get(self, num):
if num%2==0:
status="even"
else:
status="odd"
return jsonify({'square': num**2,'status':status})
class even_odd(Resource):
def get(self,num):
if num%2==0:
status="even"
else:
status="odd"
return jsonify({'value':num,'status':status})
myapi.add_resource(Hello,'/hello')
myapi.add_resource(even_odd,'/pair/<int:num>')
myapi.add_resource(Square, '/square/<int:num>')
if __name__=="__main__":
myapp.run(debug=True)