2023-02-24 16:57:51 -05:00
|
|
|
import json
|
2023-04-12 15:26:56 -04:00
|
|
|
|
2023-02-24 16:57:51 -05:00
|
|
|
from flask import Response, request
|
|
|
|
from flask_restful import Resource
|
|
|
|
|
|
|
|
from hub_api.config import Config
|
2023-04-12 15:26:56 -04:00
|
|
|
from hub_api.helpers.session_helper import session, refresh_session
|
|
|
|
|
2023-02-24 16:57:51 -05:00
|
|
|
|
2023-03-10 10:15:20 -05:00
|
|
|
class Meb(Resource, Config):
|
2023-02-24 16:57:51 -05:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2023-03-13 11:56:05 -04:00
|
|
|
def post(self):
|
2023-03-10 10:15:20 -05:00
|
|
|
"""
|
2023-02-24 16:57:51 -05:00
|
|
|
API call for requesting a specified list of enriched buildings
|
2023-03-10 10:15:20 -05:00
|
|
|
"""
|
2023-02-24 16:57:51 -05:00
|
|
|
session_id = request.headers.get('session_id', None)
|
|
|
|
token = request.headers.get('token', None)
|
|
|
|
application_uuid = request.headers.get('application_uuid', None)
|
2023-04-12 15:26:56 -04:00
|
|
|
_session = refresh_session(session_id, token, application_uuid)
|
|
|
|
if _session is None:
|
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-04-13 09:49:10 -04:00
|
|
|
token = {'token': _session['token']}
|
2023-04-12 15:26:56 -04:00
|
|
|
application_id = session(session_id)['application_id']
|
|
|
|
user_id = session(session_id)['user_id']
|
2023-04-13 09:49:10 -04:00
|
|
|
|
2023-04-12 15:26:56 -04:00
|
|
|
payload = request.get_json()
|
|
|
|
results = self.export_db_factory.results(user_id, application_id, payload)
|
|
|
|
if results == {}:
|
|
|
|
# no data found for the given parameters
|
|
|
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
|
|
|
# deserialize the response to return pure json
|
|
|
|
city_name = next(iter(results))
|
|
|
|
for building_results in results[city_name]:
|
|
|
|
values = []
|
|
|
|
for value in building_results['insel meb']:
|
|
|
|
key = next(iter(value))
|
|
|
|
values.append({key: json.loads(value[key])})
|
|
|
|
building_results['insel meb'] = values
|
|
|
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|