refactoring testApi
This commit is contained in:
13
homepage/participo/api/index.php
Normal file
13
homepage/participo/api/index.php
Normal 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;
|
||||||
|
// }
|
||||||
|
?>
|
||||||
@@ -8,7 +8,9 @@ require_once("participoLib/shiai.php");
|
|||||||
// - we send a json-formatted response
|
// - we send a json-formatted response
|
||||||
header("Content-Type: application/json");
|
header("Content-Type: application/json");
|
||||||
// - sending body payload
|
// - sending body payload
|
||||||
echo json_encode(
|
echo(
|
||||||
Shiai::dbSelect()
|
json_encode(
|
||||||
|
Shiai::dbSelect()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -5,14 +5,60 @@ import logging
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
def init():
|
class TestApi:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
@staticmethod
|
||||||
with open("testApi.config.yaml", "r") as f:
|
def __parse_arguments():
|
||||||
from yaml import safe_load
|
import argparse
|
||||||
config = safe_load(f)
|
|
||||||
|
|
||||||
return config
|
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:
|
class apiCall:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -37,20 +83,19 @@ class apiCall:
|
|||||||
except:
|
except:
|
||||||
logging.error(f"failed to parse json: {r.text}")
|
logging.error(f"failed to parse json: {r.text}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
config = init()
|
|
||||||
|
|
||||||
response = apiCall.call(
|
if __name__ == "__main__":
|
||||||
host=config["host"],
|
testApi = TestApi()
|
||||||
url="participo/api/users",
|
|
||||||
# url = "participo/api",
|
response = testApi.apiCall()
|
||||||
headers={
|
|
||||||
"Authorization": f"Basic {config['apiKey']}"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logging.info(json.dumps(response,indent=2))
|
print(
|
||||||
|
json.dumps(
|
||||||
|
response,
|
||||||
|
indent=2
|
||||||
|
)
|
||||||
|
)
|
||||||
except:
|
except:
|
||||||
logging.error(f"failed to parse to json:")
|
logging.error("failed to parse to json:")
|
||||||
logging.error(response)
|
logging.error(response)
|
||||||
|
|||||||
Reference in New Issue
Block a user