-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
79 lines (52 loc) · 1.85 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import argparse
import json
import os
import uuid
from flask import Flask, request, abort
from flask_restful import Resource, Api
parser = argparse.ArgumentParser(description="Parameters for flask-mocker")
parser.add_argument('-p', '--port', help='the port number for the server to run')
parser.add_argument('file', help='the name of your JSON data file')
parser.add_argument('--debug', dest='debug', action='store_true',
help='this runs the app in debug mode')
parser.set_defaults(debug=False, port=5000)
args = parser.parse_args()
filename = args.file
app = Flask(__name__)
api = Api(app)
if not os.path.exists(filename):
raise FileNotFoundError("File '{}' not found!".format(filename))
with open(filename, 'r') as f:
data = json.load(f)
class Item(Resource):
def get(self, category, id_):
if id_ not in data[category]:
abort(404)
return data[category][id_]
def put(self, category, id_):
if id_ not in data[category]:
abort(404)
put_data = request.form.to_dict()
data[category][id_].update(put_data)
return data[category][id_]
def delete(self, category, id_):
del data[category][id_]
return {'message': 'Delete Successful!'}
class Category(Resource):
def get(self, category):
if category not in data:
abort(404)
return data[category]
def post(self, category):
id_ = str(uuid.uuid4())
post_data = request.form.to_dict()
data[category][id_] = post_data
return {id_: data[category][id_]}
class Categories(Resource):
def get(self):
return data
api.add_resource(Categories, '/')
api.add_resource(Category, '/<string:category>')
api.add_resource(Item, '/<string:category>/<string:id_>')
if __name__ == '__main__':
app.run(port=args.port, debug=args.debug)