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",
"ignore": [
".vscode",
"*.py"
"*.py",
"*.config.yaml"
],
"uploadOnSave": true
}

View File

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

View File

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

View File

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