Skip to content

Commit

Permalink
Merge pull request #17 from internetofwater/cleanup
Browse files Browse the repository at this point in the history
Cleanup code
  • Loading branch information
webb-ben authored Sep 26, 2023
2 parents 0ccc039 + 4a4e46d commit 1b3e637
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 106 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
context: .
container_name: nldi-py
ports:
- "8080:80"
- "8081:80"
environment:
NLDI_PATH: /api/nldi
NLDI_URL: http://localhost:8081/api/nldi
Expand Down
210 changes: 157 additions & 53 deletions nldi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def get_comid_by_id(self, request: Union[APIRequest, Any],
headers = request.get_response_headers(**HEADERS)

try:
content = self.flowline_lookup.get(identifier)
feature = self.flowline_lookup.get(identifier)
features = [feature, ]
except ProviderConnectionError:
msg = 'connection error (check logs)'
return self.get_exception(
Expand All @@ -327,7 +328,8 @@ def get_comid_by_id(self, request: Union[APIRequest, Any],
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

return headers, HTTPStatus.OK, to_json(content, self.pretty_print)
content = stream_j2_template('FeatureCollection.j2', features)
return headers, HTTPStatus.OK, content

@pre_process
def get_hydrolocation(self, request: Union[APIRequest, Any]
Expand Down Expand Up @@ -416,7 +418,8 @@ def get_comid_by_position(self, request: Union[APIRequest, Any]
'NoApplicableCode', msg)

try:
content = self.flowline_lookup.get(identifier)
feature = self.flowline_lookup.get(identifier)
features = [feature, ]
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
Expand All @@ -428,7 +431,8 @@ def get_comid_by_position(self, request: Union[APIRequest, Any]
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

return headers, HTTPStatus.OK, to_json(content, self.pretty_print)
content = stream_j2_template('FeatureCollection.j2', features)
return headers, HTTPStatus.OK, content

@pre_process
def get_source_features(self, request: Union[APIRequest, Any],
Expand Down Expand Up @@ -468,21 +472,39 @@ def get_source_features(self, request: Union[APIRequest, Any],
'NoApplicableCode', msg)

plugin = self.load_plugin('FeatureLookup', source=source)
try:
content = plugin.get(identifier) if identifier else plugin.query()
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source_name}\' has not been crawled.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
if identifier:
try:
feature = plugin.get(identifier)
features = [feature, ]
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The source \'{source_name}\' has no item {identifier}.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

else:
try:
features = plugin.query()
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The source \'{source_name}\' has not been crawled.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

# content = stream_j2_template('FeatureGraph.j2', features)
content = stream_j2_template('FeatureCollection.j2', features)

_ = stream_j2_template('FeatureCollection.j2', content)
return headers, HTTPStatus.OK, _
return headers, HTTPStatus.OK, content

@pre_process
def get_basin(self, request: Union[APIRequest, Any],
Expand Down Expand Up @@ -530,11 +552,11 @@ def get_basin(self, request: Union[APIRequest, Any],
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

_ = request.params.get('simplified', 'True')
simplified = _.lower() == 'true'
_ = request.params.get('simplified', 'True').lower() == 'true'
simplified = _

_ = request.params.get('splitCatchment', 'False')
splitCatchment = _.lower() == 'true' # noqa
_ = request.params.get('splitCatchment', 'False').lower() == 'true'
splitCatchment = _

if isPoint and splitCatchment:
LOGGER.debug('Split Catchment')
Expand Down Expand Up @@ -592,8 +614,6 @@ def get_navigation_info(self, request: Union[APIRequest, Any],
'downstreamMain': url_join(nav_url, 'DM'),
'downstreamDiversions': url_join(nav_url, 'DD'),
}
if source_name == 'comid':
content.update({'pointToPoint': url_join(nav_url, 'PP')})

return headers, HTTPStatus.OK, to_json(content, self.pretty_print)

Expand Down Expand Up @@ -622,18 +642,16 @@ def get_navigation_info(self, request: Union[APIRequest, Any],
return headers, HTTPStatus.OK, to_json(content, self.pretty_print)

@pre_process
def get_navigation(self, request: Union[APIRequest, Any],
source_name: str, identifier: str,
nav_mode: str, data_source: str
) -> Tuple[dict, int, str]:
def get_fl_navigation(self, request: Union[APIRequest, Any],
source_name: str, identifier: str, nav_mode: str
) -> Tuple[dict, int, str]:
"""
Provide navigation query
:param request: A request object
:param source_name: NLDI source name
:param identifier: NLDI Source feature identifier
:param nav_mode: NLDI Navigation mode
:param data_source: NLDI output source_name
:returns: tuple of headers, status code, content
"""
Expand All @@ -642,21 +660,12 @@ def get_navigation(self, request: Union[APIRequest, Any],

headers = request.get_response_headers(**HEADERS)

try:
distance = request.params['distance']
except KeyError:
msg = 'Required request parameter \'distance\' is not present.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

start_comid = None
source_name = source_name.lower()

if source_name == 'comid':
try:
self.flowline_lookup.get(identifier)
start_comid = int(identifier)
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
Expand All @@ -668,11 +677,20 @@ def get_navigation(self, request: Union[APIRequest, Any],
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

start_comid = int(identifier)

else:
try:
source = self.crawler_source.get(source_name)
plugin = self.load_plugin('FeatureLookup', source=source)
feature = next(plugin.get(identifier))
except ProviderItemNotFoundError:
msg = f'The feature source \'{source_name}\' does not exist.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

plugin = self.load_plugin('FeatureLookup', source=source)
try:
feature = plugin.get(identifier)
start_comid = int(feature['properties']['comid'])
except ProviderQueryError:
msg = 'query error (check logs)'
Expand All @@ -685,46 +703,132 @@ def get_navigation(self, request: Union[APIRequest, Any],
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source_name}\' does not exist.'
msg = f'The source \'{source_name}\' has no item {identifier}.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

nav_results = self.func.get_navigation(
nav_mode, start_comid, distance)
try:
distance = float(request.params['distance'])
except KeyError:
msg = 'Required request parameter \'distance\' is not present.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ValueError:
msg = 'Required request parameter \'distance\' must be a number.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

nav_results = self.func.get_navigation(nav_mode, start_comid, distance)
features = self.flowline_lookup.lookup_navigation(nav_results)
content = stream_j2_template('FeatureCollection.j2', features)

source2_name = data_source.lower()
if source2_name == 'flowlines':
return headers, HTTPStatus.OK, content

@pre_process
def get_navigation(self, request: Union[APIRequest, Any],
source_name: str, identifier: str,
nav_mode: str, data_source: str
) -> Tuple[dict, int, str]:
"""
Provide navigation query
:param request: A request object
:param source_name: NLDI source name
:param identifier: NLDI Source feature identifier
:param nav_mode: NLDI Navigation mode
:param data_source: NLDI output source_name
:returns: tuple of headers, status code, content
"""
if not request.is_valid():
return self.get_exception(request)

headers = request.get_response_headers(**HEADERS)

start_comid = None
source_name = source_name.lower()

if source_name == 'comid':
try:
content = self.flowline_lookup.lookup_navigation(nav_results)
self.flowline_lookup.get(identifier)
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source2_name}\' does not exist.'
msg = f'The comid source \'{identifier}\' does not exist.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

start_comid = int(identifier)

else:
try:
source2 = self.crawler_source.get(source2_name)
plugin = self.load_plugin('FeatureLookup', source=source2)
content = plugin.lookup_navigation(nav_results)
source = self.crawler_source.get(source_name)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source_name}\' does not exist.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

plugin = self.load_plugin('FeatureLookup', source=source)
try:
feature = plugin.get(identifier)
start_comid = int(feature['properties']['comid'])
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except (KeyError, IndexError):
msg = f'The feature {identifier} from source \'{source_name}\' is not indexed.' # noqa
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source2_name}\' does not exist.'
msg = f'The source \'{source_name}\' has no item {identifier}.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

_ = stream_j2_template('FeatureCollection.j2', content)
return headers, HTTPStatus.OK, _
source2_name = data_source.lower()
try:
source2 = self.crawler_source.get(source2_name)
plugin = self.load_plugin('FeatureLookup', source=source2)
except ProviderQueryError:
msg = 'query error (check logs)'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ProviderItemNotFoundError:
msg = f'The feature source \'{source2_name}\' does not exist.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

try:
distance = float(request.params['distance'])
except KeyError:
msg = 'Required request parameter \'distance\' is not present.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)
except ValueError:
msg = 'Required request parameter \'distance\' must be a number.'
return self.get_exception(
HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format,
'NoApplicableCode', msg)

nav_results = self.func.get_navigation(nav_mode, start_comid, distance)
features = plugin.lookup_navigation(nav_results)
content = stream_j2_template('FeatureCollection.j2', features)

return headers, HTTPStatus.OK, content

def get_exception(self, status, headers, format_, code,
description) -> Tuple[dict, int, str]:
Expand Down
15 changes: 15 additions & 0 deletions nldi/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ def get_navigation_info(source_name=None, identifier=None, nav_mode=None):
API_.get_navigation_info(request, source_name, identifier, nav_mode))


@BLUEPRINT.route('/linked-data/<path:source_name>/<path:identifier>/navigation/<path:nav_mode>/flowlines') # noqa
def get_flowline_navigation(source_name=None, identifier=None, nav_mode=None): # noqa
"""
Data source flowline navigation endpoint
:param source_name: NLDI input source name
:param identifier: NLDI Source feature identifier
:param nav_mode: NLDI Navigation mode
:returns: HTTP response
"""
return get_response(API_.get_fl_navigation(
request, source_name, identifier, nav_mode))


@BLUEPRINT.route('/linked-data/<path:source_name>/<path:identifier>/navigation/<path:nav_mode>/<path:data_source>') # noqa
def get_navigation(source_name=None, identifier=None, nav_mode=None, data_source=None): # noqa
"""
Expand Down
2 changes: 1 addition & 1 deletion nldi/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

from nldi.functions.basin import get_basin
from nldi.functions.navigate import get_navigation
from nldi.lookup import _ENGINE_STORE

_ENGINE_STORE = {}
LOGGER = logging.getLogger(__name__)


Expand Down
2 changes: 2 additions & 0 deletions nldi/lookup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@
# =================================================================

"""Module containing the models for the NLDI data lookup"""

_ENGINE_STORE = {}
Loading

0 comments on commit 1b3e637

Please sign in to comment.