From c4248aa2ea6da1369f7fd831add7b11fafdd8c09 Mon Sep 17 00:00:00 2001 From: marko Date: Sat, 24 Feb 2024 10:56:28 +0100 Subject: [PATCH] reformat, added trainee debug target --- homepage/participo/.vscode/launch.json | 14 +++- homepage/participo/testApi.py | 97 ++++++++++---------------- 2 files changed, 47 insertions(+), 64 deletions(-) diff --git a/homepage/participo/.vscode/launch.json b/homepage/participo/.vscode/launch.json index b2fddbf..35e98dd 100644 --- a/homepage/participo/.vscode/launch.json +++ b/homepage/participo/.vscode/launch.json @@ -15,6 +15,16 @@ "POST", "--input", "testShiai.json" ] - } - ] + }, + { + "name": "getTrainees", + "type": "python", + "request": "launch", + "program": "testApi.py", + "console": "integratedTerminal", + "justMyCode": true, + "args": [ + "GET", + "--endpoint", "trainees" ] + } ] } \ No newline at end of file diff --git a/homepage/participo/testApi.py b/homepage/participo/testApi.py index a43325c..9c8456b 100755 --- a/homepage/participo/testApi.py +++ b/homepage/participo/testApi.py @@ -5,49 +5,32 @@ import json class TestApi: + @staticmethod def __parse_arguments(): import argparse - argParser = argparse.ArgumentParser( - "testApi" - ) - argParser.add_argument( - "method", - choices=['GET', 'POST'] - ) - argParser.add_argument( - "--endpoint", - default="shiai" - ) - argParser.add_argument( - "--host", - default="cwsvjudo.bplaced.net" - ) - argParser.add_argument( - "--path", - default="participo/api" - ) - argParser.add_argument( - "--key", - default=None - ) - argParser.add_argument( - "--configFile", - type=argparse.FileType('r'), - default="testApi.config.yaml" - ) + argParser = argparse.ArgumentParser("testApi") + argParser.add_argument("method", choices=['GET', 'POST']) + argParser.add_argument("--endpoint", default="shiai") + argParser.add_argument("--host", default="cwsvjudo.bplaced.net") + argParser.add_argument("--path", default="participo/api") + argParser.add_argument("--key", default=None) + argParser.add_argument("--configFile", + type=argparse.FileType('r'), + default="testApi.config.yaml") _LOG_LEVEL = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"] - argParser.add_argument( - "--logLevel", - choices=_LOG_LEVEL, - default="WARNING" - ) - argParser.add_argument( - "--input", "-i", - type=argparse.FileType("r"), - default="-" - ) + argParser.add_argument("--logLevel", + choices=_LOG_LEVEL, + default="WARNING") + argParser.add_argument("--input", + "-i", + type=argparse.FileType("r"), + default="-") + argParser.add_argument("--output", + "-o", + type=argparse.FileType("w"), + default="-") return argParser.parse_args() @@ -70,11 +53,8 @@ class TestApi: method=self.config.method, host=self.config.host, url="/".join([self.config.path, self.config.endpoint]), - headers={ - "Authorization": f"Basic {self.config.key}" - }, - input=load_json(self.config.input) - ) + headers={"Authorization": f"Basic {self.config.key}"}, + input=load_json(self.config.input)) def load_json(jsonFile): @@ -82,7 +62,9 @@ def load_json(jsonFile): with jsonFile as f: return json.load(f) + class apiCall: + @staticmethod def call( method: str, @@ -93,19 +75,15 @@ class apiCall: ) -> dict: import requests - if(method=="GET"): - r = requests.get( - url=f"http://{host}/{url}", - timeout=10, - headers=headers - ) - elif(method=="POST"): - r = requests.post( - json=input, - url=f"http://{host}/{url}", - timeout=10, - headers=headers - ) + if (method == "GET"): + r = requests.get(url=f"http://{host}/{url}", + timeout=10, + headers=headers) + elif (method == "POST"): + r = requests.post(json=input, + url=f"http://{host}/{url}", + timeout=10, + headers=headers) else: logging.error("This line should never been reached!") @@ -122,11 +100,6 @@ if __name__ == "__main__": response = testApi.apiCall() try: - print( - json.dumps( - response, - indent=2 - ) - ) + print(json.dumps(response, indent=2)) except Exception as e: logging.error(f"Exception {repr(e)} ({e}) while parsing json:")