-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
579 lines (444 loc) · 17.5 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import psycopg2
import psycopg2.extras
from flask import request, jsonify, Flask, render_template
from markupsafe import escape
from flask_caching import Cache
import sys
''' API code Inspired from: Patrick Smyth, "Creating Web APIs with Python and Flask,"
The Programming Historian 7 (2018), https://doi.org/10.46430/phen0072. '''
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
cache = Cache(config={'CACHE_TYPE': 'simple'})
cache.init_app(app)
#%%
@app.errorhandler(Exception)
def response(status, data=None, message="OK"):
return jsonify({"status": status, "result": data, "message": message})
#Error handler from pallets projects Flask documentation: https://flask.palletsprojects.com/en/1.1.x/patterns/errorpages/
@app.errorhandler(404)
def resource_404(e):
return jsonify({"status": 404, "result": None, "message": "Not found. The URL is not valid, please verify the URL is correct."})
@app.errorhandler(500)
def resource_500(e):
return jsonify({"status": 500, "result": None, "message": e})
#%%
# Singleton pattern
class Connection:
__instance = None
def __init__(self):
self.cur = self.get_con()
def __new__(cls):
if cls.__instance is None:
cls.__instance = object.__new__(cls)
return cls.__instance
def get_con(self):
con = psycopg2.connect(database="bsp3", user="postgres", password="5555", host="localhost")
cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
return cur
#%%
@app.route('/api/v1', methods=['GET'])
def home():
return render_template("index.html")
#%%
# Show all sectors
@app.route('/api/v1/sectors')
def allsectors():
try:
con = Connection()
con.cur.execute('SELECT * from sectors;')
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
return response(200, record)
## Test URL: http://api.emissionsintrade.com/v1/sectors
#%%
# Show all regions
@app.route('/api/v1/regions')
def allregions():
try:
con = Connection()
con.cur.execute('SELECT * from regions;')
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
return response(200, record)
## Test URL on localhost: http://127.0.0.1:5000/api/v1/regions
#%%
# Show all regions
@app.route('/api/v1/regions')
def regions(region):
region = request.args.get("region_id", "").lower()
query = 'SELECT * FROM regions WHERE region_id={};'.format(escape(region))
to_filter=[]
if region:
query += ' region_from=%s AND'
to_filter.append(region)
try:
con = Connection()
con.cur.execute(query)
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
return response(200, record)
## Test URL: http://api.emissionsintrade.com/v1/regions
#%%
# Show all stressors
@app.route('/v1/stressors')
def allstressors():
try:
con = Connection()
con.cur.execute('SELECT * from stressors;')
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
return response(200, record)
## Test URL: http://api.emissionsintrade.com/v1/stressors
#%%
# Get specific, region-to-region data
@app.route('/api/v1/stressors/<stressor>')
def info(stressor):
list_stressors = ['co2', 'vl', 'economy']
region_from = request.args.get('region_from')
region_to = request.args.get('region_to')
sector_from = request.args.get('sector_from')
sector_to = request.args.get('sector_to')
query = 'SELECT * FROM {} WHERE'.format(escape(stressor))
to_filter = []
stressor = stressor.lower()
if stressor not in list_stressors:
return response(400, message="Bad request - Looks like you need to provide a stressor. Please check the spelling of the stressor and ensure it is in lowercase.")
if region_from:
for i in region_from:
query += ' region_from=%s OR '
to_filter.append(i)
if region_to:
for i in region_to:
query += ' region_to=%s OR '
to_filter.append(i)
if sector_from:
for i in sector_from:
query += ' sector_from=%s OR '
to_filter.append(i)
if sector_to:
for i in region_from:
query += ' sector_to=%s OR '
to_filter.append(i)
if not (region_from or region_to or sector_from or sector_to):
return response(400, message="Bad request - Please check that you have at least one sector or region selected.")
query = (query[:-4])
try:
con = Connection()
con.cur.execute(query, tuple(to_filter))
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
if not record:
return response(400, message="Bad request - Please check that your region and/or sector query is correctly entered.") #do not need to provide data because data is already none by default
return response(200, record)
# Test URL: http://api.emissionsintrade.com/v1/stressors/co2?region_to=at®ion_from=be§or_to=agriculture
# Test URL localhost: http://127.0.0.1:5000/api/v1/stressors/co2?region_to=fr§or_from=agriculture
#%%
@app.route('/data')
def data():
debug = request.args.get('debug', type=bool) ## create a debug var to print the query
region_from = request.args.getlist('region_from')
region_to = request.args.getlist('region_to')
sector_from = request.args.getlist('sector_from')
sector_to = request.args.getlist('sector_to')
query = 'SELECT * FROM co2 WHERE'
#to filter will match the escape chars to the corresponding region/sector to/from
to_filter = []
r_from = ""
r_to = ""
s_from = ""
s_to = ""
for i in region_from:
r_from += '%s'
to_filter.append( "'" + i + "'" )
for i in sector_from:
s_from += '%s'
to_filter.append(i)
for i in sector_to:
s_to += '%s'
to_filter.append("'" +i + "'")
for i in region_to:
r_to += '%s'
to_filter.append("'" + i + "'")
# Check that not empty - need to avoid including the AND if empty.
if len(r_from) > 0:
query += ' region_from IN %s AND'
if len(s_from) > 0:
query += ' sector_from IN %s AND'
if len(r_to) > 0:
query += ' region_from IN %s AND'
if len(s_to) > 0:
query += ' region_from IN (' + s_to + ') AND'
if query.endswith("AND"):
query = (query[:-3])
s = []
for i in to_filter:
s += i.split(',')
if debug:
return response( 200, query % tuple(to_filter))
# make bool and return the query that is being submitted
# Execute program
try:
con = Connection()
con.cur.execute(query, tuple(to_filter))
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
return response(200, query % tuple(to_filter))
#%%
# =============================================================================
# def normalize_query_param(value):
# return value if len(value) > 1 else value[0]
#
# def normalize_query(params):
# params_non_flat = params.to_dict(flat=False)
# return {k: normalize_query_param(v) for k, v in params_non_flat.items()}
#
#
# @app.route('/data1')
#
# def datas():
# debug = request.args.get('debug', type=bool)
# req_args = request.args
# req_args = req_args.to_dict(flat=False)
#
# rfrom = []
# for i in range(len(req_args['region_from'])):
# di = {}
# for key in req_args.keys():
# di[key] = req_args[key][i]
#
#
#
# # =============================================================================
# # if key == "region_from":
# # rfrom += req_args[key][i]
# # =============================================================================
# to_filter = []
# #mu = []
# #for l in to_filter:
# # mu += " ' " + l + " ' "
#
# # for i in region_from:
#
#
#
# # =============================================================================
# # =============================================================================
# # for i in region_to:
# # r_to.append('{}'.format(i))
# # to_filter.append(i)
# #
# # =============================================================================
#
# query = 'SELECT * FROM co2 WHERE'
#
# # Check that not empty - need to avoid including the AND if empty.
# if len(to_filter) > 0:
# query += ' region_from IN ( %s ) AND'
#
# # =============================================================================
# # if len(r_to) > 0:
# # query += ' region_to IN (' + str(r_to)[1:-1] + ') AND'
#
# # =============================================================================
#
# if query.endswith("AND"):
# query = (query[:-3])
#
# if debug:
# return response(200, di)
# # make bool and return the query that is being submitted
#
#
# # =============================================================================
# # try:
# # con = Connection()
# # con.cur.execute(query, to_filter)
# #
# # except Exception as e:
# # return resource_500(str(e))
# #
# # record = con.cur.fetchall()
# # record = [dict(row) for row in record]
# #
# # if not record:
# # return response(400, message="Bad request - Please check that your region and/or sector query is correctly entered.")
# #
# #
# # return response(200, record)
# # =============================================================================
#
# =============================================================================
#%%
# =============================================================================
# @app.route("/testquery")
# def testquery():
# debug = request.args.get('debug', type=bool) ## create a debug var to print the query
#
# args = request.args.getlist('region_from')
#
# rf = args[0]
#
# if debug:
# return response(200, rf)
# =============================================================================
#%%
@app.route("/query")
def query():
'''request.args.getlist returns an IMMUTABLE multi dict. You must copy this
another list in order to parse it. The result of
region_from = request.args.getlist('region_from') is one value (for ex. 'xx,yy,zz',
for three entries in the query parameter or 'xx' for one) so in the copied
list, you need to select the first element in that list. Then, you can split
the list and assign each to a region_to/from or sector to/from.'''
# REMOVE DEBUG FROM PRODUCTION CODE
debug = request.args.get('debug', type=bool)
# =============================================================================
# Get query parameter arguments
region_from = request.args.getlist('region_from')
region_to = request.args.getlist('region_to')
sector_from = request.args.getlist('sector_from')
sector_to = request.args.getlist('sector_to')
# =============================================================================
# to_filter will match the escape chars to the corresponding region/sector to/from
# This will connect the string of %s found in r_from (one %s per argument),
# and input the correct value in the string.
to_filter = []
# Store the %s as a string. Several entries are separated by a comma
r_from = ""
r_to = ""
s_from = ""
s_to = ""
query = 'SELECT * FROM co2 WHERE'
# =============================================================================
if len(region_from) > 0:
rf = region_from[0]
rf_split = rf.split(',')
rf_string = ', '.join("'" + item + "'" for item in rf_split)
for i in rf_string:
r_from += '%s'
to_filter.append( "'" + r_from + "'" )
query += ' region_from IN (' + rf_string + ') AND'
if len(region_to) > 0:
rt = region_to[0]
rt_split = rt.split(',')
rt_string = ', '.join("'" + item + "'" for item in rt_split)
for i in rt_string:
r_to += '%s'
to_filter.append( "'" + i + "'" )
query += ' region_to IN (' + rt_string + ') AND'
if len(sector_from) > 0:
sf = sector_from[0]
sf_split = sf.split(',')
sf_string = ', '.join("'" + item + "'" for item in sf_split)
for i in sf_string:
s_from += '%s'
to_filter.append( "'" + i + "'" )
query += ' sector_from IN (' + sf_string + ') AND'
if len(sector_to) > 0:
st = sector_to[0]
st_split = st.split(',')
st_string = ', '.join("'" + item + "'" for item in st_split)
for i in st_string:
s_to += '%s'
to_filter.append( "'" + i + "'" )
query += ' sector_to IN (' + st_string + ') AND'
if query.endswith("AND"):
query = (query[:-3])
# REMOVE DEBUG FROM PRODUCTION CODE
if debug:
return response(200, query)
try:
con = Connection()
con.cur.execute(query, to_filter)
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
if not record:
return response(400, message="Bad request - Please check that your region and/or sector query is correctly entered.")
return response(200, record)
#%%
# Imports
@app.route('/api/v1/stressors/<stressor>/imports')
# "SELECT region_from, SUM(val) FROM co2 WHERE region_to='se' AND NOT region_from= 'se' GROUP BY region_from;"
# Get the regions from which emission come (region_from) to a certain region (region_to).
# The receiving region (region_to) should not include itself as an emitter of emission.
# Group the values by the regions from which the emission are sent (imported) to the select region.
def imports(stressor):
try:
region_to = request.args.get('region_to', "").lower()
except AttributeError:
return response(400, message="Bad request. The query parameter should be 'region_to'.")
query = 'SELECT region_from, SUM(val) FROM {} WHERE'.format(escape(stressor))
to_filter = []
list_stressors = ['co2', 'vl', 'economy']
stressor = stressor.lower()
if stressor not in list_stressors:
return response(400, message="Bad request - Looks like you need to provide a stressor. Please check the spelling of the stressor and ensure it is in lowercase.")
query += ' region_to=%s AND NOT'
to_filter.append(region_to)
region_from = region_to
query += ' region_from=%s GROUP BY region_from;'
to_filter.append(region_from)
try:
con = Connection()
con.cur.execute(query, to_filter)
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
if not record:
return response(400, message="Bad Request. Please ensure your syntax is correct.")
return response(200, record)
# Test URL: http://api.emissionsintrade.com/v1/stressors/economy/imports?region_to=se
#%%
# Exports
@app.route('/api/v1/stressors/<stressor>/exports')
# "SELECT region_to, SUM(val) FROM co2 WHERE region_from='se' AND NOT region_to= 'se' GROUP BY region_to;"
# Get the regions to which emissions are sent (region_to) from an
# emitting region (region_from). The emitting region (region_from) should not
# include itself as a receiver of emissions. Group the values by the regions
# to which the emissions are sent (exported).
def exports(stressor):
try:
region_from = request.args.get('region_from').lower()
except AttributeError:
return response(400, message="Bad request. The query parameter should be 'region_from'.")
query = 'SELECT region_to, SUM(val) FROM {} WHERE'.format(escape(stressor))
to_filter = []
list_stressors = ['co2', 'vl', 'economy']
stressor = stressor.lower()
if stressor not in list_stressors:
return response(400, message="Bad request - Looks like you need to provide a stressor. Please check the spelling of the stressor and ensure it is in lowercase.")
query += ' region_from=%s AND NOT'
to_filter.append(region_from)
region_to = region_from
query += ' region_to=%s GROUP BY region_to;'
to_filter.append(region_to)
try:
con = Connection()
con.cur.execute(query, to_filter)
except Exception as e:
return resource_500(str(e))
record = con.cur.fetchall()
record = [dict(row) for row in record]
if not record:
return response(400, message="Bad request - Please check that your region and/or sector query is correctly entered.")
return response(200, record)
# Test URL: http://api.emissionsintrade.com/v1/stressors/economy/exports?region_from=se
#%%
if __name__ == "__main__":
app.run(debug=True)