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
|
#!/usr/bin/env python3
import json
import os
import sys
import traceback
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from flask_compress import Compress
from flask_cors import CORS
from opendc.util import rest, path_parser, database
from opendc.util.auth import AuthError, AuthManager, AsymmetricJwtAlgorithm
from opendc.util.exceptions import AuthorizationTokenError, RequestInitializationError
from opendc.util.json import JSONEncoder
load_dotenv()
TEST_MODE = "OPENDC_FLASK_TESTING" in os.environ
# Setup Sentry if DSN is specified
if 'SENTRY_DSN' in os.environ:
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
integrations=[FlaskIntegration()],
traces_sample_rate=0.1
)
# Set up database if not testing
if not TEST_MODE:
database.DB.initialize_database(
user=os.environ['OPENDC_DB_USERNAME'],
password=os.environ['OPENDC_DB_PASSWORD'],
database=os.environ['OPENDC_DB'],
host=os.environ.get('OPENDC_DB_HOST', 'localhost'))
# Set up the core app
app = Flask("opendc")
app.testing = TEST_MODE
app.config['SECRET_KEY'] = os.environ['OPENDC_FLASK_SECRET']
app.json_encoder = JSONEncoder
# Set up CORS support
CORS(app)
compress = Compress()
compress.init_app(app)
auth = AuthManager(AsymmetricJwtAlgorithm(jwks_url=f"https://{os.environ['AUTH0_DOMAIN']}/.well-known/jwks.json"),
issuer=f"https://{os.environ['AUTH0_DOMAIN']}/", audience=os.environ['AUTH0_AUDIENCE'])
API_VERSIONS = {'v2'}
@app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
@app.route('/<string:version>/<path:endpoint_path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
@auth.require
def api_call(version, endpoint_path):
"""Call an API endpoint directly over HTTP."""
# Check whether given version is valid
if version not in API_VERSIONS:
return jsonify(error='API version not found'), 404
# Get path and parameters
(path, path_parameters) = path_parser.parse(version, endpoint_path)
query_parameters = request.args.to_dict()
for param in query_parameters:
try:
query_parameters[param] = int(query_parameters[param])
except:
pass
try:
body_parameters = json.loads(request.get_data())
except:
body_parameters = {}
# Create and call request
(req, response) = _process_message({
'id': 0,
'method': request.method,
'parameters': {
'body': body_parameters,
'path': path_parameters,
'query': query_parameters
},
'path': path,
'token': request.headers.get('auth-token')
})
print(
f'HTTP:\t{req.method} to `/{req.path}` resulted in {response.status["code"]}: {response.status["description"]}')
sys.stdout.flush()
flask_response = jsonify(json.loads(response.to_JSON()))
flask_response.status_code = response.status['code']
return flask_response
def _process_message(message):
"""Process a request message and return the response."""
try:
req = rest.Request(message)
res = req.process()
return req, res
except AuthorizationTokenError:
res = rest.Response(401, 'Authorization error')
res.id = message['id']
except RequestInitializationError as e:
res = rest.Response(400, str(e))
res.id = message['id']
if not 'method' in message:
message['method'] = 'UNSPECIFIED'
if not 'path' in message:
message['path'] = 'UNSPECIFIED'
except Exception:
res = rest.Response(500, 'Internal server error')
if 'id' in message:
res.id = message['id']
traceback.print_exc()
req = rest.Request()
req.method = message['method']
req.path = message['path']
return req, res
if __name__ == '__main__':
app.run()
|