correct API

This commit is contained in:
Guille Gutierrez 2023-07-20 20:15:24 -04:00
parent d02b1cd838
commit 68ae20f85d
8 changed files with 23 additions and 26572 deletions

View File

@ -17,8 +17,7 @@ import threading
import hub_api.helpers.session_helper as sh import hub_api.helpers.session_helper as sh
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
from hub_api.control.uptime import Uptime from hub_api.control.uptime import Uptime
from hub_api.buildings.meb import Meb from hub_api.buildings.retrofit_results import RetrofitResults
from hub_api.geolocation.reverse import Reverse
sh.begin_time = datetime.datetime.now() sh.begin_time = datetime.datetime.now()
app = flask.Flask('cerc_api') app = flask.Flask('cerc_api')
@ -30,13 +29,11 @@ api.add_resource(Uptime, '/v1.4/uptime')
# Session # Session
api.add_resource(SessionStart, '/v1.4/session/start') api.add_resource(SessionStart, '/v1.4/session/start')
api.add_resource(SessionEnd, '/v1.4/session/end') api.add_resource(SessionEnd, '/v1.4/session/end')
api.add_resource(KeepSessionAlive, '/v1.4/session/keep_alive') api.add_resource(KeepSessionAlive, '/v1.4/session/keep-alive')
# Buildings # Buildings
api.add_resource(Meb, '/v1.4/buildings/meb') api.add_resource(RetrofitResults, '/v1.4/buildings/retrofit-results')
# GeoLocation
api.add_resource(Reverse, '/v1.4/geolocation/reverse/<latitude>/<longitude>', endpoint='reverse')
with open("hub_api/docs/openapi-specs.yml", "r") as stream: with open("hub_api/docs/openapi-specs.yml", "r") as stream:
swagger_config = { swagger_config = {

View File

@ -7,7 +7,7 @@ from hub_api.config import Config
from hub_api.helpers.session_helper import session, refresh_session from hub_api.helpers.session_helper import session, refresh_session
class Meb(Resource, Config): class RetrofitResults(Resource, Config):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -26,7 +26,7 @@ class Meb(Resource, Config):
user_id = session(session_id)['user_id'] user_id = session(session_id)['user_id']
payload = request.get_json() payload = request.get_json()
results = self.export_db_factory.results(user_id, application_id, payload) results = self.database.results.results(user_id, application_id, payload)
if results == {}: if results == {}:
# no data found for the given parameters # no data found for the given parameters
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)

View File

@ -7,8 +7,7 @@ import os
import platform import platform
from pathlib import Path from pathlib import Path
from hub.exports.db_factory import DBFactory as CityExportFactory from hub.persistence.db_control import DBControl
from hub.imports.db_factory import DBFactory
class Config: class Config:
@ -16,12 +15,13 @@ class Config:
def __init__(self): def __init__(self):
dotenv_path = "{}/.env".format(os.path.expanduser('~')) dotenv_path = "{}/.env".format(os.path.expanduser('~'))
if platform.system() == 'Linux': if platform.system() == 'Linux':
dotenv_path = Path('/usr/local/etc/hub/.env').resolve() dotenv_path = Path('/home/guille/.local/etc/hub/.env').resolve()
environment = 'TEST' environment = 'TEST'
database_name = 'persistence_test' database_name = 'hub_unittests'
self.export_db_factory = CityExportFactory(db_name=database_name, app_env=environment, self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
dotenv_path=dotenv_path)
self.import_db_factory = DBFactory(db_name=database_name, app_env=environment, @property
dotenv_path=dotenv_path) def database(self):
return self._database

File diff suppressed because it is too large Load Diff

View File

@ -58,7 +58,7 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/login_succeed' $ref: '#/components/schemas/login-succeed'
headers: headers:
session_id: session_id:
type: string type: string
@ -76,7 +76,7 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/unauthorized' $ref: '#/components/schemas/unauthorized'
/v1.4/session/keep_alive: /v1.4/session/keep-alive:
put: put:
security: security:
- session_id: [] - session_id: []
@ -140,7 +140,7 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/unauthorized' $ref: '#/components/schemas/unauthorized'
/v1.4/buildings/meb: /v1.4/buildings/retrofit-results:
post: post:
security: security:
- session_id: [ ] - session_id: [ ]
@ -168,44 +168,7 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/meb' $ref: '#/components/schemas/retrofit-results'
headers:
token:
type: string
format: uuid
description: Token expected in next operation header
example: '77e1c83b-7bb0-437b-bc50-a7a58e5660ac'
'403':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/unauthorized'
/v1.4/geolocation/reverse/{latitude}/{longitude}:
get:
parameters:
- in: path
name: latitude
schema:
type: string
required: true
- in: path
name: longitude
schema:
type: string
required: true
tags:
- Reverse geolocation information
summary: Retrieve the geolocation information from the given latitude and longitude
operationId: reverse
description: Retrieve the geolocation information from the given latitude and longitude and renew the token
responses:
'200':
description: Succeed
content:
application/json:
schema:
$ref: '#/components/schemas/reverse'
headers: headers:
token: token:
type: string type: string
@ -240,18 +203,6 @@ components:
type: string type: string
format: hh:mm:ss.ms format: hh:mm:ss.ms
example: '00:09:53.600281' example: '00:09:53.600281'
reverse:
type: object
properties:
country:
type: string
example: 'CA'
city:
type: string
example: 'Montreal'
climate_zone:
type: string
example: '6'
unauthorized: unauthorized:
type: object type: object
properties: properties:
@ -264,7 +215,7 @@ components:
result: result:
type: string type: string
example: 'succeed' example: 'succeed'
login_succeed: login-succeed:
type: object type: object
properties: properties:
result: result:
@ -273,7 +224,7 @@ components:
cities: cities:
type: array type: array
example: [{'name': 'city 1', 'geometric_level_of_detail': '1'}, {'name': 'city 2', 'geometric_level_of_detail': '1'}] example: [{'name': 'city 1', 'geometric_level_of_detail': '1'}, {'name': 'city 2', 'geometric_level_of_detail': '1'}]
meb: retrofit-results:
type: object type: object
properties: properties:
result: result:
@ -281,7 +232,7 @@ components:
example: 'succeed' example: 'succeed'
results: results:
type: object type: object
example: '{ example: {
"result":"succeed", "result":"succeed",
"results":{ "results":{
"Montreal current status":[ "Montreal current status":[
@ -381,4 +332,4 @@ components:
} }
] ]
} }
}' }

