-
Notifications
You must be signed in to change notification settings - Fork 16
/
common.py
276 lines (217 loc) · 10.7 KB
/
common.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
#~~API:Feature~~
import json, uuid
from portality.core import app
from flask import request
from copy import deepcopy
from link_header import LinkHeader, Link
LINK_HEADERS = ['next', 'prev', 'last']
TOTAL_RESULTS_COUNT = ['total']
ERROR_TEMPLATE = {"status": {"type": "string"}, "error": {"type": "string"}}
CREATED_TEMPLATE = {"status": {"type": "string"}, "id": {"type": "string"}, "location": {"type": "string"}}
class Api(object):
# ~~->Swagger:Feature~~
# ~~->API:Documentation~~
SWAG_TEMPLATE = {
"description" : "",
"responses": {},
"parameters": [],
"tags": []
}
R200 = {"schema": {}, "description": "A successful request/response"}
R201 = {"schema": {"properties": CREATED_TEMPLATE}, "description": "Resource created successfully, response "
"contains the new resource ID and location."}
R201_BULK = {"schema": {"items": {"properties" : CREATED_TEMPLATE, "type" : "object"}, "type" : "array"},
"description": "Resources created successfully, response contains the new resource IDs "
"and locations."}
R204 = {"description": "OK (Request succeeded), No Content"}
R400 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "Bad Request. Your request body was missing a "
"required field, or the data in one of the "
"fields did not match the schema above (e.g. "
"string of latin letters in an integer field). "
"In the Bulk API it may mean that one of the "
"records in the bulk operation failed. See the "
"\"error\" part of the response for details."}
R401 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "Access to this route/resource requires "
"authentication, but you did not provide any "
"credentials."}
R403 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "Access to this route/resource requires "
"authentication, and you provided the wrong "
"credentials. This includes situations where you "
"are authenticated successfully via your API "
"key, but you are not the owner of a specific "
"resource and are therefore barred from "
"updating/deleting it."}
R404 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "Resource not found"}
R409 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "This resource or one it depends on is currently "
"locked for editing by another user, and you may "
"not submit changes to it at this time"}
R500 = {"schema": {"properties": ERROR_TEMPLATE}, "description": "Unable to retrieve the recource. This record "
"contains bad data"}
SWAG_API_KEY_REQ_PARAM = {
"description": "<div class=\"search-query-docs\">Go to 'MY ACCOUNT' and 'Settings' to find your API key. If there is no key, click 'Generate a new API key'. If you do not see that button, contact us.",
"required": True,
"type": "string",
"name": "api_key",
"in": "query"
}
@classmethod
@property
def SWAG_TAG(cls):
raise RuntimeError('You must override this class constant in every subclass.')
@classmethod
def _add_swag_tag(cls, template):
template['tags'].append(cls.SWAG_TAG)
return template
@classmethod
def _add_api_key(cls, template, optional=False):
# ~~->APIKey:Feature~~
api_key_param = deepcopy(cls.SWAG_API_KEY_REQ_PARAM)
if optional:
api_key_param['required'] = False
api_key_param['description'] = "<div class=\"search-query-docs\"><em>Note this parameter is optional for " \
"this route - you could, but don't have to supply a key. Doing so grants " \
"you access to records of yours that are not public, in addition to all " \
"public records.</em> Go to 'MY ACCOUNT' and 'Settings' to find your API key. If there is no key, click 'Generate a new API key'. If you do not see that button, contact us."
template["parameters"].insert(0, api_key_param)
return template
@classmethod
def _build_swag_response(cls, template, api_key_optional_override=None, api_key_override=None):
"""
Construct the swagger response structure upon a template
:param template
:param api_key_optional_override: override the class-level value of API_KEY_OPTIONAL
:return: an updated template
"""
template = deepcopy(template)
cls._add_swag_tag(template)
if api_key_override is not False:
if api_key_optional_override is not None:
cls._add_api_key(template, optional=api_key_optional_override)
elif hasattr(cls, 'API_KEY_OPTIONAL'):
cls._add_api_key(template, optional=cls.API_KEY_OPTIONAL)
return template
class Api400Error(Exception):
pass
class Api401Error(Exception):
pass
class Api403Error(Exception):
pass
class Api404Error(Exception):
pass
class Api409Error(Exception):
"""
API error to throw if a resource being edited is locked
"""
pass
class Api500Error(Exception):
pass
class DataObjectJsonEncoder(json.JSONEncoder):
def default(self, o):
return o.data
class ModelJsonEncoder(json.JSONEncoder):
def default(self, o):
return o.data
def created(obj, location):
app.logger.info("Sending 201 Created: {x}".format(x=location))
t = deepcopy(CREATED_TEMPLATE)
t['status'] = "created"
t['id'] = obj.id
t['location'] = location
resp = respond(json.dumps(t), 201)
resp.headers["Location"] = location
resp.status_code = 201
return resp
def bulk_created(ids_and_locations):
app.logger.info("Sending 201 Created for bulk request")
out = []
for id, loc in ids_and_locations:
t = deepcopy(CREATED_TEMPLATE)
t['status'] = "created"
t['id'] = id
t['location'] = loc
out.append(t)
resp = respond(json.dumps(out), 201)
resp.status_code = 201
return resp
def no_content():
return respond("", 204)
def jsonify_data_object(do):
data = json.dumps(do, cls=DataObjectJsonEncoder)
return respond(data, 200)
def jsonify_models(models):
data = json.dumps(models, cls=ModelJsonEncoder)
metadata = {}
for k in LINK_HEADERS + TOTAL_RESULTS_COUNT:
if k in models.data:
metadata[k] = models.data[k]
return respond(data, 200, metadata=metadata)
def generate_link_headers(metadata):
"""
Generate Link: HTTP headers for API navigation.
:param metadata: Dictionary with none, some or all of the
keys 'next', 'prev' and 'last' defined. The values are the
corresponding pre-generated links.
"""
link_metadata = {k: v for k, v in metadata.items() if k in LINK_HEADERS}
links = []
for k, v in link_metadata.items():
links.append(Link(v, rel=k)) # e.g. Link("https://example.com/foo", rel="next")
return str(LinkHeader(links)) # RFC compliant headers e.g.
# <https://example.com/foo>; rel=next, <https://example.com/bar>; rel=last
def respond(data, status, metadata=None):
# avoid subtle bugs, don't use mutable objects as default vals in Python
# https://pythonconquerstheuniverse.wordpress.com/category/python-gotchas/
if metadata is None:
metadata = {}
headers = {'Access-Control-Allow-Origin': '*'}
link = generate_link_headers(metadata)
if link:
headers['Link'] = link
if 'total' in metadata:
headers['X-Total-Count'] = metadata['total']
callback = request.args.get('callback', False)
if callback:
content = str(callback) + '(' + str(data) + ')'
return app.response_class(content, status, headers, mimetype='application/javascript')
else:
return app.response_class(data, status, headers, mimetype='application/json')
@app.errorhandler(Api400Error)
def bad_request(error):
magic = uuid.uuid1()
app.logger.info("Sending 400 Bad Request from client: {x} (ref: {y})".format(x=str(error), y=magic))
t = deepcopy(ERROR_TEMPLATE)
t['status'] = 'bad_request'
t['error'] = str(error) + " (ref: {y})".format(y=magic)
return respond(json.dumps(t), 400)
@app.errorhandler(Api404Error)
def not_found(error):
magic = uuid.uuid1()
app.logger.info("Sending 404 Not Found from client: {x} (ref: {y})".format(x=str(error), y=magic))
t = deepcopy(ERROR_TEMPLATE)
t['status'] = 'not_found'
t['error'] = str(error) + " (ref: {y})".format(y=magic)
return respond(json.dumps(t), 404)
@app.errorhandler(Api401Error)
def unauthorised(error):
magic = uuid.uuid1()
app.logger.info("Sending 401 Unauthorised from client: {x} (ref: {y})".format(x=str(error), y=magic))
t = deepcopy(ERROR_TEMPLATE)
t['status'] = 'unauthorised'
t['error'] = str(error) + " (ref: {y})".format(y=magic)
return respond(json.dumps(t), 401)
@app.errorhandler(Api403Error)
def forbidden(error):
magic = uuid.uuid1()
app.logger.info("Sending 403 Forbidden from client: {x} (ref: {y})".format(x=str(error), y=magic))
t = deepcopy(ERROR_TEMPLATE)
t['status'] = 'forbidden'
t['error'] = str(error) + " (ref: {y})".format(y=magic)
return respond(json.dumps(t), 403)
@app.errorhandler(Api500Error)
def bad_request(error):
magic = uuid.uuid1()
app.logger.info("Sending 500 Bad Request from client: {x} (ref: {y})".format(x=str(error), y=magic))
t = deepcopy(ERROR_TEMPLATE)
t['status'] = 'Unable to retrieve the recource.'
t['error'] = str(error) + " (ref: {y})".format(y=magic)
return respond(json.dumps(t), 500)