Commit 808e602d authored by Jerome Touvier's avatar Jerome Touvier
Browse files

code refactoring

parent eef664fc
VERSION = "1.0.0"
parameters = {
"network": None,
"station": None,
"location": None,
"channel": None,
"starttime": None,
"endtime": None,
"net": "*",
"sta": "*",
"loc": "*",
"cha": "*",
"start": None,
"end": None,
"time": None,
"nodata": "204",
}
class Parameters:
def __init__(self):
self.network = None
self.station = None
self.location = None
self.channel = None
self.starttime = None
self.endtime = None
self.net = "*"
self.sta = "*"
self.loc = "*"
self.cha = "*"
self.start = None
self.end = None
self.time = None
self.nodata = "204"
self.constraints = {
"booleans": [],
"floats": [],
"not_none": [],
}
def todict(self):
return self.__dict__
ALIAS = [
......
......@@ -5,7 +5,7 @@ from copy import copy
from multiprocessing import Process, Queue
from apps.constants import ALIAS
from apps.constants import parameters
from apps.constants import Parameters
from apps.globals import Error
from apps.globals import HTTP
from apps.globals import TIMEOUT
......@@ -64,7 +64,7 @@ def check_parameters(params):
def checks_get(request):
# get default parameters
params = copy(parameters)
params = Parameters().todict()
# check if the parameters are unknown
(p, result) = check_request(request, params, ALIAS)
......
......@@ -136,32 +136,27 @@ def check_request(request, params, alias):
return (keys, {"msg": HTTP._200_, "details": Error.VALID_PARAM, "code": 200})
def check_base_parameters(
params, max_days=None, not_none=None, booleans=None, floats=None
):
def check_base_parameters(params, max_days=None):
# Search for missing mandatory parameters
if not_none is not None:
for key in not_none:
if params[key] is None:
return error_param(params, Error.MISSING + key)
for key in params["constraints"]["not_none"]:
if params[key] is None:
return error_param(params, Error.MISSING + key)
# Boolean parameter validations
if booleans is not None:
for key in booleans:
val = params[key]
if not is_valid_bool_string(val):
return error_param(params, f"Invalid {key} value: {val} {Error.BOOL}.")
params[key] = True if val.lower() in STRING_TRUE else False
for key in params["constraints"]["booleans"]:
val = params[key]
if not is_valid_bool_string(val):
return error_param(params, f"Invalid {key} value: {val} {Error.BOOL}.")
params[key] = True if val.lower() in STRING_TRUE else False
# Float parameter validations
if floats is not None:
for key in floats:
val = params[key]
if is_valid_float(val):
params[key] = float(val)
elif val is not None:
return error_param(params, f"Invalid {key} value: {val}")
for key in params["constraints"]["floats"]:
val = params[key]
if is_valid_float(val):
params[key] = float(val)
elif val is not None:
return error_param(params, f"Invalid {key} value: {val}")
# Station validations
network = params["network"].split(",")
......
......@@ -113,6 +113,11 @@ class MyTest(unittest.TestCase):
p1["end"] = None
p1["time"] = "2018-02-12T04:08:02"
p1["nodata"] = "204"
p1["constraints"] = {
"booleans": [],
"floats": [],
"not_none": [],
}
valid_param = {"msg": HTTP._200_, "details": Error.VALID_PARAM, "code": 200}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment