refactor input handling
parent
fe1d1d51f3
commit
8cd30321f2
18
server.py
18
server.py
|
@ -52,22 +52,22 @@ def api_method(function):
|
||||||
connection = sqlite3.connect(dbname)
|
connection = sqlite3.connect(dbname)
|
||||||
# read in the body from the request to a string...
|
# read in the body from the request to a string...
|
||||||
if cherrypy.request.method == "POST":
|
if cherrypy.request.method == "POST":
|
||||||
body = str(cherrypy.request.body.read(), "utf8")
|
read_in = str(cherrypy.request.body.read(), "utf8")
|
||||||
else:
|
if not read_in:
|
||||||
body = ""
|
|
||||||
# the body may be empty, not all methods require input
|
# the body may be empty, not all methods require input
|
||||||
if body:
|
body = {}
|
||||||
body = json.loads(body)
|
else:
|
||||||
if isinstance(body, dict):
|
body = json.loads(read_in)
|
||||||
|
if not isinstance(body, dict):
|
||||||
|
raise BBJParameterError("Non-JSONObject input")
|
||||||
# lowercase all of its top-level keys
|
# lowercase all of its top-level keys
|
||||||
body = {key.lower(): value for key, value in body.items()}
|
body = {key.lower(): value for key, value in body.items()}
|
||||||
|
else:
|
||||||
|
body = {}
|
||||||
|
|
||||||
username = cherrypy.request.headers.get("User")
|
username = cherrypy.request.headers.get("User")
|
||||||
auth = cherrypy.request.headers.get("Auth")
|
auth = cherrypy.request.headers.get("Auth")
|
||||||
|
|
||||||
if debug:
|
|
||||||
print("\n\n\nBody: {}\n\ne----------".format(body))
|
|
||||||
|
|
||||||
if (username and not auth) or (auth and not username):
|
if (username and not auth) or (auth and not username):
|
||||||
raise BBJParameterError(
|
raise BBJParameterError(
|
||||||
"User or Auth was given without the other.")
|
"User or Auth was given without the other.")
|
||||||
|
|
Loading…
Reference in New Issue