Skip to content

Instantly share code, notes, and snippets.

@nmwalsh
Created December 7, 2017 02:53
Show Gist options
  • Save nmwalsh/1c236b94fe54d3fa4c6a7cfc182cd4c6 to your computer and use it in GitHub Desktop.
Save nmwalsh/1c236b94fe54d3fa4c6a7cfc182cd4c6 to your computer and use it in GitHub Desktop.
class PredictsResource(object):
def on_get(self, req, resp):
"""Handles GET requests"""
resp.status = falcon.HTTP_200 # This is the default status
resp.body = ('\nThis is the PREDICT endpoint. \n'
'Both requests and responses are served in JSON. \n'
'\n'
'INPUT: Flower Lengths (in cm) \n'
' "sepal_length":[num] \n'
' "sepal_width": [num] \n'
' "petal_length":[num] \n'
' "petal_width": [num] \n\n'
'OUTPUT: Prediction (Species) \n'
' "Species": [string] \n\n')
def on_post(self, req, resp):
"""Handles POST requests"""
try:
raw_json = req.stream.read()
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400,
'Error',
ex.message)
try:
result_json = json.loads(raw_json.decode(), encoding='utf-8')
# For Python 2.x, replace with
# result_json = json.loads(raw_json, encoding='utf-8')
except ValueError:
raise falcon.HTTPError(falcon.HTTP_400,
'Malformed JSON',
'Could not decode the request body. The '
'JSON was incorrect.')
resp.status = falcon.HTTP_200
resp.body = json.dumps(invoke_predict(raw_json))
# For Python 2.x, replace with
# resp.body = json.dumps(invoke_predict(raw_json), encoding='utf-8') encoding not necessary in python3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment