-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrest_server.py
44 lines (31 loc) · 1.19 KB
/
rest_server.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
42
43
44
from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
import json
app = Flask(__name__)
api = Api(app)
class Employees(Resource):
def get(self):
return json.dumps({'employees': ['david','lili','jack']} )
class Tracks(Resource):
def get(self):
result = {1:'david', 2:'lili', 3:'jack'}
return json.dumps(result)
class Employees_Name(Resource):
def get(self, employee_id):
employees = {1:'david', 2:'lili', 3:'jack'}
employee_id = int(employee_id)
if(employee_id in employees):
return employees[employee_id]
else:
return 'no_employee_with_id_{0}'.format(employee_id)
api.add_resource(Employees, '/employees') # Route_1
api.add_resource(Tracks, '/tracks') # Route_2
api.add_resource(Employees_Name, '/employees/<employee_id>') # Route_3
if __name__ == '__main__':
app.run(host="0.0.0.0", port='5002')
'''
http://127.0.0.1:5002/employees shows ids of all the employees
http://127.0.0.1:5002/tracks shows tracks details
http://127.0.0.1:5002/employees/3 shows details of employee whose employeeid is 3
'''