From 80337a5cc5774073c176990a1b03e30dbadfc232 Mon Sep 17 00:00:00 2001 From: guille Date: Mon, 13 Mar 2023 11:56:05 -0400 Subject: [PATCH] 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. --- hub_api/buildings/meb.py | 14 +++++++------- hub_api/control/session.py | 2 +- hub_api/docs/openapi-specs.yml | 27 +++++++++++++++------------ hub_api/helpers/session_helper.py | 5 ++++- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/hub_api/buildings/meb.py b/hub_api/buildings/meb.py index e08e3be..732a2c7 100644 --- a/hub_api/buildings/meb.py +++ b/hub_api/buildings/meb.py @@ -9,23 +9,23 @@ class Meb(Resource, Config): def __init__(self): super().__init__() - def get(self): + def post(self): """ API call for requesting a specified list of enriched buildings """ session_id = request.headers.get('session_id', None) token = request.headers.get('token', None) application_uuid = request.headers.get('application_uuid', None) - if active_session(session_id, token, application_uuid): - refresh_session(session_id, token, application_uuid) payload = request.get_json() buildings = {} - for city in payload['cities']: - buildings[city] = {} - for building in payload['cities'][city]: - buildings[city][building] = self.export_db_factory.building_info(building, city) + 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 diff --git a/hub_api/control/session.py b/hub_api/control/session.py index 2d9eaf5..feade4a 100644 --- a/hub_api/control/session.py +++ b/hub_api/control/session.py @@ -38,7 +38,7 @@ class SessionStart(Resource, Config): 'ip': ip, 'cities': [] } - cities = self.export_db_factory.cities_by_user(user_info.id) + cities = self.export_db_factory.cities_by_user_and_application(user_info.id, user_info.application_id) for city in cities: session['cities'].append({ "name": city.name, diff --git a/hub_api/docs/openapi-specs.yml b/hub_api/docs/openapi-specs.yml index 055d223..bf079ee 100644 --- a/hub_api/docs/openapi-specs.yml +++ b/hub_api/docs/openapi-specs.yml @@ -140,19 +140,22 @@ paths: schema: $ref: '#/components/schemas/unauthorized' /v1.4/buildings/meb: - get: + post: security: - session_id: [ ] - token: [ ] - application_uuid: [ ] - parameters: - - in: body - name: data - schema: - type: string - example: [{"city_1" :["building_1", "building_2"]}, {"city_2" :["building_3", "building_8"]}] - required: true - description: Json containing the city name and the buildings whose monthly energy balance is requested. + requestBody: + content: + application/json: + schema: + required: + - cities + properties: + cities: + type: object + description: Cities list + example: { "cities": [ { "city_1": [ "building_1", "building_2" ] }, { "city_2": [ "building_3", "building_8" ] } ] } tags: - Buildings monthly energy balance meb summary: Retrieve the monthly energy balance results for the given buildings in the given cities @@ -227,8 +230,8 @@ components: type: string example: 'succeed' results: - type: string - example: '[ + type: object + example: '{"results":[ { "city_1":{ "building_1":{ @@ -421,4 +424,4 @@ components: } } } - ]' \ No newline at end of file + ]}' \ No newline at end of file diff --git a/hub_api/helpers/session_helper.py b/hub_api/helpers/session_helper.py index 536492d..12686d1 100644 --- a/hub_api/helpers/session_helper.py +++ b/hub_api/helpers/session_helper.py @@ -109,4 +109,7 @@ def refresh_session(session_id, token, application_uuid): def active_session(session_id, token, application_uuid): - return _validate_session(session_id=session_id, token=token, application_uuid=application_uuid) + _is_valid = _validate_session(session_id=session_id, token=token, application_uuid=application_uuid) + if _is_valid: + refresh_session(session_id, token, application_uuid) + return _is_valid