wip: rest api user endpoint

This commit is contained in:
marko
2023-12-24 19:46:49 +01:00
parent 9ab62485a6
commit 38cc02120c
4 changed files with 60 additions and 28 deletions

View File

@@ -7,7 +7,8 @@
"remotePath": "/www/participo", "remotePath": "/www/participo",
"ignore": [ "ignore": [
".vscode", ".vscode",
"*.py" "*.py",
"*.config.yaml"
], ],
"uploadOnSave": true "uploadOnSave": true
} }

View File

@@ -39,15 +39,23 @@ $wkSqlQuery = "SELECT DISTINCT"
$wkSqlResponse = dbConnector::query($wkSqlQuery); $wkSqlResponse = dbConnector::query($wkSqlQuery);
// Postprocessing
// - convert the comma separated list into an array
foreach( $wkSqlResponse as &$user){ foreach( $wkSqlResponse as &$user){
$user['eMail'] = explode(",", $user['eMail']); $user['eMail'] = explode(",", $user['eMail']);
foreach( $user['eMail'] as &$email){
$email = trim($email);
}
} }
header('Access-Control-Allow-Headers: *'); // Sending Response
header('Access-Control-Allow-Origin: *'); // - setting header
header('Content-Type: application/json');
// - sending body payload
echo( echo(
json_encode($wkSqlResponse) 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);
?>

View File

@@ -3,32 +3,54 @@
from http import client from http import client
import logging import logging
import json import json
import yaml
host="cwsvjudo.bplaced.net"
with open("testApi.config.yaml", "r") as f: def init():
config = yaml.load(f) 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) class apiCall:
connection.request( @staticmethod
method="GET", def call(
url="/participo/api/users", host: str,
headers={ url: str,
"Authorization": f"Basic {config['apiKey']}" headers: dict = None
} ) -> dict:
) import requests
response = connection.getresponse()
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:
try: return r.json()
print(json.dumps(json.loads(body),indent=2)) except:
except: logging.error(f"failed to parse json: {r.text}")
print(f"failed to parse to json")
print(body)
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)

View File

@@ -21,3 +21,4 @@ header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
echo(json_encode($wkSqlResponse)); echo(json_encode($wkSqlResponse));
?>