refactoring testApi
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user