refactoring testApi

This commit is contained in:
marko
2023-12-30 11:09:06 +01:00
parent f2cf7ff764
commit 13ea342be8
3 changed files with 81 additions and 21 deletions

View File

@@ -0,0 +1,13 @@
<?php
header('Content-Type: application/json');
// $endpoint = $_SERVER['REQUEST_URI'];
// switch ($endpoint){
// case '/':
// echo json_encode([
// 'message' => 'Welcome to the API'
// ]);
// break;
// }
?>

View File

@@ -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()
)
);
?>

View File

@@ -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)