Finalising tweaks before deployment.
This commit is contained in:
@@ -18,7 +18,7 @@ def fares_query(
|
|||||||
toc: str | None = None,
|
toc: str | None = None,
|
||||||
) -> list[dict[str, dict[str, str]]]:
|
) -> list[dict[str, dict[str, str]]]:
|
||||||
url = f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}"
|
url = f"https://fares.ballast-data.co.uk/fares?origin={origin}&destination={destination}"
|
||||||
url += f"&toc={toc}" if toc is not None else ""
|
url += f"&toc={toc.upper()}" if toc is not None else ""
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url=url,
|
url=url,
|
||||||
auth=(
|
auth=(
|
||||||
|
|||||||
+10
-4
@@ -35,6 +35,8 @@ COLUMN_RENAMES = {
|
|||||||
"restriction_code": "Restriction",
|
"restriction_code": "Restriction",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALID_QUERY_TERMS = ["origin", "destination", "toc"]
|
||||||
|
|
||||||
DEFAULT_TEMPLATE_PATH = Path(__file__).parents[2] / "media/template.html"
|
DEFAULT_TEMPLATE_PATH = Path(__file__).parents[2] / "media/template.html"
|
||||||
|
|
||||||
logger: Logger = Logger(name=__name__, level=INFO)
|
logger: Logger = Logger(name=__name__, level=INFO)
|
||||||
@@ -46,10 +48,10 @@ class FaresHandler(BaseHTTPRequestHandler):
|
|||||||
text = self.requestline.split(" ")[1].split("?")
|
text = self.requestline.split(" ")[1].split("?")
|
||||||
if len(text) == 1:
|
if len(text) == 1:
|
||||||
return {}
|
return {}
|
||||||
text = text[1]
|
|
||||||
return {
|
return {
|
||||||
(_s := s.split("="))[0]: _s[-1] if _s[-1] != "" else None
|
_s[0]: _s[-1] if _s[-1] != "" else None
|
||||||
for s in text.split("&")
|
for s in text[1].split("&")
|
||||||
|
if (_s := s.split("="))[0] in VALID_QUERY_TERMS
|
||||||
}
|
}
|
||||||
|
|
||||||
def content_of_GET(self, template_path: Path = DEFAULT_TEMPLATE_PATH) -> str:
|
def content_of_GET(self, template_path: Path = DEFAULT_TEMPLATE_PATH) -> str:
|
||||||
@@ -76,6 +78,7 @@ class FaresHandler(BaseHTTPRequestHandler):
|
|||||||
text = "No Fares Found."
|
text = "No Fares Found."
|
||||||
with open(template_path, "r") as rf:
|
with open(template_path, "r") as rf:
|
||||||
return rf.read().replace("<!-- Content -->", text)
|
return rf.read().replace("<!-- Content -->", text)
|
||||||
|
|
||||||
text = '<table style="margin-left: auto; margin-right: auto;">'
|
text = '<table style="margin-left: auto; margin-right: auto;">'
|
||||||
text += (
|
text += (
|
||||||
'<tr style="font-size: 1.6em;"> <th> Flow </th> <th> Fares </th> </tr>\n'
|
'<tr style="font-size: 1.6em;"> <th> Flow </th> <th> Fares </th> </tr>\n'
|
||||||
@@ -119,9 +122,11 @@ class FaresHandler(BaseHTTPRequestHandler):
|
|||||||
self.end_headers()
|
self.end_headers()
|
||||||
_ = self.wfile.write(content.encode())
|
_ = self.wfile.write(content.encode())
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except ConnectionError as ex:
|
except ConnectionError as ex:
|
||||||
logger.warning(f"{type(ex)} | {ex}")
|
logger.warning(f"{type(ex)} | {ex}")
|
||||||
self.send_response(503, "Upstream API refused to respond.")
|
self.send_response(503, "Our upstream API refused to respond.")
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.critical(f"{type(ex)} | {ex}")
|
logger.critical(f"{type(ex)} | {ex}")
|
||||||
self.send_response(
|
self.send_response(
|
||||||
@@ -129,6 +134,7 @@ class FaresHandler(BaseHTTPRequestHandler):
|
|||||||
"Something abnormal occured, apologies. It has been logged with importance.",
|
"Something abnormal occured, apologies. It has been logged with importance.",
|
||||||
)
|
)
|
||||||
raise ex
|
raise ex
|
||||||
|
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user