Skip to content
Snippets Groups Projects

WDFN 831 - Add API call to Flask web server to retrieve a monitoring location pages site summary information from WQP

Merged Bucknell, Mary S. requested to merge mbucknell/waterdataui:wdfn-831 into main
2 unresolved threads
12 files
+ 305
6
Compare changes
  • Side-by-side
  • Inline
Files
12
+ 66
0
"""
Class and functions for querying WQP data
"""
from requests import exceptions as request_exceptions, Session
class WQPService:
"""
Provide access to WQP services
"""
def __init__(self, endpoint: str, logger):
"""
Initialize class used to create a session to the WQP endpoint
:param endpoint: - endpoint for the WQP services (should end with a slash)
:param logger: Instance of python logger
"""
self.session = Session()
self.endpoint = endpoint
self.logger = logger
def get_data(self, data_kind: str, params: dict):
"""
Retrieve data of dataKind from WQP using the query parameters. See
WQP swagger docs for allowed data_kind strings and parameters
:param data_kind : See WQP swagger docs for valid values
:param params: query parameters to be included in get request
:return
- status_code - status code returned from the service request
- reason - textual reason of responded HTTP status
- response - str text response
"""
url = f'{self.endpoint}{data_kind}'
self.logger.debug(f'Requesting data from {url} with query parameters {params}')
try:
response = self.session.get(url, params=params)
except (request_exceptions.Timeout, request_exceptions.ConnectionError) as err:
self.logger.error(f'Unable to request data from {url} with reason: {repr(err)}')
return (500, repr(err), '')
self.logger.debug(f'Received response from {url} with status_code {response.status_code}')
return (response.status_code, response.reason, response.content)
def get_monitoring_location_summary(self, monitoring_location_id: str):
"""
Retrieve the summary data for the period of record for site_id and agency_code as a csv and
return a tuple with the request status and reason and the csv string.
:param monitoring_location_id: format is agency_code-siteid
:return
- status_code - status code returned from the service request
- reason - textual reason of responded HTTP status
- response - csv contents
"""
status_code, reason, content = self.get_data('data/summary/monitoringlocation/search', {
'dataProfile': 'periodOfRecord',
'mimeType': 'csv',
'summaryYears': 'all',
'zip': 'no',
'siteid': monitoring_location_id
})
if status_code != 200:
return status_code, reason, content
return status_code, reason, content.decode('utf-8')
Loading