forked from mozilla/marketplace-api-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketfuel.py
120 lines (84 loc) · 3.42 KB
/
rocketfuel.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
"""
Rocketfuel is where the publishing tool lives.
"""
import json
from flask import Response
import app
DB = {
'pk': 0,
'collections': {},
}
def api_error(message, status_code=400):
return app.make_response(json.dumps(message), status_code)
@app.route('/api/v1/rocketfuel/collections/', methods=['GET', 'POST'])
def collections_list():
if app.request.method == 'POST':
form = app.request.form
DB['pk'] += 1
return {
'title': form.get('title'),
'description': form.get('description'),
'id': DB['pk'],
'apps': [],
'collection_type': form.get('collection_type'),
'category': form.get('category'),
'region': form.get('region'),
'carrier': form.get('carrier'),
}
def gen():
i = 0
while 1:
yield app.defaults.collection('Collection', 'collection-%d' % i)
i += 1
query = app.request.args.get('q')
data = app._paginated('objects', gen, 0 if query == 'empty' else 25)
return data
@app.route('/api/v1/rocketfuel/collections/<slug>/', methods=['GET', 'PATCH'])
def collections_get(slug):
return app.defaults.collection('Collection %s' % slug, slug)
def _validate_app(app_id, slug):
if not app_id:
return api_error({'detail': '`app` was not provided.'})
if app_id in DB['collections'].get(slug, []):
return api_error({'detail': '`app` already exists in collection.'})
@app.route('/api/v1/rocketfuel/collections/<slug>/add_app/', methods=['POST'])
def collections_add_app(slug):
app_id = app.request.form.get('app')
_validate_app(app_id, slug)
DB['collections'].setdefault(slug, []).append(app_id)
collection = app.defaults.collection('Collection %s' % slug, slug)
collection.update(apps=['/api/v1/apps/app/%s/' % app_id])
return collection
@app.route('/api/v1/rocketfuel/collections/<slug>/remove_app/',
methods=['POST'])
def collections_remove_app(slug):
app_id = app.request.form.get('app')
_validate_app(app_id, slug)
# Uncomment if you want things to behave as they would with the real API.
# if app_id not in DB['collections'].get(slug, []):
# return api_error({'detail': '`app` does not exist in collection.'})
try:
DB['collections'].get(slug, []).remove(app_id)
except ValueError:
pass
collection = app.defaults.collection('Collection %s' % slug, slug)
collection.update(apps=[])
return collection
@app.route('/api/v1/rocketfuel/collections/<slug>/reorder_app/',
methods=['POST'])
def collections_reorder_app(slug):
app_id = app.request.form.get('app')
position = app.request.form.get('position')
_validate_app(app_id, slug)
# Uncomment if you want things to behave as they would with the real API.
# if app_id not in DB['collections'].get(slug, []):
# return api_error({'detail': '`app` does not exist in collection.'})
if not position:
return api_error({'detail': '`position` was not provided.'})
return app.defaults.collection('Collection %s' % slug, slug)
@app.route('/api/v1/rocketfuel/collections/<slug>/image/', methods=['POST', 'GET'])
def collections_image(slug):
return Response(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej'
'3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5C'
'YII='.decode('base64'), mimetype='image/png')