Refactor:

Correct swagger definition.
MEB uses body, so should be a post method not a get according to REST definition.
active_session now refresh the session also if valid.
partial implementation for meb.
This commit is contained in:
Guille Gutierrez 2023-03-13 14:21:43 -04:00
parent 80337a5cc5
commit aa76eff3ab
2 changed files with 20 additions and 57 deletions

View File

@ -2,7 +2,7 @@ import json
from flask import Response, request from flask import Response, request
from flask_restful import Resource from flask_restful import Resource
from hub_api.helpers.session_helper import active_session, refresh_session from hub_api.helpers.session_helper import active_session, session
from hub_api.config import Config from hub_api.config import Config
class Meb(Resource, Config): class Meb(Resource, Config):
@ -17,19 +17,20 @@ class Meb(Resource, Config):
token = request.headers.get('token', None) token = request.headers.get('token', None)
application_uuid = request.headers.get('application_uuid', None) application_uuid = request.headers.get('application_uuid', None)
if active_session(session_id, token, application_uuid): if active_session(session_id, token, application_uuid):
application_id = session(session_id)['application_id']
user_id = session(session_id)['user_id']
payload = request.get_json() payload = request.get_json()
buildings = {} results = self.export_db_factory.results(user_id, application_id, payload)
for city in payload['cities']: if results == {}:
city_name = next(iter(city)) # no data found for the given parameters
buildings[city_name] = {} return Response(json.dumps({'result': 'succeed', 'results': results}), status=200)
print(city[city_name]) # deserialize the response to return pure json
for building in city[city_name]: city_name = next(iter(results))
print(building) for building_results in results[city_name]:
# buildings[city][building] = self.export_db_factory.building_info(building, city) values = []
for value in building_results['insel meb']:
# TODO: finish formatting buildings to match swagger documentation key = next(iter(value))
# and change export_db_factory.building_info to use MEB database values.append({key: json.loads(value[key])})
# call when ready building_results['insel meb'] = values
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200)
return Response(json.dumps({'result': 'succeed', 'results': buildings}), status=200)
return Response(json.dumps({'error': 'unauthorized'}), status=403) return Response(json.dumps({'error': 'unauthorized'}), status=403)

View File

@ -16,48 +16,6 @@ construction_catalog = None
usage_catalog = None usage_catalog = None
debug_mode = False debug_mode = False
class SessionData:
def __init__(self, session):
self._session = session
@property
def city(self):
return self._session['city']
@property
def id(self):
return self._session['session_id']
@property
def token(self):
return self._session['token']
@property
def headers(self):
return {'session_id': str(self.id), 'token': str(self.token)}
@property
def greenery_percentage(self):
return self._session['greenery_percentage']
@greenery_percentage.setter
def greenery_percentage(self, value):
self._session['greenery_percentage'] = value
@property
def greenery_catalog(self):
return greenery_catalog
@property
def construction_catalog(self):
return construction_catalog
@property
def usage_catalog(self):
return usage_catalog
def expired_sessions_collector(session_timeout_duration): def expired_sessions_collector(session_timeout_duration):
""" """
Goes through each session in sessions and removes expired session(s) Goes through each session in sessions and removes expired session(s)
@ -113,3 +71,7 @@ def active_session(session_id, token, application_uuid):
if _is_valid: if _is_valid:
refresh_session(session_id, token, application_uuid) refresh_session(session_id, token, application_uuid)
return _is_valid return _is_valid
def session(session_id) -> {}:
return sessions[session_id]