-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
125 lines (93 loc) · 4.1 KB
/
api.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
from flask import request
from flask_restful import Resource, Api, reqparse
import os, sys
import json
## Add convida lib and convida server lib to path
convida_lib_path = os.getcwd()
lib = os.path.join(convida_lib_path, 'COnVIDa-lib')
lib_aux = os.path.join(lib, 'lib')
sys.path.append(lib_aux)
convida_server_path = os.getcwd()
server_path = os.path.join(convida_server_path, 'COnVIDa-lib')
server_aux = os.path.join(server_path, 'server')
sys.path.append(server_aux)
from convida import COnVIDa
from regions import Regions
from datatype import DataType
from convida_server import convida_server
import pandas as pd
def init_api(app):
"""Deploys API REST for queries.
Parameters
----------
app : Flask instance
Returns
-------
"""
api = Api(app, prefix='/api', default_mediatype='application/json')
api.add_resource(DefaultRequest, '')
api.add_resource(TemporalRequest, '/temporal')
api.add_resource(RegionalRequest, '/regional')
parser = reqparse.RequestParser()
parser.add_argument('data', type=list, location='json')
parser.add_argument('regions', type=list, location='json')
parser.add_argument('start_date', type=str)
parser.add_argument('end_date', type=str)
class DefaultRequest(Resource):
def post(self):
return 'Usage -> http://localhost:8899/api/[temporal|regional]', 400
class TemporalRequest(Resource):
def post(self):
request.get_json(force=True)
args = parser.parse_args()
datasources_temporal_es = COnVIDa.get_data_items_names(DataType.TEMPORAL, language='ES')
datasources_temporal_es_indiv = []
for i in datasources_temporal_es.values():
for j in i:
datasources_temporal_es_indiv.append(j)
datasources_temporal_en = COnVIDa.get_data_items_names(DataType.TEMPORAL, language='EN')
datasources_temporal_en_indiv = []
for i in datasources_temporal_en.values():
for j in i:
datasources_temporal_en_indiv.append(j)
if all(elem in datasources_temporal_es_indiv for elem in args['data']):
lang = 'ES'
elif all(elem in datasources_temporal_en_indiv for elem in args['data']):
lang = 'EN'
else:
return "data_type error", 400
start_date = pd.to_datetime(str(args['start_date']), format='%Y-%m-%d')
end_date = pd.to_datetime(str(args['end_date']), format='%Y-%m-%d')
data = convida_server.get_data_items(data_items=args['data'],
regions=args['regions'],
start_date=start_date,
end_date=end_date, language=lang)
data.index = data.index.astype(str)
index = set(data.columns.get_level_values(0))
json_out = {}
for region in index:
json_out[region] = json.dumps(json.loads(data[region].to_json(default_handler=dict)),
sort_keys=True)
return json_out
class RegionalRequest(Resource):
def post(self):
request.get_json(force=True)
args = parser.parse_args()
datasources_regional_es = COnVIDa.get_data_items_names(DataType.GEOGRAPHICAL, language='ES')
datasources_regional_en = COnVIDa.get_data_items_names(DataType.GEOGRAPHICAL, language='EN')
lang = ''
if all(elem in list(datasources_regional_es.values())[0] for elem in args['data']):
lang = 'ES'
elif all(elem in list(datasources_regional_en.values())[0] for elem in args['data']):
lang = 'EN'
else:
return "data_type error", 400
data = convida_server.get_data_items(data_items=args['data'],
regions=args['regions'],
language=lang)
index = set(data.columns.get_level_values(0))
json_out = {}
for region in index:
json_out[region] = json.dumps(json.loads(data[region].to_json(default_handler=dict)),
sort_keys=True)
return json_out