From f9ca3d2477bce10335f7e2a4e4932b39e7b182ca Mon Sep 17 00:00:00 2001 From: Woze Parrot Date: Thu, 4 Apr 2024 23:58:18 -0400 Subject: [PATCH] feat: return only json --- tinymod/stats/api.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tinymod/stats/api.py b/tinymod/stats/api.py index ee6b40d7..c829d9f5 100644 --- a/tinymod/stats/api.py +++ b/tinymod/stats/api.py @@ -6,6 +6,9 @@ from common.helpers import getenv from common.benchmarks import CachedBenchmarks +API_INVALID_ARGUMENT = json.dumps({"error": "Invalid argument."}) +API_INVALID_COMMAND = json.dumps({"error": "Invalid command."}) + class Api: async def start(self): if not getenv("DEV", False): @@ -26,21 +29,21 @@ async def handler(self, protocol): case "get-benchmark": try: filename, system, last_n = args except ValueError: - await protocol.send("1 Invalid argument.") + await protocol.send(API_INVALID_ARGUMENT) continue # ensure last_n is an integer try: last_n = int(last_n) except ValueError: - await protocol.send("1 Invalid argument.") + await protocol.send(API_INVALID_ARGUMENT) continue if last_n < 0: - await protocol.send("1 Invalid argument.") + await protocol.send(API_INVALID_ARGUMENT) continue logging.info(f"{protocol.remote_address} requested benchmarks for {filename} on {system} for last {last_n}.") benchmarks = CachedBenchmarks.cache.get((filename, system), [])[-last_n:] benchmarks = [{"x": x, "y": y} for x, y in benchmarks] - await protocol.send("0 " + json.dumps({"filename": filename, "system": system, "benchmarks": benchmarks})) + await protocol.send(json.dumps({"filename": filename, "system": system, "benchmarks": benchmarks})) case "get-commit": - await protocol.send("0 " + json.dumps({"commit": CachedBenchmarks.curr_commit})) + await protocol.send(json.dumps({"commit": CachedBenchmarks.curr_commit})) case _: - await protocol.send("1 Invalid command.") + await protocol.send(API_INVALID_COMMAND)