View File

@ -1,43 +0,0 @@
import json
import math
from pathlib import Path
from flask import Response, request
from flask.views import MethodView
from hub_api.config import Config
from hub_api.helpers.session_helper import refresh_session
class Reverse(MethodView, Config):
def __init__(self):
super().__init__()
self._reverse_path = Path(Path(__file__).parent.parent / 'data/cities15000.txt').resolve()
def get(self, latitude: float, longitude: float):
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']}
latitude = float(latitude)
longitude = float(longitude)
distance = math.inf
country = 'unknown'
city = 'unknown'
with open(self._reverse_path, 'r') as f:
for line_number, line in enumerate(f):
fields = line.split('\t')
file_city_name = fields[1]
file_latitude = float(fields[4])
file_longitude = float(fields[5])
file_country_code = fields[8]
new_distance = math.sqrt(pow((latitude-file_latitude),2) + pow((longitude-file_longitude),2))
if distance > new_distance:
distance = new_distance
country = file_country_code
city = file_city_name
return Response(json.dumps({'country': country, 'city':city}), status=200, headers=token)

View File

@ -16,6 +16,7 @@ construction_catalog = None
usage_catalog = None usage_catalog = None
debug_mode = False debug_mode = False
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)

View File

@ -27,3 +27,4 @@ flasgger
cerc-hub cerc-hub
python-dotenv python-dotenv
mapbox_earcut mapbox_earcut
cerc-costs