api_v1.4/hub_api/persistence/retrofit_results.py

58 lines
2.2 KiB
Python

import json
from flask import Response, request
from flask_restful import Resource
from hub_api.config import Config
from hub_api.helpers.session_helper import session, refresh_session
class RetrofitResults(Resource, Config):
def __init__(self):
super().__init__()
def post(self):
"""
API call for requesting a specified list of enriched persistence
"""
session_id = request.headers.get('session-id', None)
token = request.headers.get('token', None)
application_uuid = request.headers.get('application-uuid', None)
_session = refresh_session(session_id, token, application_uuid)
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
token = {'token': _session['token']}
application_id = session(session_id)['application_id']
user_id = session(session_id)['user_id']
payload = request.get_json()
if 'scenarios' not in payload:
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
# retrieve the buildings info
buildings = []
buildings_info = []
scenario_name = None
for scenario in payload['scenarios']:
scenario_name = next(iter(scenario))
for name in scenario[scenario_name]:
if name not in buildings:
buildings.append(name)
for building in buildings:
building_info = self.database.building(building, user_id, application_id, scenario_name)
self.energy_systems_catalog.get_entry(building_info.system_name)
buildings_info.append(building_info)
results = self.database.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
scenario = next(iter(results))
for building_results in results[scenario]:
values = []
for value in building_results['insel meb']:
key = next(iter(value))
values.append({key: json.loads(str(value[key]))})
building_results['insel meb'] = values
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)