-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
378 lines (310 loc) · 11.8 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""Main file of the app. Loaded once on server startup!"""
from functools import wraps
import json
from flask import Flask, jsonify, request
from src.controller.platform_controller import PlatformController
from src.controller.game_controller import GameController
from src.controller.user_controller import UserController
from src.controller.version_controller import VersionController
from src.controller.copy_controller import CopyController
from src.controller.story_controller import StoryController
from src.controller.transaction_controller import TransactionController
from src.controller.note_controller import NoteController
from src.repository.user_repository import UserRepository
from src.connection.mysql_factory import MySQLFactory
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
##############
# Load config
##############
with open('configuration.json', encoding='UTF-8') as json_file:
configurationData = json.load(json_file)
################
# DB connection
################
MySQLFactory.init(
configurationData['db_host'],
configurationData['db_user'],
configurationData['db_password'],
configurationData['database']
)
##################
# User management
##################
def token_required(decorated_function):
@wraps(decorated_function)
def decorator(*args, **kwargs):
token = None
if 'Authorization' in request.headers:
header_value = request.headers['Authorization']
if header_value.find(' ') != -1:
array = header_value.split(' ')
if array[0] == 'token':
token = array[1]
if not token:
return jsonify({'message': 'Missing token', 'code': 12}), 403
user_repo = UserRepository(MySQLFactory.get())
current_user = user_repo.get_active_by_token(token)
if None is current_user:
return jsonify({'message': 'Token is invalid', 'code': 13}), 403
# This method is the only one to need the current_user
if 'renew_token' == decorated_function.__name__:
return decorated_function(current_user, *args, **kwargs)
return decorated_function(*args, **kwargs)
return decorator
########################################################################
# After request: cache management, close DB connection...
########################################################################
@app.after_request
def after_request(response):
"""Handle logic after each request"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
MySQLFactory.close()
return response
##########
# Routes
##########
# Home
@app.route('/')
def home():
"""Homepage with layout"""
return jsonify({'message': 'Hello!'}), 200
# Users
@app.route('/api/v1/user/authenticate', methods=['POST'])
def authenticate_user():
"""Returns the token of the given user"""
controller = UserController
return controller.authenticate(MySQLFactory.get())
@app.route('/api/v1/user', methods=['GET'])
@token_required
def get_user():
"""Returns the user according to one filter"""
controller = UserController
return controller.get_by_filter(
MySQLFactory.get(),
request.args.get('filter', ''),
request.args.get('value', '')
)
@app.route('/api/v1/user', methods=['POST'])
@token_required
def create_user():
"""Creates a user"""
controller = UserController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/user/<int:entity_id>', methods=['PATCH'])
@token_required
def update_user(entity_id):
"""Updates a user"""
controller = UserController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/user/renew-token', methods=['POST'])
@token_required
def renew_token(current_user):
"""Renew the API token of the current user"""
controller = UserController
return controller.renew_token(MySQLFactory.get(), current_user)
# Platforms
@app.route('/api/v1/platform/<int:entity_id>', methods=['GET'])
def get_platform_by_id(entity_id):
"""Returns the platform according to its id"""
controller = PlatformController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/platform', methods=['POST'])
@token_required
def create_platform():
"""Create a platform"""
controller = PlatformController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/platform/<int:entity_id>', methods=['PATCH'])
@token_required
def update_platform(entity_id):
"""Update the platform according to its id"""
controller = PlatformController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/platform/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_platform(entity_id):
"""Delete the platform according to its id"""
controller = PlatformController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/platforms', methods=['GET'])
def get_platforms():
"""Get the platforms"""
controller = PlatformController
return controller.get_list(MySQLFactory.get())
# Games
@app.route('/api/v1/game/<int:entity_id>', methods=['GET'])
def get_game_by_id(entity_id):
"""Returns the game according to its id"""
controller = GameController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/game', methods=['POST'])
@token_required
def create_game():
"""Create a game"""
controller = GameController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/game/<int:entity_id>', methods=['PATCH'])
@token_required
def update_game(entity_id):
"""Update the game according to its id"""
controller = GameController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/game/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_game(entity_id):
"""Delete the game according to its id"""
controller = GameController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/games', methods=['GET'])
def get_games():
"""Get the games"""
controller = GameController
return controller.get_list(MySQLFactory.get())
# Versions
@app.route('/api/v1/version/<int:entity_id>', methods=['GET'])
def get_version_by_id(entity_id):
"""Returns the version according to its id"""
controller = VersionController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/version', methods=['POST'])
@token_required
def create_version():
"""Create a version"""
controller = VersionController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/version/<int:entity_id>', methods=['PATCH'])
@token_required
def update_version(entity_id):
"""Update the version according to its id"""
controller = VersionController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/version/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_version(entity_id):
"""Delete the version according to its id"""
controller = VersionController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/versions', methods=['GET'])
def get_versions():
"""Get the versions"""
controller = VersionController
return controller.get_list(MySQLFactory.get())
# Copies
@app.route('/api/v1/copy/<int:entity_id>', methods=['GET'])
def get_copy_by_id(entity_id):
"""Returns the copy according to its id"""
controller = CopyController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/copy', methods=['POST'])
@token_required
def create_copy():
"""Create a copy"""
controller = CopyController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/copy/<int:entity_id>', methods=['PATCH'])
@token_required
def update_copy(entity_id):
"""Update the copy according to its id"""
controller = CopyController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/copy/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_copy(entity_id):
"""Delete the copy according to its id"""
controller = CopyController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/copies', methods=['GET'])
def get_copies():
"""Get the copies"""
controller = CopyController
return controller.get_list(MySQLFactory.get())
# Stories
@app.route('/api/v1/story/<int:entity_id>', methods=['GET'])
def get_story_by_id(entity_id):
"""Returns the story according to its id"""
controller = StoryController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/story', methods=['POST'])
@token_required
def create_story():
"""Create a story"""
controller = StoryController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/story/<int:entity_id>', methods=['PATCH'])
@token_required
def update_story(entity_id):
"""Update the story according to its id"""
controller = StoryController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/story/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_story(entity_id):
"""Delete the story according to its id"""
controller = StoryController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/stories', methods=['GET'])
def get_stories():
"""Get the stories"""
controller = StoryController
return controller.get_list(MySQLFactory.get())
# Transactions
@app.route('/api/v1/transaction/<int:entity_id>', methods=['GET'])
def get_transaction_by_id(entity_id):
"""Returns the transaction according to its id"""
controller = TransactionController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/transaction', methods=['POST'])
@token_required
def create_transaction():
"""Create a transaction"""
controller = TransactionController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/transaction/<int:entity_id>', methods=['PATCH'])
@token_required
def update_transaction(entity_id):
"""Update the transaction according to its id"""
controller = TransactionController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/transaction/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_transaction(entity_id):
"""Delete the transaction according to its id"""
controller = TransactionController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/transactions', methods=['GET'])
def get_transactions():
"""Get the transactions"""
controller = TransactionController
return controller.get_list(MySQLFactory.get())
# Notes
@app.route('/api/v1/note/<int:entity_id>', methods=['GET'])
def get_note_by_id(entity_id):
"""Returns the note according to its id"""
controller = NoteController
return controller.get_by_id(MySQLFactory.get(), entity_id)
@app.route('/api/v1/note', methods=['POST'])
@token_required
def create_note():
"""Create a note"""
controller = NoteController
return controller.create(MySQLFactory.get())
@app.route('/api/v1/note/<int:entity_id>', methods=['PATCH'])
@token_required
def update_note(entity_id):
"""Update the note according to its id"""
controller = NoteController
return controller.update(MySQLFactory.get(), entity_id)
@app.route('/api/v1/note/<int:entity_id>', methods=['DELETE'])
@token_required
def delete_note(entity_id):
"""Delete the note according to its id"""
controller = NoteController
return controller.delete(MySQLFactory.get(), entity_id)
@app.route('/api/v1/notes', methods=['GET'])
def get_notes():
"""Get the notes"""
controller = NoteController
return controller.get_list(MySQLFactory.get())