Skip to content

Commit

Permalink
Removed the class
Browse files Browse the repository at this point in the history
  • Loading branch information
markmcdowall committed Jul 3, 2017
1 parent 7745b73 commit 94c0677
Showing 1 changed file with 77 additions and 78 deletions.
155 changes: 77 additions & 78 deletions rest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,70 +23,69 @@
APP = Flask(__name__)
#app.config['DEBUG'] = False

class get_help(object):
def help_usage(error_message, status_code,
parameters_required, parameters_provided):
"""
Usage Help
Description of the basic usage patterns for GET functions for the app,
including any parameters that were provided byt he user along with the
available parameters that are required/optional.
Parameters
----------
error_message : str | None
Error message detailing what has gone wrong. If there are no errors then
None should be passed.
status_code : int
HTTP status code.
parameters_required : list
List of the text names for each paramter required by the end point. An
empty list should be provided if there are no parameters required
parameters_provided : dict
Dictionary of the parameters and the matching values provided by the
user. An empyt dictionary should be passed if there were no parameters
provided by the user.
Returns
-------
str
JSON formated status message to display to the user
"""
parameters = {
'user_id' : ['User ID', 'str', 'REQUIRED'],
'file_id' : ['File ID', 'str', 'REQUIRED'],
'chrom' : ['Chromosome', 'str', 'REQUIRED'],
'start' : ['Start', 'int', 'REQUIRED'],
'end' : ['End', 'int', 'REQUIRED'],
'res' : ['Resolution', 'int', 'REQUIRED'],
'region' : ['Region ID', 'int', 'REQUIRED'],
'model' : ['Model ID', 'str', 'REQUIRED'],
'page' : ['Page number (default: 0)', 'int', 'OPTIONAL'],
'mpp' : ['Models per page (default: 10; max: 100)', 'int', 'OPTIONAL'],
}

used_param = {k : parameters[k] for k in parameters_required if k in parameters}

usage = {
'_links' : {
'_self' : request.base_url,
'_parent' : request.url_root + 'mug/api/dmp'
},
'parameters' : used_param
}
message = {
'usage' : usage,
'status_code' : status_code
}

if parameters_provided:
message['provided_parameters'] = parameters_provided

if error_message != None:
message['error'] = error_message

return message
def help_usage(error_message, status_code,
parameters_required, parameters_provided):
"""
Usage Help
Description of the basic usage patterns for GET functions for the app,
including any parameters that were provided byt he user along with the
available parameters that are required/optional.
Parameters
----------
error_message : str | None
Error message detailing what has gone wrong. If there are no errors then
None should be passed.
status_code : int
HTTP status code.
parameters_required : list
List of the text names for each paramter required by the end point. An
empty list should be provided if there are no parameters required
parameters_provided : dict
Dictionary of the parameters and the matching values provided by the
user. An empyt dictionary should be passed if there were no parameters
provided by the user.
Returns
-------
str
JSON formated status message to display to the user
"""
parameters = {
'user_id' : ['User ID', 'str', 'REQUIRED'],
'file_id' : ['File ID', 'str', 'REQUIRED'],
'chrom' : ['Chromosome', 'str', 'REQUIRED'],
'start' : ['Start', 'int', 'REQUIRED'],
'end' : ['End', 'int', 'REQUIRED'],
'res' : ['Resolution', 'int', 'REQUIRED'],
'region' : ['Region ID', 'int', 'REQUIRED'],
'model' : ['Model ID', 'str', 'REQUIRED'],
'page' : ['Page number (default: 0)', 'int', 'OPTIONAL'],
'mpp' : ['Models per page (default: 10; max: 100)', 'int', 'OPTIONAL'],
}

used_param = {k : parameters[k] for k in parameters_required if k in parameters}

usage = {
'_links' : {
'_self' : request.base_url,
'_parent' : request.url_root + 'mug/api/dmp'
},
'parameters' : used_param
}
message = {
'usage' : usage,
'status_code' : status_code
}

if parameters_provided:
message['provided_parameters'] = parameters_provided

if error_message != None:
message['error'] = error_message

return message

class GetEndPoints(Resource):
"""
Expand Down Expand Up @@ -161,11 +160,11 @@ def get(self):

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return get_help.help_usage(None, 200, params_required, {})
return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return get_help.help_usage(
return help_usage(
'MissingParameters',
400,
params_required,
Expand Down Expand Up @@ -241,11 +240,11 @@ def get(self):

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return get_help.help_usage(None, 200, params_required, {})
return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return get_help.help_usage(
return help_usage(
'MissingParameters',
400,
params_required,
Expand All @@ -256,7 +255,7 @@ def get(self):
resolution = int(resolution)
except ValueError:
# ERROR - one of the parameters is not of integer type
return get_help.help_usage(
return help_usage(
'IncorrectParameterType',
400,
params_required,
Expand Down Expand Up @@ -344,11 +343,11 @@ def get(self):

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return get_help.help_usage(None, 200, params_required, {})
return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return get_help.help_usage(
return help_usage(
'MissingParameters',
400,
params_required,
Expand All @@ -368,7 +367,7 @@ def get(self):
resolution = int(resolution)
except ValueError:
# ERROR - one of the parameters is not of integer type
return get_help.help_usage(
return help_usage(
'IncorrectParameterType',
400,
params_required,
Expand Down Expand Up @@ -457,11 +456,11 @@ def get(self):

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return get_help.help_usage(None, 200, params_required, {})
return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return get_help.help_usage(
return help_usage(
'MissingParameters',
400,
params_required,
Expand All @@ -477,7 +476,7 @@ def get(self):
resolution = int(resolution)
except ValueError:
# ERROR - one of the parameters is not of integer type
return get_help.help_usage(
return help_usage(
'IncorrectParameterType',
400,
params_required,
Expand Down Expand Up @@ -574,11 +573,11 @@ def get(self):

# Display the parameters available
if sum([x is None for x in params]) == len(params):
return get_help.help_usage(None, 200, params_required, {})
return help_usage(None, 200, params_required, {})

# ERROR - one of the required parameters is NoneType
if sum([x is not None for x in params]) != len(params):
return get_help.help_usage(
return help_usage(
'MissingParameters',
400,
params_required,
Expand All @@ -603,7 +602,7 @@ def get(self):
mpp = int(mpp)
except ValueError:
# ERROR - one of the parameters is not of integer type
return get_help.help_usage(
return help_usage(
'IncorrectParameterType',
400,
params_required,
Expand Down

0 comments on commit 94c0677

Please sign in to comment.