Try to unify API responses

This commit is contained in:
2024-07-21 16:10:02 +02:00
parent a01515aa9c
commit b76c434216
5 changed files with 59 additions and 38 deletions

View File

@ -72,10 +72,12 @@ if ($path == '') {
header('Allow: GET');
if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8');
echo "405 Method Not Allowed :(\n";
header('Content-Length: 23');
echo "405 Method Not Allowed\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Method not allowed\"}]}\n";
header('Content-Length: 48');
echo "{\"errors\": [{\"message\": \"Method not allowed\"}]}\n";
} else {
header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0');
@ -89,7 +91,7 @@ if ($path == '') {
echo "$c\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [";
echo "{\"data\": [";
$first = true;
foreach ($clients as $c) {
if (!$first) echo ",";
@ -140,18 +142,20 @@ foreach ($clients as $c) {
authenticate_client($c);
if ($path === "/$c") {
header("Location: $c/");
header('Status: 303');
exit("303 See Other :)\n");
header("Location: $c/");
header('Content-Length: 14');
exit("303 See Other\n");
} elseif ($path === "/$c/") {
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
header("Status: 405");
header("Allow: GET");
exit("405 Method Not Allowed :(\n");
header('Content-Length: 23');
exit("405 Method Not Allowed\n");
}
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"success\", \"data\": [\n";
echo "{\"data\": [\n";
$first = true;
foreach (scandir(".data/clients/$c/") as $file) {
if (str_starts_with($file, ".") || str_ends_with($file, ".php")) continue;
@ -183,13 +187,15 @@ foreach ($clients as $c) {
$file = substr($path, strlen("/$c/"));
$path = ".data/clients/$c/$file";
if (str_contains($file, '/')) {
header("Status: 400");
exit("400 Bad Request :(\n");
header('Status: 400');
header('Content-Length: 16');
exit("400 Bad Request\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
$size = filesize($path);
if ($size === false) {
header("Status: 404");
exit("404 Not Found :(\n");
header('Status: 404');
header('Content-Length: 14');
exit("404 Not Found\n");
}
$type = mime_content_type($path);
header("Content-Type: $type");
@ -201,24 +207,29 @@ foreach ($clients as $c) {
$fp = fopen($path, 'wb');
if ($fp === false) {
header("Status: 500");
exit("500 Internal Server Error :(\n");
header("Content-Length: 26");
exit("500 Internal Server Error\n");
}
while ($data = fread($putdata, 4096))
fwrite($fp, $data);
fclose($fp);
fclose($putdata);
header("Status: 201");
exit("201 Created :)\n");
header('Content-Length: 12');
exit("201 Created\n");
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
if (unlink($path) === false) {
header("Status: 500");
exit("500 Internal Server Error :(\n");
header("Content-Length: 26");
exit("500 Internal Server Error\n");
}
exit("200 OK :)\n");
header('Content-Length: 6');
exit("200 OK\n");
} else {
header("Status: 405");
header("Allow: GET, PUT, DELETE");
exit("405 Method Not Allowed :(\n");
header("Content-Length: 23");
exit("405 Method Not Allowed\n");
}
exit();
@ -227,10 +238,12 @@ foreach ($clients as $c) {
header("Status: 404");
if ($format === 'text') {
header('Content-Type: text/plain; charset=UTF-8');
echo "404 Not Found :(\n";
header('Content-Length: 14');
echo "404 Not Found\n";
} else if ($format === 'json') {
header('Content-Type: application/json; charset=UTF-8');
echo "{\"status\": \"error\", \"errors\": [{\"message\": \"Not found\"}]}\n";
header('Content-Length: 39');
echo "{\"errors\": [{\"message\": \"Not found\"}]}\n";
} else {
header('Content-Type: text/html; charset=UTF-8');
header('Content-Length: 0');