-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock-server.py
executable file
·126 lines (107 loc) · 2.86 KB
/
mock-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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
from flask import Flask, request, render_template
from flask_restful import Resource, Api
from md5 import md5
from copy import deepcopy
projects_data = [
{
'id': md5('sample').hexdigest(),
'url': '/projects/' + md5('sample').hexdigest(),
'name': 'sample',
'resources': [
{
'id': md5('/pom.xml').hexdigest(),
'name': 'pom.xml',
'path': '/'
},
{
'id': md5('/src/').hexdigest(),
'name': 'src',
'path': '/src/'
},
{
'id': md5('/src/Main.java').hexdigest(),
'name': 'Main.java',
'path': '/src/'
}
]
},
{
'id': md5('sample2').hexdigest(),
'url': '/projects/' + md5('sample2').hexdigest(),
'name': 'sample2'
}
]
builds_data = [
{
'id': md5('sample-build-1').hexdigest(),
'status': 'scheduled'
},
{
'id': md5('sample2-build-2').hexdigest(),
'status': 'running'
},
{
'id': md5('sample2-build-1').hexdigest(),
'status': 'done'
}
]
app = Flask(__name__)
api = Api(app)
@app.route("/tests.html")
def test():
print(app.root_path)
return render_template('tests.html')
@api.resource('/projects/')
class ProjectListAPI(Resource):
def get(self):
return {
'items': [project_simplify(project) for project in projects_data]
}
@api.resource('/projects/<string:project_id>')
class ProjectAPI(Resource):
def get(self, project_id):
return project_simplify(project_find(project_id))
@api.resource('/projects/<string:project_id>/build')
class ProjectBuildAPI(Resource):
def post(self, project_id):
return {
'id': md5('sample-build-1').hexdigest(),
'status': 'scheduled'
}
@api.resource('/projects/<string:project_id>/resources')
class ResourceListAPI(Resource):
def get(self, project_id):
return {
'items': project_find(project_id)['resources']
}
@api.resource('/projects/<string:project_id>/resources/<string:resource_id>')
class ResourceAPI(Resource):
def get(self, project_id, resource_id):
return resource_find(project_id, resource_id)
@api.resource('/builds/')
class BuildListAPI(Resource):
def get(self):
return {
'items': builds_data
}
@api.resource('/builds/<string:build_id>')
class BuildAPI(Resource):
def get(self, build_id):
return build_find(build_id)
def project_simplify(project):
return {
'id': project['id'],
'url': project['url'],
'name': project['name']
}
def project_find(project_id):
return find(project_id, projects_data)
def find(element_id, collection):
return next(element for element in collection if element['id'] == element_id)
def resource_find(project_id, resource_id):
return next(resource for resource in project_find(project_id)['resources'] if resource['id'] == resource_id)
def build_find(build_id):
return find(build_id, builds_data)
if __name__ == "__main__":
app.run(debug=True)