diff --git a/homepage/participo/api/index.php b/homepage/participo/api/index.php new file mode 100644 index 0000000..8559627 --- /dev/null +++ b/homepage/participo/api/index.php @@ -0,0 +1,13 @@ + 'Welcome to the API' +// ]); +// break; +// } +?> \ No newline at end of file diff --git a/homepage/participo/api/shiai.php b/homepage/participo/api/shiai.php index aa1a4aa..e024153 100644 --- a/homepage/participo/api/shiai.php +++ b/homepage/participo/api/shiai.php @@ -8,7 +8,9 @@ require_once("participoLib/shiai.php"); // - we send a json-formatted response header("Content-Type: application/json"); // - sending body payload -echo json_encode( - Shiai::dbSelect() +echo( + json_encode( + Shiai::dbSelect() + ) ); ?> diff --git a/homepage/participo/testApi.py b/homepage/participo/testApi.py index b84fa23..0880793 100755 --- a/homepage/participo/testApi.py +++ b/homepage/participo/testApi.py @@ -5,14 +5,60 @@ import logging import json -def init(): - logging.basicConfig(level=logging.DEBUG) - with open("testApi.config.yaml", "r") as f: - from yaml import safe_load - config = safe_load(f) - - return config +class TestApi: + @staticmethod + def __parse_arguments(): + import argparse + argParser = argparse.ArgumentParser( + "testApi" + ) + + 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" + ) + + return argParser.parse_args() + + def __init__(self): + self.config = self.__parse_arguments() + self.config.logLevel = getattr(logging, self.config.logLevel) + logging.basicConfig(level=self.config.logLevel) + + with self.config.configFile as cf: + from yaml import safe_load + configData = safe_load(cf) + + # @todo: add the attributes from the config file to the config (if not already set) + if not self.config.key and 'apiKey' in configData: + self.config.key = configData['apiKey'] + + def apiCall(self): + return apiCall.call( + host=self.config.host, + url="/".join([self.config.path, self.config.endpoint]), + headers={ + "Authorization": f"Basic {self.config.key}" + } + ) class apiCall: @staticmethod @@ -37,20 +83,19 @@ class apiCall: except: logging.error(f"failed to parse json: {r.text}") -if __name__ == "__main__": - config = init() - response = apiCall.call( - host=config["host"], - url="participo/api/users", - # url = "participo/api", - headers={ - "Authorization": f"Basic {config['apiKey']}" - } - ) +if __name__ == "__main__": + testApi = TestApi() + + response = testApi.apiCall() try: - logging.info(json.dumps(response,indent=2)) + print( + json.dumps( + response, + indent=2 + ) + ) except: - logging.error(f"failed to parse to json:") + logging.error("failed to parse to json:") logging.error(response)