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
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
from hub_api.control.uptime import Uptime
from hub_api.buildings.meb import Meb
from hub_api.geolocation.reverse import Reverse
from hub_api.buildings.retrofit_results import RetrofitResults
sh.begin_time = datetime.datetime.now()
app = flask.Flask('cerc_api')
@ -30,13 +29,11 @@ api.add_resource(Uptime, '/v1.4/uptime')
# Session
api.add_resource(SessionStart, '/v1.4/session/start')
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
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:
swagger_config = {

View File

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

View File

@ -7,8 +7,7 @@ import os
import platform
from pathlib import Path
from hub.exports.db_factory import DBFactory as CityExportFactory
from hub.imports.db_factory import DBFactory
from hub.persistence.db_control import DBControl
class Config:
@ -16,12 +15,13 @@ class Config:
def __init__(self):
dotenv_path = "{}/.env".format(os.path.expanduser('~'))
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'
database_name = 'persistence_test'
database_name = 'hub_unittests'
self.export_db_factory = CityExportFactory(db_name=database_name, app_env=environment,
dotenv_path=dotenv_path)
self.import_db_factory = DBFactory(db_name=database_name, app_env=environment,
dotenv_path=dotenv_path)
self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
@property
def database(self):
return self._database

File diff suppressed because it is too large Load Diff

View File

@ -58,7 +58,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/login_succeed'
$ref: '#/components/schemas/login-succeed'
headers:
session_id:
type: string
@ -76,7 +76,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/unauthorized'
/v1.4/session/keep_alive:
/v1.4/session/keep-alive:
put:
security:
- session_id: []
@ -140,7 +140,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/unauthorized'
/v1.4/buildings/meb:
/v1.4/buildings/retrofit-results:
post:
security:
- session_id: [ ]
@ -168,44 +168,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/meb'
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'
$ref: '#/components/schemas/retrofit-results'
headers:
token:
type: string
@ -240,18 +203,6 @@ components:
type: string
format: hh:mm:ss.ms
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:
type: object
properties:
@ -264,7 +215,7 @@ components:
result:
type: string
example: 'succeed'
login_succeed:
login-succeed:
type: object
properties:
result:
@ -273,7 +224,7 @@ components:
cities:
type: array
example: [{'name': 'city 1', 'geometric_level_of_detail': '1'}, {'name': 'city 2', 'geometric_level_of_detail': '1'}]
meb:
retrofit-results:
type: object
properties:
result:
@ -281,7 +232,7 @@ components:
example: 'succeed'
results:
type: object
example: '{
example: {
"result":"succeed",
"results":{
"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
debug_mode = False
def expired_sessions_collector(session_timeout_duration):
"""
Goes through each session in sessions and removes expired session(s)

View File

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