Improved endpoint execution

This commit is contained in:
Filip Znachor 2024-01-27 10:37:56 +01:00
parent 1c844f4f78
commit 8b75dbf929

View file

@ -12,6 +12,9 @@ class API {
}
public function convertResponse(mixed $res): array {
if ($res === null) {
$res = ["error" => "invalid_endpoint"];
}
if (is_bool($res)) {
$res = ["success" => $res];
}
@ -33,7 +36,7 @@ class API {
function getClass(array $path): string | null {
$root = &$this->map;
foreach ($path as $part) {
foreach ($path as &$part) {
if (!array_key_exists($part, $root)) return null;
$root = &$root[$part];
}
@ -55,9 +58,9 @@ class API {
return explode("/", $action);
}
function execute(array $path, array $data) {
function execute(array $path, array $data): mixed {
$class = $this->getClass($path);
if ($class == null) return ["error" => "invalid_endpoint"];
if ($class == null) return null;
$endpoint = $this->getEndpoint($class);
return $endpoint->get($data);
}