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_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
class Meb(Resource, Config):
@ -17,19 +17,20 @@ class Meb(Resource, Config):
token = request.headers.get('token', None)
application_uuid = request.headers.get('application_uuid', None)
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()
buildings = {}
for city in payload['cities']:
city_name = next(iter(city))
buildings[city_name] = {}
print(city[city_name])
for building in city[city_name]:
print(building)
# buildings[city][building] = self.export_db_factory.building_info(building, city)
# TODO: finish formatting buildings to match swagger documentation
# and change export_db_factory.building_info to use MEB database
# call when ready
return Response(json.dumps({'result': 'succeed', 'results': buildings}), status=200)
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)
# 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)
return Response(json.dumps({'error': 'unauthorized'}), status=403)

View File

@ -16,48 +16,6 @@ construction_catalog = None
usage_catalog = None
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):
"""
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:
refresh_session(session_id, token, application_uuid)
return _is_valid
def session(session_id) -> {}:
return sessions[session_id]