More refactoring
This commit is contained in:
parent
e6270fa43a
commit
31592abfea
59
main.py
59
main.py
|
@ -71,8 +71,35 @@ def doConn(args, cfrm = None, cto = None):
|
|||
content += str(e)
|
||||
return (HTTP500, HTML, content)
|
||||
|
||||
return(HTTP200, JSON if json else XML, content)
|
||||
return (HTTP200, JSON if json else XML, content)
|
||||
|
||||
def doVal(args, cfrm):
|
||||
try:
|
||||
try:
|
||||
name = (cfrm or args["name"][0]).encode("latin-1").decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
name = cfrm or args["name"][0]
|
||||
|
||||
if not name:
|
||||
raise ValueError()
|
||||
|
||||
except:
|
||||
content = "<h1>400 Bad Request</h1>\n"
|
||||
content += "A \"name\" value is required for this type of request."
|
||||
return HTTP400, HTML, content
|
||||
|
||||
json = "json" in args
|
||||
|
||||
try:
|
||||
content = workers.val.worker(name, json)
|
||||
|
||||
except Exception as e:
|
||||
content = "<h1>500 Internal Server Error</h1>\n"
|
||||
if "debug" in args:
|
||||
content += str(e).encode()
|
||||
return HTTP500, HTML, content
|
||||
|
||||
return HTTP200, JSON if json else XML, content
|
||||
|
||||
def application(env, re):
|
||||
if env["REQUEST_METHOD"] == "POST":
|
||||
|
@ -126,33 +153,9 @@ def application(env, re):
|
|||
return
|
||||
|
||||
elif rtype.lower() in ["val", "validate"]:
|
||||
try:
|
||||
try:
|
||||
name = (cfrm or args["name"][0]).encode("latin-1").decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
name = cfrm or args["name"][0]
|
||||
|
||||
if not name:
|
||||
raise ValueError()
|
||||
|
||||
except:
|
||||
re("400 Bad Request", [])
|
||||
yield "<h1>400 Bad Request</h1>".encode()
|
||||
yield "A \"name\" value is required for this type of request.".encode()
|
||||
return
|
||||
|
||||
try:
|
||||
output = workers.val.worker(name, json)
|
||||
|
||||
except Exception as e:
|
||||
re("500 Internal Server Error", [])
|
||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
||||
if "debug" in args:
|
||||
yield str(e).encode()
|
||||
return
|
||||
|
||||
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
||||
yield output.encode()
|
||||
status, ctype, content = doVal(args, cfrm)
|
||||
re(status, [("Content-Type", ctype)])
|
||||
yield content.encode()
|
||||
return
|
||||
|
||||
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
|
||||
|
|
Loading…
Reference in a new issue