-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
70 lines (59 loc) · 1.86 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
from flask import Flask, jsonify, request
from functools import wraps
from matomo_pull.utils import DatabaseAlreadyUpdatedError
import jwt
import os
import main
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
def check_for_token(func):
@wraps(func)
def wrapped(*args, **kwargs):
token = request.args.get('token')
if not token:
return jsonify({'message': 'Missing token'}), 403
try:
jwt.decode(token, app.config['SECRET_KEY'], 'HS256')
except jwt.exceptions.ExpiredSignatureError:
return jsonify({'message': 'Token expired'}), 403
except jwt.exceptions.InvalidSignatureError:
return jsonify({'message': 'Invalid token'}), 403
except Exception as e:
return jsonify({'message': f"{type(e).__name__}: {str(e)}"})
return func(*args, **kwargs)
return wrapped
def check_data(func):
@wraps(func)
def wrapped(*args, **kwargs):
try:
[
request.args['base_url'],
request.args['id_site'],
request.args['start_date'],
request.args['token_auth'],
request.args['db_name']
]
except Exception:
return jsonify({'message': 'Invalid data'}), 403
return func(*args, **kwargs)
return wrapped
@app.route('/')
@check_for_token
@check_data
def index():
data = request.args
try:
main.exec(data)
except DatabaseAlreadyUpdatedError:
return jsonify(
{'message': 'Database already updated'}
), 200
except Exception:
return jsonify(
{'message': 'Error executing script: recheck database variables'}
), 403
return jsonify(
{'message': 'Database successfully imported'}
), 200
if __name__ == "__main__":
app.run()