From 1540d8893bd69afe5bb0d10e6fb97ae15160f43f Mon Sep 17 00:00:00 2001 From: Travis Rivers <travrivers88@gmail.com> Date: Fri, 21 Feb 2020 10:12:07 -0700 Subject: [PATCH] update parse method to take advantage of defaults in webservicequery object --- geomagio/webservice/data.py | 80 ++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/geomagio/webservice/data.py b/geomagio/webservice/data.py index 9dc1a5d0..e28bc5a4 100644 --- a/geomagio/webservice/data.py +++ b/geomagio/webservice/data.py @@ -5,16 +5,10 @@ from obspy import UTCDateTime from collections import OrderedDict from json import dumps - - from geomagio.edge import EdgeFactory from geomagio.iaga2002 import IAGA2002Writer from geomagio.imfjson import IMFJSONWriter -DEFAULT_DATA_TYPE = "variation" -DEFAULT_ELEMENTS = ("X", "Y", "Z", "F") -DEFAULT_OUTPUT_FORMAT = "iaga2002" -DEFAULT_SAMPLING_PERIOD = "60" ERROR_CODE_MESSAGES = { 204: "No Data", 400: "Bad Request", @@ -22,7 +16,7 @@ ERROR_CODE_MESSAGES = { 409: "Conflict", 500: "Internal Server Error", 501: "Not Implemented", - 503: "Service Unavailable", + 503: "Service Unavailable" } VALID_DATA_TYPES = ["variation", "adjusted", "quasi-definitive", "definitive"] VALID_OUTPUT_FORMATS = ["iaga2002", "json"] @@ -30,9 +24,10 @@ VALID_SAMPLING_PERIODS = ["1", "60"] blueprint = Blueprint("data", __name__) factory = EdgeFactory( - host='cwbpub.cr.usgs.gov', - port=2060, - write_port=7981) + host=os.getenv('host', 'cwbpub.cr.usgs.gov'), + port=os.getenv('port', 2060), + write_port=os.getenv('write_port', 7981) + ) def init_app(app: Flask): global blueprint @@ -40,7 +35,6 @@ def init_app(app: Flask): app.register_blueprint(blueprint) - class WebServiceQuery(object): """Query parameters for a web service request. Parameters @@ -63,13 +57,12 @@ class WebServiceQuery(object): output format. default 'iaga2002'. """ - def __init__( self, observatory_id=None, starttime=None, endtime=None, - elements=None, + elements=("X", "Y", "Z", "F"), sampling_period=60, data_type="variation", output_format="iaga2002", @@ -111,7 +104,6 @@ class WebServiceQuery(object): " Valid values are: %s" % (self.output_format, VALID_OUTPUT_FORMATS) ) - @blueprint.route("/data", methods=["GET"]) def get_data(): query_params = request.args @@ -130,7 +122,12 @@ def get_data(): error_body = error(400, message, parsed_query, url) return error_body - timeseries = get_timeseries(parsed_query) + try: + timeseries = get_timeseries(parsed_query) + except Exception as e: + message = str(e) + error_body = error(500, message, parsed_query, url) + return error_body return format_timeseries(timeseries, parsed_query) @@ -147,14 +144,14 @@ def http_error(code, message, query, url): http_error_body = json_error(code, message, url) return http_error_body else: - http_error_body = iaga2002_error(code, message) + http_error_body = iaga2002_error(code, message, url) return http_error_body def json_error(code, message, url): error_dict = OrderedDict() error_dict['type'] = "Error" error_dict['metadata'] = OrderedDict() - error_dict['metadata']['status'] = 400 + error_dict['metadata']['status'] = code date = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ") error_dict['metadata']['generated'] = date error_dict['metadata']['url'] = url @@ -168,15 +165,14 @@ def json_error(code, message, url): def iaga2002_error(code, message, url): status_message = ERROR_CODE_MESSAGES[code] - error_body = 'Error ' + str(code) + ': ' + status_message + \ - '\n\n' + message + '\n\n' + \ - 'Usage details are available from ' + \ - 'http://geomag.usgs.gov/ws/edge/ \n\n' + \ - 'Request:\n' + \ - url + '\n\n' + \ - 'Request Submitted:\n' + \ - datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") + '\n\n' - + error_body = 'Error ' + str(code) + ': ' \ + + status_message + '\n\n' + message + '\n\n'\ + + 'Usage details are available from '\ + + 'http://geomag.usgs.gov/ws/edge/ \n\n'\ + + 'Request:\n' + url + '\n\n' + 'Request Submitted:\n'\ + + datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") + '\n' + + error_body = Response(error_body, mimetype="text/plain") return error_body def parse_query(query): @@ -197,8 +193,10 @@ def parse_query(query): WebServiceException if any parameters are not supported. """ + # Create web service query object + params = WebServiceQuery() - # Get values + # Get and assign values if not query.get("endtime"): now = datetime.now() today = UTCDateTime(now.year, now.month, now.day, 0) @@ -211,38 +209,30 @@ def parse_query(query): else: start_time = UTCDateTime(query.get("starttime")) - if not query.get("sampling_period"): - sampling_period = DEFAULT_SAMPLING_PERIOD - else: + if query.get("sampling_period"): sampling_period = query.get("sampling_period") + params.sampling_period = sampling_period - if not query.get("format"): - format = DEFAULT_OUTPUT_FORMAT - else: + if query.get("format"): format = query.get("format") + params.output_format = format observatory = query.get("observatory") - if not query.get("channels"): - channels = DEFAULT_ELEMENTS - else: + if query.get("channels"): channels = query.get("channels").split(",") + params.elements = channels - type = query.get("type") - - params = WebServiceQuery() + if query.get("type"): + type = query.get("type") + params.data_type = type params.observatory_id = observatory params.starttime = start_time params.endtime = end_time - params.elements = channels - params.sampling_period = sampling_period - params.data_type = type - params.output_format = format return params - def get_timeseries(query): """ Parameters @@ -271,7 +261,6 @@ def get_timeseries(query): return timeseries - def format_timeseries(timeseries, query): """Formats timeseries into JSON or IAGA data @@ -300,7 +289,6 @@ def format_timeseries(timeseries, query): return iaga_output - class WebServiceException(Exception): """Base class for exceptions thrown by web services.""" pass -- GitLab