diff --git a/homepage/participo/.vscode/sftp.json b/homepage/participo/.vscode/sftp.json index 447647d..39cd462 100644 --- a/homepage/participo/.vscode/sftp.json +++ b/homepage/participo/.vscode/sftp.json @@ -7,7 +7,8 @@ "remotePath": "/www/participo", "ignore": [ ".vscode", - "*.py" + "*.py", + "*.config.yaml" ], "uploadOnSave": true } \ No newline at end of file diff --git a/homepage/participo/api/users.php b/homepage/participo/api/users.php index ffeb110..c30f7af 100644 --- a/homepage/participo/api/users.php +++ b/homepage/participo/api/users.php @@ -39,15 +39,23 @@ $wkSqlQuery = "SELECT DISTINCT" $wkSqlResponse = dbConnector::query($wkSqlQuery); +// Postprocessing +// - convert the comma separated list into an array foreach( $wkSqlResponse as &$user){ $user['eMail'] = explode(",", $user['eMail']); + foreach( $user['eMail'] as &$email){ + $email = trim($email); + } } -header('Access-Control-Allow-Headers: *'); -header('Access-Control-Allow-Origin: *'); - +// Sending Response +// - setting header +header('Content-Type: application/json'); +// - sending body payload echo( json_encode($wkSqlResponse) - // json_encode(getallheaders()) - // json_encode($_SERVER) ); + +// @todo Should not be necessary. But until the problem with the timeout time of the requesting client is solved, this explicit exit() stands! +exit(0); +?> \ No newline at end of file diff --git a/homepage/participo/testApi.py b/homepage/participo/testApi.py index 5ef39d2..b84fa23 100755 --- a/homepage/participo/testApi.py +++ b/homepage/participo/testApi.py @@ -3,32 +3,54 @@ from http import client import logging import json -import yaml -host="cwsvjudo.bplaced.net" -with open("testApi.config.yaml", "r") as f: - config = yaml.load(f) +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 -logging.basicConfig(level=logging.DEBUG) -connection = client.HTTPConnection(host=host, port=80, timeout=1) -connection.request( - method="GET", - url="/participo/api/users", - headers={ - "Authorization": f"Basic {config['apiKey']}" - } -) -response = connection.getresponse() +class apiCall: + @staticmethod + def call( + host: str, + url: str, + headers: dict = None + ) -> dict: + import requests -print("Status: {} and reason: {}".format(response.status, response.reason)) + r = requests.get( + url=f"http://{host}/{url}", + # @todo The client always awaits this timeout. Even when the meaningful body is already received.params= + # - I don't see any of the examples out there do it different from me. + # - The browser doesn't seem to have this problem. + timeout=10, + headers=headers + ) -body = response.read() -try: - print(json.dumps(json.loads(body),indent=2)) -except: - print(f"failed to parse to json") - print(body) + try: + return r.json() + except: + logging.error(f"failed to parse json: {r.text}") -connection.close() +if __name__ == "__main__": + config = init() + + response = apiCall.call( + host=config["host"], + url="participo/api/users", + # url = "participo/api", + headers={ + "Authorization": f"Basic {config['apiKey']}" + } + ) + + try: + logging.info(json.dumps(response,indent=2)) + except: + logging.error(f"failed to parse to json:") + logging.error(response) diff --git a/homepage/participo/wk.api.php b/homepage/participo/wk.api.php index 3afe702..bee2c7b 100644 --- a/homepage/participo/wk.api.php +++ b/homepage/participo/wk.api.php @@ -21,3 +21,4 @@ header('Access-Control-Allow-Headers: *'); header('Access-Control-Allow-Origin: *'); echo(json_encode($wkSqlResponse)); +?> \ No newline at end of file