-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmock.py
More file actions
54 lines (42 loc) · 1.34 KB
/
Copy pathmock.py
File metadata and controls
54 lines (42 loc) · 1.34 KB
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
# support all faker methods
# https://faker.readthedocs.io/en/master/locales/zh_CN.html
# USAGE:
# export FLASK_APP=mock.py flask run
# windows: set FLASK_APP=mock.py flask run
from flask import Flask, jsonify, request
from faker import Faker
import json
app = Flask(__name__)
fake = Faker('zh_CN')
def load_config(file_name="./config.json"):
try:
with open(file_name) as f:
return json.loads(f.read())
except:
exit("can not load config file")
conf = load_config()
@app.route('/')
@app.route('/<path:url_path>')
def index(url_path="", methods=['GET', 'POST']):
path = "/" + url_path
if path in conf and request.method == conf[path]['method'].upper() :
return jsonify(build_response(conf[path]['response']))
return "/%s is not defined " %(url_path)
def build_response(res):
size = int(res['length'])
if size == 1:
response = build_obj(res['content'])
else:
response = []
for i in range(0, size):
response.append(build_obj(res['content']))
return response
def build_obj(content):
obj = {}
for each in [item for item in content.split(';')]:
k, v = each.split(':')
if '(' in v and ')' in v:
obj[k] = eval("fake.%s" %(v))
else:
obj[k] = eval("fake.%s()" %(v))
return obj