Refactoring: Split functions in main.py
This commit is contained in:
parent
31592abfea
commit
76fd3ba93e
108
main.py
108
main.py
|
@ -101,6 +101,57 @@ def doVal(args, cfrm):
|
||||||
|
|
||||||
return HTTP200, JSON if json else XML, content
|
return HTTP200, JSON if json else XML, content
|
||||||
|
|
||||||
|
def doNearby(args):
|
||||||
|
try:
|
||||||
|
lat = float(args["lat"][0].replace(",", "."))
|
||||||
|
lon = float(args["lon"][0].replace(",", "."))
|
||||||
|
|
||||||
|
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
|
except:
|
||||||
|
content = "<h1>400 Bad Request</h1>\n"
|
||||||
|
content += "\"lat\" and \"lon\" values are required for this type of request."
|
||||||
|
return HTTP400, HTML, content
|
||||||
|
|
||||||
|
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
|
||||||
|
|
||||||
|
try:
|
||||||
|
distance = int(distance)
|
||||||
|
if distance < 0 or distance > 10000:
|
||||||
|
raise ValueError()
|
||||||
|
except:
|
||||||
|
content = "<h1>400 Bad Request</h1>\n"
|
||||||
|
content += "\"distance\" must be a value between 0 and 10000."
|
||||||
|
return HTTP400, HTML, content
|
||||||
|
|
||||||
|
json = "json" in args
|
||||||
|
|
||||||
|
try:
|
||||||
|
content = workers.closest.worker(lat, lon, distance, json)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
content = "<h1>500 Internal Server Error</h1>"
|
||||||
|
if "debug" in args:
|
||||||
|
content += repr(e)
|
||||||
|
return HTTP500, HTML, content
|
||||||
|
|
||||||
|
return HTTP200, JSON if json else XML, content
|
||||||
|
|
||||||
|
def doRadar(args):
|
||||||
|
trains = args["train"] if "train" in args else None
|
||||||
|
json = "json" in args
|
||||||
|
|
||||||
|
try:
|
||||||
|
content = workers.radar.worker(trains, json)
|
||||||
|
except Exception as e:
|
||||||
|
content = "<h1>500 Internal Server Error</h1>\n"
|
||||||
|
if "debug" in args:
|
||||||
|
content += repr(e)
|
||||||
|
return HTTP500, HTML, content
|
||||||
|
|
||||||
|
return HTTP200, JSON if json else XML, content
|
||||||
|
|
||||||
def application(env, re):
|
def application(env, re):
|
||||||
if env["REQUEST_METHOD"] == "POST":
|
if env["REQUEST_METHOD"] == "POST":
|
||||||
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
|
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
|
||||||
|
@ -144,8 +195,6 @@ def application(env, re):
|
||||||
yield "A request type must be provided.".encode()
|
yield "A request type must be provided.".encode()
|
||||||
return
|
return
|
||||||
|
|
||||||
json = "json" in args
|
|
||||||
|
|
||||||
if rtype.lower() in ["conn", "connection"]:
|
if rtype.lower() in ["conn", "connection"]:
|
||||||
status, ctype, content = doConn(args, cfrm, cto)
|
status, ctype, content = doConn(args, cfrm, cto)
|
||||||
re(status, [("Content-Type", ctype)])
|
re(status, [("Content-Type", ctype)])
|
||||||
|
@ -159,58 +208,15 @@ def application(env, re):
|
||||||
return
|
return
|
||||||
|
|
||||||
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
|
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
|
||||||
try:
|
status, ctype, content = doNearby(args)
|
||||||
lat = float(args["lat"][0].replace(",", "."))
|
re(status, [("Content-Type", ctype)])
|
||||||
lon = float(args["lon"][0].replace(",", "."))
|
yield content.encode()
|
||||||
|
|
||||||
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
|
|
||||||
raise ValueError()
|
|
||||||
|
|
||||||
except:
|
|
||||||
re("400 Bad Request", [])
|
|
||||||
yield "<h1>400 Bad Request</h1>".encode()
|
|
||||||
yield "\"lat\" and \"lon\" values are required for this type of request.".encode()
|
|
||||||
return
|
|
||||||
|
|
||||||
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
|
|
||||||
|
|
||||||
try:
|
|
||||||
distance = int(distance)
|
|
||||||
if distance < 0 or distance > 10000:
|
|
||||||
raise ValueError()
|
|
||||||
except:
|
|
||||||
re("400 Bad Request", [])
|
|
||||||
yield "<h1>400 Bad Request</h1>"
|
|
||||||
yield "\"distance\" must be a value between 0 and 10000."
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
output = workers.closest.worker(lat, lon, distance, json)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
re("500 Internal Server Error", [])
|
|
||||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
||||||
if "debug" in args:
|
|
||||||
yield e.encode()
|
|
||||||
return
|
|
||||||
|
|
||||||
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
|
||||||
yield output.encode()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
elif rtype.lower() in ["radar", "live"]:
|
elif rtype.lower() in ["radar", "live"]:
|
||||||
trains = args["train"] if "train" in args else None
|
status, ctype, content = doRadar(args)
|
||||||
try:
|
re(status, [("Content-Type", ctype)])
|
||||||
output = workers.radar.worker(trains, json)
|
yield content.encode()
|
||||||
except Exception as e:
|
|
||||||
re("500 Internal Server Error", [])
|
|
||||||
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
||||||
if "debug" in args:
|
|
||||||
yield e.encode()
|
|
||||||
return
|
|
||||||
|
|
||||||
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
|
||||||
yield output.encode()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue