forked from mozilla/marketplace-api-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
237 lines (181 loc) · 6.11 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Flue is a simple application which mocks out the APIs used by Fireplace.
Pointing your instance of Fireplace using settings.js will allow you to
quickly get up and running without needing your own installation of Zamboni
or without needing to use -dev (offline mode).
"""
import json
import random
from flask import make_response, request
import app
import defaults
import persona
import rocketfuel
import services
@app.route('/api/v1/account/login/', methods=['POST'])
def login():
assertion = request.form.get('assertion')
audience = request.form.get('audience')
is_native = int(request.form.get('is_native'))
print 'Attempting verification:', audience, is_native
email = persona.verify_assertion(assertion, audience, is_native)
if not email:
return make_response('{"error": "bad_assertion"}', 403)
# At this point, we know that the user is a valid user.
return {
'error': None,
'token': persona.get_token(email),
'settings': {
'display_name': email.split('@')[0],
'email': email,
'region': 'us',
},
'permissions': {},
'apps': defaults._user_apps(),
}
@app.route('/api/v1/account/settings/mine/', methods=['GET', 'PATCH'])
def settings():
return {
'display_name': 'Joe User',
'email': request.args.get('email'),
'region': 'us',
}
@app.route('/api/v1/abuse/app/', methods=['POST'])
def app_abuse():
if not request.form.get('text'):
return {'error': True}
return {'error': False}
@app.route('/api/v1/account/feedback/', methods=['POST'])
def feedback():
if not request.form.get('feedback'):
return {'error': True}
return {'error': False}
@app.route('/api/v1/apps/app/<slug>/privacy/', methods=['GET'])
def privacy(slug):
return {
'privacy_policy': defaults.ptext(),
}
@app.route('/api/v1/apps/category/')
def categories():
return {
'objects': [
defaults.category('shopping', 'Shopping'),
defaults.category('games', 'Games'),
defaults.category('productivity', 'Productivity'),
defaults.category('social', 'Social'),
defaults.category('music', 'Music'),
defaults.category('lifestyle', 'Thug Life'),
]
}
@app.route('/api/v1/account/installed/mine/')
def installed():
def gen():
i = 0
while 1:
yield defaults.app('Purchased App', 'purchase%d' % i)
i += 1
query = request.args.get('q')
data = app._paginated('objects', gen, 0 if query == 'empty' else 42)
return data
@app.route('/api/v1/fireplace/search/', endpoint='search-fireplace')
@app.route('/api/v1/apps/search/')
def search():
offset = int(request.args.get('offset', 0))
def gen():
i = 0
while 1:
nb = i + 1 + offset
yield defaults.app('Result %d' % nb, 'sr%d' % nb)
i += 1
query = request.args.get('q')
data = app._paginated('objects', gen, 0 if query == 'empty' else 42)
return data
@app.route('/api/v1/fireplace/search/featured/')
def category():
def gen():
i = 0
while 1:
yield defaults.app('Category Item', 'catm %d' % i)
i += 1
data = app._paginated('objects', gen)
data['collections'] = [
defaults.collection('Collection', 'collection-0',
collection_type=0),
defaults.collection('Collection', 'collection-1',
collection_type=0),
]
data['featured'] = [
defaults.collection('Featured', 'featured', collection_type=1),
]
data['operator'] = [
defaults.collection('Operator Shelf', 'operator', collection_type=2),
]
return data
@app.route('/api/v1/apps/rating/', methods=['GET', 'POST'])
def app_ratings():
if request.method == 'POST':
return {'error': False}
def gen():
i = 0
while 1:
yield defaults.rating()
i += 1
slug = request.form.get('app') or request.args.get('app')
data = app._paginated('objects', gen)
data['info'] = {
'slug': slug,
'average': random.random() * 4 + 1,
}
data.update(defaults.rating_user_data(slug))
return data
@app.route('/api/v1/apps/rating/<id>/', methods=['GET', 'PUT', 'DELETE'])
def app_rating(id):
if request.method in ('PUT', 'DELETE'):
return {'error': False}
return defaults.rating()
@app.route('/api/v1/apps/rating/<id>/flag/', methods=['POST'])
def app_rating_flag(id):
return ''
@app.route('/api/v1/fireplace/app/<slug>/')
def app_(slug):
return defaults.app('Something something %s' % slug, slug)
@app.route('/api/v1/installs/record/', methods=['POST'])
def record_free():
return {'error': False}
@app.route('/api/v1/receipts/install/', methods=['POST'])
def record_paid():
return {'error': False}
@app.route('/api/v1/apps/<id>/statistics/', methods=['GET', 'POST'])
def app_stats(id):
return json.loads(open('./fixtures/3serieschart.json', 'r').read())
@app.route('/api/v1/fireplace/consumer-info/', methods=['GET'])
def consumer_info():
return {
'region': 'us',
'apps': defaults._user_apps()
}
@app.route('/api/v1/fireplace/collection/<slug>/',
endpoint='collection-fireplace')
@app.route('/api/v1/rocketfuel/collections/<slug>/')
def collection_detail(slug):
"""
Returns a randomly generated colleciton.
name -- name of collection
_num -- as GET param, num of apps to add to collection.
Not part of API, for benchmark purposes only.
"""
num = int(request.args.get('_num', 16))
return defaults.collection(slug, '%s-%s' % (slug, num),
num=num, collection_type=0)
@app.route('/api/v2/feed/collections/', methods=['GET', 'POST'])
def collection_listing():
colls = []
for i in range(5):
colls.append(defaults.collection('some name %s' % i,
'some_name_%s' % i,
collection_type=i))
return {
'objects': colls
}
if __name__ == '__main__':
app.run()