reformat, added trainee debug target

This commit is contained in:
marko
2024-02-24 10:56:28 +01:00
parent b5d2655e8c
commit c4248aa2ea
2 changed files with 47 additions and 64 deletions

View File

@@ -15,6 +15,16 @@
"POST", "POST",
"--input", "testShiai.json" "--input", "testShiai.json"
] ]
} },
] {
"name": "getTrainees",
"type": "python",
"request": "launch",
"program": "testApi.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"GET",
"--endpoint", "trainees" ]
} ]
} }

View File

@@ -5,49 +5,32 @@ import json
class TestApi: class TestApi:
@staticmethod @staticmethod
def __parse_arguments(): def __parse_arguments():
import argparse import argparse
argParser = argparse.ArgumentParser( argParser = argparse.ArgumentParser("testApi")
"testApi" argParser.add_argument("method", choices=['GET', 'POST'])
) argParser.add_argument("--endpoint", default="shiai")
argParser.add_argument( argParser.add_argument("--host", default="cwsvjudo.bplaced.net")
"method", argParser.add_argument("--path", default="participo/api")
choices=['GET', 'POST'] argParser.add_argument("--key", default=None)
) argParser.add_argument("--configFile",
argParser.add_argument( type=argparse.FileType('r'),
"--endpoint", default="testApi.config.yaml")
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"] _LOG_LEVEL = ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]
argParser.add_argument( argParser.add_argument("--logLevel",
"--logLevel", choices=_LOG_LEVEL,
choices=_LOG_LEVEL, default="WARNING")
default="WARNING" argParser.add_argument("--input",
) "-i",
argParser.add_argument( type=argparse.FileType("r"),
"--input", "-i", default="-")
type=argparse.FileType("r"), argParser.add_argument("--output",
default="-" "-o",
) type=argparse.FileType("w"),
default="-")
return argParser.parse_args() return argParser.parse_args()
@@ -70,11 +53,8 @@ class TestApi:
method=self.config.method, method=self.config.method,
host=self.config.host, host=self.config.host,
url="/".join([self.config.path, self.config.endpoint]), url="/".join([self.config.path, self.config.endpoint]),
headers={ headers={"Authorization": f"Basic {self.config.key}"},
"Authorization": f"Basic {self.config.key}" input=load_json(self.config.input))
},
input=load_json(self.config.input)
)
def load_json(jsonFile): def load_json(jsonFile):
@@ -82,7 +62,9 @@ def load_json(jsonFile):
with jsonFile as f: with jsonFile as f:
return json.load(f) return json.load(f)
class apiCall: class apiCall:
@staticmethod @staticmethod
def call( def call(
method: str, method: str,
@@ -93,19 +75,15 @@ class apiCall:
) -> dict: ) -> dict:
import requests import requests
if(method=="GET"): if (method == "GET"):
r = requests.get( r = requests.get(url=f"http://{host}/{url}",
url=f"http://{host}/{url}", timeout=10,
timeout=10, headers=headers)
headers=headers elif (method == "POST"):
) r = requests.post(json=input,
elif(method=="POST"): url=f"http://{host}/{url}",
r = requests.post( timeout=10,
json=input, headers=headers)
url=f"http://{host}/{url}",
timeout=10,
headers=headers
)
else: else:
logging.error("This line should never been reached!") logging.error("This line should never been reached!")
@@ -122,11 +100,6 @@ if __name__ == "__main__":
response = testApi.apiCall() response = testApi.apiCall()
try: try:
print( print(json.dumps(response, indent=2))
json.dumps(
response,
indent=2
)
)
except Exception as e: except Exception as e:
logging.error(f"Exception {repr(e)} ({e}) while parsing json:") logging.error(f"Exception {repr(e)} ({e}) while parsing json:")