-
Notifications
You must be signed in to change notification settings - Fork 16
/
api_v3.py
393 lines (312 loc) · 16.3 KB
/
api_v3.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import json
from flask import Blueprint, jsonify, url_for, request, make_response, render_template, redirect
from flask_login import current_user
from flask_swagger import swagger
from portality.api.current import ApplicationsCrudApi, ArticlesCrudApi, JournalsCrudApi, ApplicationsBulkApi, \
ArticlesBulkApi
from portality.api.current import DiscoveryApi, DiscoveryException
from portality.api.current import jsonify_models, jsonify_data_object, Api400Error, Api404Error, created, \
no_content, bulk_created
from portality.core import app
from portality.decorators import api_key_required, api_key_optional, swag, write_required
from portality.lib import plausible
blueprint = Blueprint('api_v3', __name__)
API_VERSION_NUMBER = '3.0.1' # OA start added 2022-03-21
# Google Analytics category for API events
GA_CATEGORY = app.config.get('GA_CATEGORY_API', 'API Hit')
GA_ACTIONS = app.config.get('GA_ACTIONS_API', {})
@blueprint.route('/')
def api_v3_root():
return redirect(url_for('.api_spec'))
@blueprint.route('/docs')
def docs():
account_url = None
if current_user.is_authenticated:
account_url = url_for('account.username', username=current_user.id, _external=True,
_scheme=app.config.get('PREFERRED_URL_SCHEME', 'https'))
return render_template('api/current/api_docs.html',
api_version=API_VERSION_NUMBER,
base_url=app.config.get("BASE_API_URL", url_for('.api_v3_root')),
contact_us_url=url_for('doaj.contact'),
account_url=account_url)
@blueprint.route('/swagger.json')
def api_spec():
swag = swagger(app)
swag['info']['title'] = ""
swag['info']['version'] = API_VERSION_NUMBER
# Strip out the duplicate versioned route from the swagger so we only show latest as /api/
[swag['paths'].pop(p) for p in list(swag['paths'].keys()) if p.startswith('/api/v3/')]
return make_response((jsonify(swag), 200, {'Access-Control-Allow-Origin': '*'}))
# Handle wayward paths by raising an API404Error
@blueprint.route("/<path:invalid_path>", methods=["POST", "GET", "PUT", "DELETE", "PATCH", "HEAD"]) # leaving out methods should mean all, but tests haven't shown that behaviour.
def missing_resource(invalid_path):
docs_url = app.config.get("BASE_URL", "") + url_for('.docs')
spec_url = app.config.get("BASE_URL", "") + url_for('.api_spec')
raise Api404Error("No endpoint at {0}. See {1} for valid paths or read the documentation at {2}.".format(invalid_path, spec_url, docs_url))
@swag(swag_summary='Search your applications <span class="red">[Authenticated, not public]</span>',
swag_spec=DiscoveryApi.get_application_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@blueprint.route("/search/applications/<path:search_query>")
@api_key_required
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('search_applications', 'Search applications'),
record_value_of_which_arg='search_query')
def search_applications(search_query):
# get the values for the 2 other bits of search info: the page number and the page size
page = request.values.get("page", 1)
psize = request.values.get("pageSize", 10)
sort = request.values.get("sort")
# check the page is an integer
try:
page = int(page)
except:
raise Api400Error("Page number was not an integer")
# check the page size is an integer
try:
psize = int(psize)
except:
raise Api400Error("Page size was not an integer")
try:
results = DiscoveryApi.search('application', current_user, search_query, page, psize, sort)
except DiscoveryException as e:
raise Api400Error(str(e))
return jsonify_models(results)
@swag(swag_summary='Search journals',
swag_spec=DiscoveryApi.get_journal_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@blueprint.route('/search/journals/<path:search_query>')
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('search_journals', 'Search journals'),
record_value_of_which_arg='search_query')
def search_journals(search_query):
# get the values for the 2 other bits of search info: the page number and the page size
page = request.values.get("page", 1)
psize = request.values.get("pageSize", 10)
sort = request.values.get("sort")
# check the page is an integer
try:
page = int(page)
except:
raise Api400Error("Page number was not an integer")
# check the page size is an integer
try:
psize = int(psize)
except:
raise Api400Error("Page size was not an integer")
try:
results = DiscoveryApi.search('journal', None, search_query, page, psize, sort)
except DiscoveryException as e:
raise Api400Error(str(e))
return jsonify_models(results)
@swag(swag_summary='Search articles',
swag_spec=DiscoveryApi.get_article_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@blueprint.route('/search/articles/<path:search_query>')
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('search_articles', 'Search articles'),
record_value_of_which_arg='search_query')
def search_articles(search_query):
# get the values for the 2 other bits of search info: the page number and the page size
page = request.values.get("page", 1)
psize = request.values.get("pageSize", 10)
sort = request.values.get("sort")
# check the page is an integer
try:
page = int(page)
except:
raise Api400Error("Page number was not an integer")
# check the page size is an integer
try:
psize = int(psize)
except:
raise Api400Error("Page size was not an integer")
results = None
try:
results = DiscoveryApi.search('article', None, search_query, page, psize, sort)
except DiscoveryException as e:
raise Api400Error(str(e))
return jsonify_models(results)
#########################################
# Application CRUD API
@blueprint.route("/applications", methods=["POST"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Create an application <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsCrudApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('create_application', 'Create application'))
def create_application():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
a = ApplicationsCrudApi.create(data, current_user._get_current_object())
# respond with a suitable Created response
return created(a, url_for("api_v3.retrieve_application", application_id=a.id))
@blueprint.route("/applications/<application_id>", methods=["GET"])
@api_key_required
@swag(swag_summary='Retrieve an application <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsCrudApi.retrieve_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_application', 'Retrieve application'),
record_value_of_which_arg='application_id')
def retrieve_application(application_id):
a = ApplicationsCrudApi.retrieve(application_id, current_user)
return jsonify_models(a)
@blueprint.route("/applications/<application_id>", methods=["PUT"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Update an application <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsCrudApi.update_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('update_application', 'Update application'),
record_value_of_which_arg='application_id')
def update_application(application_id):
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
ApplicationsCrudApi.update(application_id, data, current_user._get_current_object())
# respond with a suitable No Content successful response
return no_content()
@blueprint.route("/applications/<application_id>", methods=["DELETE"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Delete an application <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsCrudApi.delete_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('delete_application', 'Delete application'),
record_value_of_which_arg='application_id')
def delete_application(application_id):
ApplicationsCrudApi.delete(application_id, current_user._get_current_object())
return no_content()
#########################################
# Article CRUD API
@blueprint.route("/articles", methods=["POST"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Create an article <span class="red">[Authenticated, not public]</span>',
swag_spec=ArticlesCrudApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('create_article', 'Create article'))
def create_article():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
a = ArticlesCrudApi.create(data, current_user._get_current_object())
# respond with a suitable Created response
return created(a, url_for("api_v3.retrieve_article", article_id=a.id))
@blueprint.route("/articles/<article_id>", methods=["GET"])
@api_key_optional
@swag(swag_summary='Retrieve an article',
swag_spec=ArticlesCrudApi.retrieve_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_article', 'Retrieve article'),
record_value_of_which_arg='article_id')
def retrieve_article(article_id):
a = ArticlesCrudApi.retrieve(article_id, current_user)
return jsonify_models(a)
@blueprint.route("/articles/<article_id>", methods=["PUT"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Update an article <span class="red">[Authenticated, not public]</span>',
swag_spec=ArticlesCrudApi.update_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('update_article', 'Update article'),
record_value_of_which_arg='article_id')
def update_article(article_id):
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
ArticlesCrudApi.update(article_id, data, current_user._get_current_object())
# respond with a suitable No Content successful response
return no_content()
@blueprint.route("/articles/<article_id>", methods=["DELETE"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Delete an article <span class="red">[Authenticated, not public]</span>',
swag_spec=ArticlesCrudApi.delete_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('delete_article', 'Delete article'),
record_value_of_which_arg='article_id')
def delete_article(article_id):
ArticlesCrudApi.delete(article_id, current_user)
return no_content()
#########################################
# Journal R API
@blueprint.route('/journals/<journal_id>', methods=['GET'])
@api_key_optional
@swag(swag_summary='Retrieve a journal by ID',
swag_spec=JournalsCrudApi.retrieve_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('retrieve_journal', 'Retrieve journal'),
record_value_of_which_arg='journal_id')
def retrieve_journal(journal_id):
return jsonify_data_object(JournalsCrudApi.retrieve(journal_id, current_user))
#########################################
# Application Bulk API
@blueprint.route("/bulk/applications", methods=["POST"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Create applications in bulk <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsBulkApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_application_create', 'Bulk application create'))
def bulk_application_create():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
ids = ApplicationsBulkApi.create(data, current_user._get_current_object())
# get all the locations for the ids
inl = []
for id in ids:
inl.append((id, url_for("api_v3.retrieve_application", application_id=id)))
# respond with a suitable Created response
return bulk_created(inl)
@blueprint.route("/bulk/applications", methods=["DELETE"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Delete applications in bulk <span class="red">[Authenticated, not public]</span>',
swag_spec=ApplicationsBulkApi.delete_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_application_delete', 'Bulk application delete'))
def bulk_application_delete():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
ApplicationsBulkApi.delete(data, current_user._get_current_object())
return no_content()
#########################################
# Article Bulk API
@blueprint.route("/bulk/articles", methods=["POST"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Bulk article creation <span class="red">[Authenticated, not public]</span>',
swag_spec=ArticlesBulkApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_article_create', 'Bulk article create'))
def bulk_article_create():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
# delegate to the API implementation
ids = ArticlesBulkApi.create(data, current_user._get_current_object())
# get all the locations for the ids
inl = []
for id in ids:
inl.append((id, url_for("api_v3.retrieve_article", article_id=id)))
# respond with a suitable Created response
return bulk_created(inl)
@blueprint.route("/bulk/articles", methods=["DELETE"])
@api_key_required
@write_required(api=True)
@swag(swag_summary='Bulk article delete <span class="red">[Authenticated, not public]</span>',
swag_spec=ArticlesBulkApi.delete_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes.
@plausible.pa_event(GA_CATEGORY, action=GA_ACTIONS.get('bulk_article_delete', 'Bulk article delete'))
def bulk_article_delete():
# get the data from the request
try:
data = json.loads(request.data.decode("utf-8"))
except:
raise Api400Error("Supplied data was not valid JSON")
ArticlesBulkApi.delete(data, current_user._get_current_object())
return no_content()