From 52bb8afa13f9ee9b17104cd32884f9ea63835e6e Mon Sep 17 00:00:00 2001 From: Guille Date: Fri, 28 Jul 2023 08:26:02 -0400 Subject: [PATCH] correct API --- hub_api/config.py | 6 ++++++ hub_api/control/session.py | 10 ++++------ hub_api/docs/openapi-specs.yml | 4 ++-- hub_api/helpers/session_helper.py | 6 ++++-- hub_api/tests/test_api.py | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 hub_api/tests/test_api.py diff --git a/hub_api/config.py b/hub_api/config.py index d220534..95ad1da 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -8,6 +8,7 @@ import platform from pathlib import Path from hub.persistence.db_control import DBControl +from hub.persistence.repository import Repository class Config: @@ -21,7 +22,12 @@ class Config: database_name = 'montreal_retrofit_test' self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) + self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) @property def database(self): return self._database + + @property + def repository(self): + return self._repository diff --git a/hub_api/control/session.py b/hub_api/control/session.py index 73a05b5..18fc783 100644 --- a/hub_api/control/session.py +++ b/hub_api/control/session.py @@ -39,16 +39,14 @@ class SessionStart(Resource, Config): 'application_id': user_info.application_id, 'application_uuid': application_uuid, 'ip': ip, - 'cities': [] + 'scenarios': [] } cities = self.database.cities_by_user_and_application(user_info.id, user_info.application_id) for city in cities: - session['cities'].append({ - "name": city.name, - "geometric_level_of_detail": city.level_of_detail - }) + if city.scenario not in session['scenarios']: + session['scenarios'].append(city.scenario) sessions[session_id] = session - response = Response(json.dumps({'cities': session['cities'], 'result': 'OK'}), status=200) + response = Response(json.dumps({'scenarios': session['scenarios'], 'result': 'OK'}), status=200) response.headers['session_id'] = session_id response.headers['token'] = token return response diff --git a/hub_api/docs/openapi-specs.yml b/hub_api/docs/openapi-specs.yml index c265ca9..692c55a 100644 --- a/hub_api/docs/openapi-specs.yml +++ b/hub_api/docs/openapi-specs.yml @@ -288,9 +288,9 @@ components: result: type: string example: 'succeed' - cities: + scenarios: type: array - example: [{'name': 'city 1', 'geometric_level_of_detail': '1'}, {'name': 'city 2', 'geometric_level_of_detail': '1'}] + example: ["current status", "skin retrofit", "system retrofit and pv", "skin and system retrofit with pv" ] retrofit-results: type: object properties: diff --git a/hub_api/helpers/session_helper.py b/hub_api/helpers/session_helper.py index 1e9803e..8f73ce4 100644 --- a/hub_api/helpers/session_helper.py +++ b/hub_api/helpers/session_helper.py @@ -24,9 +24,11 @@ def expired_sessions_collector(session_timeout_duration): while True: if bool(sessions): for _session in list(sessions): - _expire = datetime.datetime.strptime(sessions[session]['expire'], '%Y-%m-%d %H:%M:%S.%f') + print(_session) + _expire = datetime.datetime.strptime(_session['expire'], '%Y-%m-%d %H:%M:%S.%f') if _expire < datetime.datetime.now(): - print("session with session_id: ", session, "expired.") + print("session for user: ", _session['username'], "expired.") + del sessions[session] time.sleep(60 * int(session_timeout_duration)) diff --git a/hub_api/tests/test_api.py b/hub_api/tests/test_api.py new file mode 100644 index 0000000..af23e4f --- /dev/null +++ b/hub_api/tests/test_api.py @@ -0,0 +1,16 @@ +from unittest import TestCase + +from hub_api.config import Config +from hub.persistence.models import City, Application, CityObject, SimulationResults, User + + +class TestApi(TestCase): + def test_create_database(self): + config = Config() + Application.__table__.create(bind=config.repository.engine, checkfirst=True) + User.__table__.create(bind=config.repository.engine, checkfirst=True) + City.__table__.create(bind=config.repository.engine, checkfirst=True) + CityObject.__table__.create(bind=config.repository.engine, checkfirst=True) + SimulationResults.__table__.create(bind=config.repository.engine, checkfirst=True) + config.database.create_user('Administrator', ) + self.assertTrue(True)