WIP: posting shiai to the wkKalendar via api

This commit is contained in:
marko
2024-01-07 18:36:43 +01:00
parent 4bf364c83b
commit 14108660f9
6 changed files with 150 additions and 24 deletions

20
homepage/participo/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "post",
"type": "python",
"request": "launch",
"program": "testApi.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"POST",
"--input", "testShiai.json"
]
}
]
}

View File

@@ -3,14 +3,52 @@ require_once("inc/bootstrap.php");
require_once("participoLib/shiai.php"); require_once("participoLib/shiai.php");
$method = $_SERVER['REQUEST_METHOD'];
// Sending Response // Sending Response
// - setting header // - 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 // - check if an valid api key was send
echo( authorize();
json_encode( // - depending on the method we perform different actions
Shiai::dbSelect() switch($method){
) // Create
); case 'POST':
$postData = json_decode(file_get_contents('php://input'), true);
if(!$postData){
die(json_encode([
'error'=>$postData . " not valid json data!"
]));
}
die(json_encode(
Shiai::fromArray($postData)->asArray()
));
break;
// Read
case 'GET':
echo(json_encode(
Shiai::dbSelect()
));
break;
// Update
case 'PUT':
die(json_encode([
'success'=>false,
'reason'=>$method.".not supported yet."
]));
break;
// Delete
case 'DELETE':
die(json_encode([
'success'=>false,
'reason'=>$method.".not supported yet."
]));
break;
// all other methods not supported
default:
die(json_encode([
'success'=>false,
'reason'=>$method.".not supported."
]));
}
?> ?>

View File

@@ -55,7 +55,7 @@ class participo
/** lazy loading of the session user */ /** lazy loading of the session user */
public static function sessionUser(bool $forceLoading = true) public static function sessionUser(bool $forceLoading = true)
{ {
if (is_null($sessionUser) || $forceLoading) { if (is_null(self::$sessionUser) || $forceLoading) {
self::$sessionUser = User::loadFromDb(self::getSessionUserId()); self::$sessionUser = User::loadFromDb(self::getSessionUserId());
} }
return self::$sessionUser; return self::$sessionUser;

View File

@@ -21,10 +21,10 @@ class Shiai
*/ */
private static $tableName = "wettkampfkalender"; private static $tableName = "wettkampfkalender";
public function __construct($id, $date, $name, $ageclasses, $place, $announcementUrl, $routeUrl, $galleryUrl, $promoImgUrl) public function __construct($id, $date, $name, $ageclasses, $place, $announcementUrl, $routeUrl, $galleryUrl=null, $promoImgUrl=null)
{ {
//! @todo input validation and sanitation //! @todo input validation and sanitation
$this->id = (int) $id; $this->id = filterId($id);
$this->date = DateTime::createFromFormat('Y-m-d', $date); $this->date = DateTime::createFromFormat('Y-m-d', $date);
$this->name = $name; $this->name = $name;
$this->ageclasses = $ageclasses ? self::akListString2jgArray($ageclasses) : null; $this->ageclasses = $ageclasses ? self::akListString2jgArray($ageclasses) : null;
@@ -35,6 +35,32 @@ class Shiai
$this->promoImgUrl = $promoImgUrl; $this->promoImgUrl = $promoImgUrl;
} }
public static function fromArray(array $shiai){
$id = $shiai['id'] ?? null;
$date = $shiai['date'] ?? null;
$name = $shiai['name'] ?? null;
$ageclasses = $shiai['ageclasses'] ?? null;
$place = $shiai['place'] ?? null;
$announcementUrl = $shiai['announcementUrl'] ?? null;
$routeUrl = $shiai['routeUrl'] ?? null;
// gallery stuff removed for now
return new Shiai($id, $date, $name, $ageclasses, $place, $announcementUrl, $routeUrl);
}
public function asArray(){
return [
'id'=>$this->id,
'date'=>$this->date->format('Y-m-d'),
'name'=>$this->name,
// @todo at least in theory this should again hold age categories
'ageclasses'=>implode(" ", $this->ageclasses),
'place'=>$this->place,
'announcementUrl'=>$this->announcementUrl,
'routeUrl'=>$this->announcementUrl
];
}
public function getId() public function getId()
{ {
return $this->id; return $this->id;

View File

@@ -75,6 +75,26 @@ class User
return dbConnector::getLastInsertId(); return dbConnector::getLastInsertId();
} }
public static function dbSelectWithAttribute(int $attributeId)
{
$query =
"SELECT DISTINCT" .
" `wkParticipo_Users`.* " .
" FROM `wkParticipo_Users`" .
" JOIN `wkParticipo_user<=>userAttributes`" .
" ON `wkParticipo_user<=>userAttributes`.`userId` = `wkParticipo_Users`.`id`" .
" WHERE `wkParticipo_user<=>userAttributes`.`attributeId` = :attributeId".
" ORDER BY `wkParticipo_Users`.`id` ASC;";
$params = [
':attributeId' => ['value' => $attributeId, 'data_type' => PDO::PARAM_INT]
];
$response = dbConnector::query($query, $params);
return $response;
}
/** Name of the table with all the Users /** Name of the table with all the Users
* *
* @var string * @var string

View File

@@ -12,7 +12,10 @@ class TestApi:
argParser = argparse.ArgumentParser( argParser = argparse.ArgumentParser(
"testApi" "testApi"
) )
argParser.add_argument(
"method",
choices=['GET', 'POST']
)
argParser.add_argument( argParser.add_argument(
"--endpoint", "--endpoint",
default="shiai" default="shiai"
@@ -40,6 +43,11 @@ class TestApi:
choices=_LOG_LEVEL, choices=_LOG_LEVEL,
default="WARNING" default="WARNING"
) )
argParser.add_argument(
"--input", "-i",
type=argparse.FileType("r"),
default="-"
)
return argParser.parse_args() return argParser.parse_args()
@@ -59,33 +67,47 @@ class TestApi:
def apiCall(self): def apiCall(self):
return apiCall.call( return apiCall.call(
method=self.config.method,
host=self.config.host, host=self.config.host,
url="/".join([self.config.path, self.config.endpoint]), url="/".join([self.config.path, self.config.endpoint]),
headers={ headers={
"Authorization": f"Basic {self.config.key}" "Authorization": f"Basic {self.config.key}"
} },
input=load_json(self.config.input)
) )
def load_json(jsonFile):
import json
with jsonFile as f:
return json.load(f)
class apiCall: class apiCall:
@staticmethod @staticmethod
def call( def call(
method: str,
host: str, host: str,
url: str, url: str,
headers: dict = None headers: dict = None,
input: dict = None,
) -> dict: ) -> dict:
import requests import requests
r = requests.get( if(method=="GET"):
url=f"http://{host}/{url}", r = requests.get(
# @todo The client always awaits this timeout. Even when the url=f"http://{host}/{url}",
# meaningful body is already received.params= timeout=10,
# - I don't see any of the examples out there do it different from headers=headers
# me. )
# - The browser doesn't seem to have this problem. elif(method=="POST"):
timeout=10, r = requests.post(
headers=headers json=input,
) url=f"http://{host}/{url}",
timeout=10,
headers=headers
)
else:
logging.error("This line should never been reached!")
try: try:
return r.json() return r.json()