1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-13 00:24:15 +02:00

feat(rest-api): Moving from CDA and CMA to more universal format of REST API'S #435

BREAKING CHANGES

From now we don't have CDA and CMA groups.

New api endpoints:

Config
GET /api/config
POST /api/config
PATCH /api/config
DELETE /api/config

Entries
GET /api/entries
POST /api/entries
PATCH /api/entries
PUT /api/entries
PUT /api/entries/copy
DELETE /api/entries

Files
GET /api/files

Images
GET /api/images

Registry
GET /api/registry
This commit is contained in:
Awilum
2020-06-27 20:53:14 +03:00
parent cd89c9e1a4
commit 2ed1eace57
12 changed files with 177 additions and 496 deletions

View File

@@ -128,15 +128,12 @@ include_once 'dependencies.php';
/**
* Include API ENDPOINTS
*/
include_once 'endpoints/access/access.php';
include_once 'endpoints/delivery/entries.php';
include_once 'endpoints/delivery/registry.php';
include_once 'endpoints/delivery/config.php';
include_once 'endpoints/delivery/media/files.php';
include_once 'endpoints/management/entries.php';
include_once 'endpoints/management/config.php';
include_once 'endpoints/management/media/files.php';
include_once 'endpoints/images/images.php';
include_once 'endpoints/access.php';
include_once 'endpoints/entries.php';
include_once 'endpoints/registry.php';
include_once 'endpoints/config.php';
include_once 'endpoints/files.php';
include_once 'endpoints/images.php';
/**
* Set internal encoding

View File

@@ -12,6 +12,13 @@ namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
/**
* API sys messages
*/
$api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' => 'AccessTokenInvalid'], 'message' => 'The access token you sent could not be found or is invalid.'];
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate access token
*/

View File

@@ -16,33 +16,27 @@ use function array_replace_recursive;
use function count;
/**
* API sys messages
* Validate config token
*/
$api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' => 'AccessTokenInvalid'], 'message' => 'The access token you sent could not be found or is invalid.'];
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate management config token
*/
function validate_management_config_token($token) : bool
function validate_config_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/management/config/' . $token . '/token.yaml');
return Filesystem::has(PATH['project'] . '/tokens/config/' . $token . '/token.yaml');
}
/**
* Fetch item in the config
* Fetch config item
*
* endpoint: GET /api/delivery/config
* endpoint: GET /api/config
*
* Query:
* key - [REQUIRED] - Unique identifier of the config item.
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Content Delivery API token for Config.
* key - [REQUIRED] - Unique identifier of the config item.
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Config API token.
*
* Returns:
* An array of config item objects.
*/
$app->get('/api/management/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->get('/api/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
@@ -52,11 +46,11 @@ $app->get('/api/management/config', function (Request $request, Response $respon
$config = $query['config'];
$token = $query['token'];
if ($flextype['registry']->get('flextype.settings.api.management.config.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.config.enabled')) {
// Validate delivery token
if (validate_delivery_config_token($token)) {
$delivery_config_token_file_path = PATH['project'] . '/tokens/management/config/' . $token . '/token.yaml';
// Validate config token
if (validate_config_token($token)) {
$delivery_config_token_file_path = PATH['project'] . '/tokens/config/' . $token . '/token.yaml';
// Set delivery token file
if ($delivery_config_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_config_token_file_path), 'yaml')) {
@@ -105,21 +99,22 @@ $app->get('/api/management/config', function (Request $request, Response $respon
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});
/**
* Create new item in the config
*
* endpoint: POST /api/management/config
* endpoint: POST /api/config
*
* Body:
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Content Management API token for Config.
* token - [REQUIRED] - Valid Config token.
* access_token - [REQUIRED] - Valid Access token.
* data - [REQUIRED] - Data to store for the config.
*
* Returns:
* Returns the config item object for the config item that was just created.
*/
$app->post('/api/management/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->post('/api/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -130,19 +125,19 @@ $app->post('/api/management/config', function (Request $request, Response $respo
$config = $post_data['config'];
$data = $post_data['data'];
if ($flextype['registry']->get('flextype.settings.api.management.config.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.config.enabled')) {
// Validate management and access token
if (validate_management_config_token($token) && validate_access_token($access_token)) {
$management_config_token_file_path = PATH['project'] . '/tokens/management/config/' . $token . '/token.yaml';
// Validate config and access token
if (validate_config_token($token) && validate_access_token($access_token)) {
$config_token_file_path = PATH['project'] . '/tokens/config/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_config_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_config_token_file_path), 'yaml')) &&
// Set config and access token file
if (($config_token_file_data = $flextype['serializer']->decode(Filesystem::read($config_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_config_token_file_data['state'] === 'disabled' ||
($management_config_token_file_data['limit_calls'] !== 0 && $management_config_token_file_data['calls'] >= $management_config_token_file_data['limit_calls'])) {
if ($config_token_file_data['state'] === 'disabled' ||
($config_token_file_data['limit_calls'] !== 0 && $config_token_file_data['calls'] >= $config_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -169,7 +164,7 @@ $app->post('/api/management/config', function (Request $request, Response $respo
$response_code = ($create_config) ? 200 : 404;
// Update calls counter
Filesystem::write($management_config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_config_token_file_data, ['calls' => $management_config_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($config_token_file_data, ['calls' => $config_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -198,18 +193,18 @@ $app->post('/api/management/config', function (Request $request, Response $respo
/**
* Update config item
*
* endpoint: POST /api/management/config
* endpoint: PATCH /api/config
*
* Body:
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Content Management API token for Config.
* token - [REQUIRED] - Valid Config token.
* access_token - [REQUIRED] - Valid Access token.
* data - [REQUIRED] - Data to store for the config.
*
* Returns:
* Returns the config item object for the config item that was just created.
*/
$app->patch('/api/management/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->patch('/api/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -220,20 +215,20 @@ $app->patch('/api/management/config', function (Request $request, Response $resp
$data = $post_data['data'];
$config = $post_data['config'];
if ($flextype['registry']->get('flextype.settings.api.management.config.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.config.enabled')) {
// Validate management and access token
if (validate_management_config_token($token) && validate_access_token($access_token)) {
// Validate config and access token
if (validate_config_token($token) && validate_access_token($access_token)) {
$management_config_token_file_path = PATH['project'] . '/tokens/management/config/' . $token . '/token.yaml';
$config_token_file_path = PATH['project'] . '/tokens/config/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_config_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_config_token_file_path), 'yaml')) &&
// Set config and access token file
if (($config_token_file_data = $flextype['serializer']->decode(Filesystem::read($config_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_config_token_file_data['state'] === 'disabled' ||
($management_config_token_file_data['limit_calls'] !== 0 && $management_config_token_file_data['calls'] >= $management_config_token_file_data['limit_calls'])) {
if ($config_token_file_data['state'] === 'disabled' ||
($config_token_file_data['limit_calls'] !== 0 && $config_token_file_data['calls'] >= $config_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -260,7 +255,7 @@ $app->patch('/api/management/config', function (Request $request, Response $resp
$response_code = ($update_config) ? 200 : 404;
// Update calls counter
Filesystem::write($management_config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_config_token_file_data, ['calls' => $management_config_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($config_token_file_data, ['calls' => $config_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -287,22 +282,21 @@ $app->patch('/api/management/config', function (Request $request, Response $resp
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});
/**
* Delete config item
*
* endpoint: DELETE /api/management/config
* endpoint: DELETE /api/config
*
* Body:
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Content Management API token for Config.
* token - [REQUIRED] - Valid Config token.
* access_token - [REQUIRED] - Valid Access token.
* data - [REQUIRED] - Data to store for the config.
*
* Returns:
* Returns an empty body with HTTP status 204
*/
$app->delete('/api/management/config', function (Request $request, Response $response) use ($flextype) {
$app->delete('/api/config', function (Request $request, Response $response) use ($flextype) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -313,19 +307,19 @@ $app->delete('/api/management/config', function (Request $request, Response $res
$data = $post_data['data'];
$config = $post_data['config'];
if ($flextype['registry']->get('flextype.settings.api.management.config.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.config.enabled')) {
// Validate management and access token
if (validate_management_config_token($token) && validate_access_token($access_token)) {
$management_config_token_file_path = PATH['project'] . '/tokens/management/config/' . $token . '/token.yaml';
// Validate config and access token
if (validate_config_token($token) && validate_access_token($access_token)) {
$config_token_file_path = PATH['project'] . '/tokens/config/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_config_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_config_token_file_path), 'yaml')) &&
// Set config and access token file
if (($config_token_file_data = $flextype['serializer']->decode(Filesystem::read($config_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_config_token_file_data['state'] === 'disabled' ||
($management_config_token_file_data['limit_calls'] !== 0 && $management_config_token_file_data['calls'] >= $management_config_token_file_data['limit_calls'])) {
if ($config_token_file_data['state'] === 'disabled' ||
($config_token_file_data['limit_calls'] !== 0 && $config_token_file_data['calls'] >= $config_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -341,7 +335,7 @@ $app->delete('/api/management/config', function (Request $request, Response $res
$response_code = ($delete_config) ? 204 : 404;
// Update calls counter
Filesystem::write($management_config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_config_token_file_data, ['calls' => $management_config_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($config_token_file_data, ['calls' => $config_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {

View File

@@ -1,105 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use function array_replace_recursive;
/**
* API sys messages
*/
$api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' => 'AccessTokenInvalid'], 'message' => 'The access token you sent could not be found or is invalid.'];
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate delivery config token
*/
function validate_delivery_config_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/delivery/config/' . $token . '/token.yaml');
}
/**
* Fetch item in the config
*
* endpoint: GET /api/delivery/config
*
* Query:
* key - [REQUIRED] - Unique identifier of the config item.
* config - [REQUIRED] - Unique identifier of the config namespace.
* token - [REQUIRED] - Valid Content Delivery API token for Config.
*
* Returns:
* An array of config item objects.
*/
$app->get('/api/delivery/config', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
// Set variables
$key = $query['key'];
$config = $query['config'];
$token = $query['token'];
if ($flextype['registry']->get('flextype.settings.api.delivery.config.enabled')) {
// Validate delivery token
if (validate_delivery_config_token($token)) {
$delivery_config_token_file_path = PATH['project'] . '/tokens/delivery/config/' . $token . '/token.yaml';
// Set delivery token file
if ($delivery_config_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_config_token_file_path), 'yaml')) {
if ($delivery_config_token_file_data['state'] === 'disabled' ||
($delivery_config_token_file_data['limit_calls'] !== 0 && $delivery_config_token_file_data['calls'] >= $delivery_config_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
// Fetch config
if ($flextype['config']->has($config, $key)) {
$response_data['data']['key'] = $key;
$response_data['data']['value'] = $flextype['config']->get($config, $key);
// Set response code
$response_code = 200;
} else {
$response_data = [];
$response_code = 404;
}
// Update calls counter
Filesystem::write($delivery_config_token_file_path, $flextype['serializer']->encode(array_replace_recursive($delivery_config_token_file_data, ['calls' => $delivery_config_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
// Return response
return $response
->withJson($api_sys_messages['NotFound'], $response_code);
}
// Return response
return $response
->withJson($response_data, $response_code);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});

View File

@@ -1,99 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use function array_replace_recursive;
use function count;
/**
* API sys messages
*/
$api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' => 'AccessTokenInvalid'], 'message' => 'The access token you sent could not be found or is invalid.'];
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate delivery entries token
*/
function validate_delivery_entries_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/delivery/entries/' . $token . '/token.yaml');
}
/**
* Fetch entry(entries)
*
* endpoint: GET /api/delivery/entries
*
* Query:
* id - [REQUIRED] - Unique identifier of the entry(entries).
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
* filter - [OPTIONAL] - Select items in collection by given conditions.
*
* Returns:
* An array of entry item objects.
*/
$app->get('/api/delivery/entries', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
// Set variables
$id = $query['id'];
$token = $query['token'];
$filter = $query['filter'] ?? null;
if ($flextype['registry']->get('flextype.settings.api.delivery.entries.enabled')) {
// Validate delivery token
if (validate_delivery_entries_token($token)) {
$delivery_entries_token_file_path = PATH['project'] . '/tokens/delivery/entries/' . $token. '/token.yaml';
// Set delivery token file
if ($delivery_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) {
if ($delivery_entries_token_file_data['state'] === 'disabled' ||
($delivery_entries_token_file_data['limit_calls'] !== 0 && $delivery_entries_token_file_data['calls'] >= $delivery_entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
// Fetch entry
$response_data['data'] = $flextype['entries']->fetch($id, $filter);
// Set response code
$response_code = count($response_data['data']) > 0 ? 200 : 404;
// Update calls counter
Filesystem::write($delivery_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
// Return response
return $response
->withJson($api_sys_messages['NotFound'], $response_code);
}
// Return response
return $response
->withJson($response_data, $response_code);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});

View File

@@ -1,106 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use function array_replace_recursive;
/**
* API sys messages
*/
$api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' => 'AccessTokenInvalid'], 'message' => 'The access token you sent could not be found or is invalid.'];
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate delivery media files token
*/
function validate_delivery_media_files_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/delivery/media/files/' . $token . '/token.yaml');
}
/**
* Fetch media file(s)
*
* endpoint: GET /api/delivery/media/files
*
* Query:
* path - [REQUIRED] - Unique identifier of the file path.
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
*
* Returns:
* An array of entry item objects.
*/
$app->get('/api/delivery/media/files', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
// Set variables
$path = $query['path'];
$token = $query['token'];
if ($flextype['registry']->get('flextype.settings.api.delivery.media.files.enabled')) {
// Validate delivery token
if (validate_delivery_media_files_token($token)) {
$delivery_files_token_file_path = PATH['project'] . '/tokens/delivery/media/files/' . $token. '/token.yaml';
// Set delivery token file
if ($delivery_files_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_files_token_file_path), 'yaml')) {
if ($delivery_files_token_file_data['state'] === 'disabled' ||
($delivery_files_token_file_data['limit_calls'] !== 0 && $delivery_files_token_file_data['calls'] >= $delivery_files_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
// Create files array
$files = [];
// Get list if file or files for specific folder
if (is_dir($path)) {
$files = $flextype['media_files']->fetchCollection($path);
} else {
$files = $flextype['media_files']->fetchSingle($path);
}
// Write response data
$response_data['data'] = $files;
// Set response code
$response_code = count($response_data['data']) > 0 ? 200 : 404;
// Update calls counter
Filesystem::write($delivery_files_token_file_path, $flextype['serializer']->encode(array_replace_recursive($delivery_files_token_file_data, ['calls' => $delivery_files_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
// Return response
return $response
->withJson($api_sys_messages['NotFound'], $response_code);
}
// Return response
return $response
->withJson($response_data, $response_code);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
return $response
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});

View File

@@ -22,27 +22,28 @@ $api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' =>
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate management entries token
* Validate entries entries token
*/
function validate_management_entries_token($token) : bool
function validate_entries_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml');
return Filesystem::has(PATH['project'] . '/tokens/entries/' . $token . '/token.yaml');
}
/**
* Fetch entry(entries)
*
* endpoint: GET /api/management/entries
* endpoint: GET /api/entries
*
* Query:
* id - [REQUIRED] - Unique identifier of the entry(entries).
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* filter - [OPTIONAL] - Select items in collection by given conditions.
*
* Returns:
* An array of entry item objects.
*/
$app->get('/api/management/entries', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->get('/api/entries', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
@@ -51,15 +52,16 @@ $app->get('/api/management/entries', function (Request $request, Response $respo
$token = $query['token'];
$filter = $query['filter'] ?? null;
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
// Validate management token
if (validate_management_entries_token($token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token. '/token.yaml';
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Set management token file
if ($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
// Validate entries token
if (validate_entries_token($token)) {
$entries_token_file_path = PATH['project'] . '/tokens/' . $token. '/token.yaml';
// Set entries token file
if ($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -70,7 +72,7 @@ $app->get('/api/management/entries', function (Request $request, Response $respo
$response_code = count($response_data['data']) > 0 ? 200 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -96,22 +98,21 @@ $app->get('/api/management/entries', function (Request $request, Response $respo
->withJson($api_sys_messages['AccessTokenInvalid'], 401);
});
/**
* Create entry
*
* endpoint: POST /api/management/entries
* endpoint: POST /api/entries
*
* Body:
* id - [REQUIRED] - Unique identifier of the entry.
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* access_token - [REQUIRED] - Valid Access token.
* data - [REQUIRED] - Data to store for the entry.
*
* Returns:
* Returns the entry item object for the entry item that was just created.
*/
$app->post('/api/management/entries', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->post('/api/entries', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -122,19 +123,19 @@ $app->post('/api/management/entries', function (Request $request, Response $resp
$id = $post_data['id'];
$data = $post_data['data'];
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Validate management and access token
if (validate_management_entries_token($token) && validate_access_token($access_token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml';
// Validate entries and access token
if (validate_entries_token($token) && validate_access_token($access_token)) {
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) &&
// Set entries and access token file
if (($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -156,7 +157,7 @@ $app->post('/api/management/entries', function (Request $request, Response $resp
$response_code = ($create_entry) ? 200 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -185,18 +186,18 @@ $app->post('/api/management/entries', function (Request $request, Response $resp
/**
* Update entry
*
* endpoint: PATCH /api/management/entries
* endpoint: PATCH /api/entries
*
* Body:
* id - [REQUIRED] - Unique identifier of the entry.
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* access_token - [REQUIRED] - Valid Authentication token.
* data - [REQUIRED] - Data to update for the entry.
*
* Returns:
* Returns the entry item object for the entry item that was just updated.
*/
$app->patch('/api/management/entries', function (Request $request, Response $response) use ($flextype) {
$app->patch('/api/entries', function (Request $request, Response $response) use ($flextype) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -207,19 +208,19 @@ $app->patch('/api/management/entries', function (Request $request, Response $res
$id = $post_data['id'];
$data = $post_data['data'];
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Validate management and access token
if (validate_management_entries_token($token) && validate_access_token($access_token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml';
// Validate entries and access token
if (validate_entries_token($token) && validate_access_token($access_token)) {
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) &&
// Set entries and access token file
if (($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -241,7 +242,7 @@ $app->patch('/api/management/entries', function (Request $request, Response $res
$response_code = ($update_entry) ? 200 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -270,18 +271,18 @@ $app->patch('/api/management/entries', function (Request $request, Response $res
/**
* Rename entry
*
* endpoint: PUT /api/management/entries
* endpoint: PUT /api/entries
*
* Body:
* id - [REQUIRED] - Unique identifier of the entry.
* new_id - [REQUIRED] - New Unique identifier of the entry.
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* access_token - [REQUIRED] - Valid Authentication token.
*
* Returns:
* Returns the entry item object for the entry item that was just renamed.
*/
$app->put('/api/management/entries', function (Request $request, Response $response) use ($flextype) {
$app->put('/api/entries', function (Request $request, Response $response) use ($flextype) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -292,19 +293,19 @@ $app->put('/api/management/entries', function (Request $request, Response $respo
$id = $post_data['id'];
$new_id = $post_data['new_id'];
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Validate management and access token
if (validate_management_entries_token($token) && validate_access_token($access_token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml';
// Validate entries and access token
if (validate_entries_token($token) && validate_access_token($access_token)) {
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) &&
// Set entries and access token file
if (($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -327,7 +328,7 @@ $app->put('/api/management/entries', function (Request $request, Response $respo
$response_code = ($rename_entry) ? 200 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -356,18 +357,18 @@ $app->put('/api/management/entries', function (Request $request, Response $respo
/**
* Copy entry(entries)
*
* endpoint: PUT /api/management/entries/copy
* endpoint: PUT /api/entries/copy
*
* Body:
* id - [REQUIRED] - Unique identifier of the entry.
* new_id - [REQUIRED] - New Unique identifier of the entry.
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* access_token - [REQUIRED] - Valid Authentication token.
*
* Returns:
* Returns the entry item object for the entry item that was just copied.
*/
$app->put('/api/management/entries/copy', function (Request $request, Response $response) use ($flextype) {
$app->put('/api/entries/copy', function (Request $request, Response $response) use ($flextype) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -378,19 +379,19 @@ $app->put('/api/management/entries/copy', function (Request $request, Response $
$id = $post_data['id'];
$new_id = $post_data['new_id'];
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Validate management and access token
if (validate_management_entries_token($token) && validate_access_token($access_token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml';
// Validate entries and access token
if (validate_entries_token($token) && validate_access_token($access_token)) {
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) &&
// Set entries and access token file
if (($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -413,7 +414,7 @@ $app->put('/api/management/entries/copy', function (Request $request, Response $
$response_code = ($copy_entry) ? 200 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {
@@ -442,17 +443,17 @@ $app->put('/api/management/entries/copy', function (Request $request, Response $
/**
* Delete entry
*
* endpoint: DELETE /api/management/entries
* endpoint: DELETE /api/entries
*
* Body:
* id - [REQUIRED] - Unique identifier of the entry.
* token - [REQUIRED] - Valid Content Management API token for Entries.
* token - [REQUIRED] - Valid Entries token.
* access_token - [REQUIRED] - Valid Authentication token.
*
* Returns:
* Returns an empty body with HTTP status 204
*/
$app->delete('/api/management/entries', function (Request $request, Response $response) use ($flextype) {
$app->delete('/api/entries', function (Request $request, Response $response) use ($flextype) {
// Get Post Data
$post_data = $request->getParsedBody();
@@ -462,19 +463,19 @@ $app->delete('/api/management/entries', function (Request $request, Response $re
$access_token = $post_data['access_token'];
$id = $post_data['id'];
if ($flextype['registry']->get('flextype.settings.api.management.entries.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
// Validate management and access token
if (validate_management_entries_token($token) && validate_access_token($access_token)) {
$management_entries_token_file_path = PATH['project'] . '/tokens/management/entries/' . $token . '/token.yaml';
// Validate entries and access token
if (validate_entries_token($token) && validate_access_token($access_token)) {
$entries_token_file_path = PATH['project'] . '/tokens/entries/' . $token . '/token.yaml';
$access_token_file_path = PATH['project'] . '/tokens/access/' . $access_token . '/token.yaml';
// Set management and access token file
if (($management_entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($management_entries_token_file_path), 'yaml')) &&
// Set entries and access token file
if (($entries_token_file_data = $flextype['serializer']->decode(Filesystem::read($entries_token_file_path), 'yaml')) &&
($access_token_file_data = $flextype['serializer']->decode(Filesystem::read($access_token_file_path), 'yaml'))) {
if ($management_entries_token_file_data['state'] === 'disabled' ||
($management_entries_token_file_data['limit_calls'] !== 0 && $management_entries_token_file_data['calls'] >= $management_entries_token_file_data['limit_calls'])) {
if ($entries_token_file_data['state'] === 'disabled' ||
($entries_token_file_data['limit_calls'] !== 0 && $entries_token_file_data['calls'] >= $entries_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -490,7 +491,7 @@ $app->delete('/api/management/entries', function (Request $request, Response $re
$response_code = ($delete_entry) ? 204 : 404;
// Update calls counter
Filesystem::write($management_entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($management_entries_token_file_data, ['calls' => $management_entries_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($entries_token_file_path, $flextype['serializer']->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {

View File

@@ -21,26 +21,26 @@ $api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' =>
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate delivery media files token
* Validate files token
*/
function validate_delivery_media_files_token($token) : bool
function validate_files_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/delivery/media/files/' . $token . '/token.yaml');
return Filesystem::has(PATH['project'] . '/tokens/files/' . $token . '/token.yaml');
}
/**
* Fetch media file(s)
* Fetch file(s)
*
* endpoint: GET /api/delivery/media/files
* endpoint: GET /api/files
*
* Query:
* path - [REQUIRED] - Unique identifier of the file path.
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
* token - [REQUIRED] - Valid Files token.
*
* Returns:
* An array of entry item objects.
*/
$app->get('/api/delivery/media/files', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->get('/api/files', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
@@ -49,11 +49,11 @@ $app->get('/api/delivery/media/files', function (Request $request, Response $res
$path = $query['path'];
$token = $query['token'];
if ($flextype['registry']->get('flextype.settings.api.delivery.media.files.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.files.enabled')) {
// Validate delivery token
if (validate_delivery_media_files_token($token)) {
$delivery_files_token_file_path = PATH['project'] . '/tokens/delivery/media/files/' . $token. '/token.yaml';
if (validate_files_token($token)) {
$delivery_files_token_file_path = PATH['project'] . '/tokens/files/' . $token. '/token.yaml';
// Set delivery token file
if ($delivery_files_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_files_token_file_path), 'yaml')) {

View File

@@ -21,26 +21,26 @@ $api_sys_messages['AccessTokenInvalid'] = ['sys' => ['type' => 'Error', 'id' =>
$api_sys_messages['NotFound'] = ['sys' => ['type' => 'Error', 'id' => 'NotFound'], 'message' => 'The resource could not be found.'];
/**
* Validate delivery registry token
* Validate registry token
*/
function validate_delivery_registry_token($token) : bool
function validate_registry_token($token) : bool
{
return Filesystem::has(PATH['project'] . '/tokens/delivery/registry/' . $token . '/token.yaml');
return Filesystem::has(PATH['project'] . '/tokens/registry/' . $token . '/token.yaml');
}
/**
* Fetch registry item
*
* endpoint: GET /api/delivery/registry
* endpoint: GET /api/registry
*
* Query:
* id - [REQUIRED] - Unique identifier of the registry item.
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
* token - [REQUIRED] - Valid Registry token.
*
* Returns:
* An array of registry item objects.
*/
$app->get('/api/delivery/registry', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
$app->get('/api/registry', function (Request $request, Response $response) use ($flextype, $api_sys_messages) {
// Get Query Params
$query = $request->getQueryParams();
@@ -49,16 +49,16 @@ $app->get('/api/delivery/registry', function (Request $request, Response $respon
$id = $query['id'];
$token = $query['token'];
if ($flextype['registry']->get('flextype.settings.api.delivery.registry.enabled')) {
if ($flextype['registry']->get('flextype.settings.api.registry.enabled')) {
// Validate delivery token
if (validate_delivery_registry_token($token)) {
$delivery_registry_token_file_path = PATH['project'] . '/tokens/delivery/registry/' . $token . '/token.yaml';
// Validate token
if (validate_registry_token($token)) {
$registry_token_file_path = PATH['project'] . '/tokens/registry/' . $token . '/token.yaml';
// Set delivery token file
if ($delivery_registry_token_file_data = $flextype['serializer']->decode(Filesystem::read($delivery_registry_token_file_path), 'yaml')) {
if ($delivery_registry_token_file_data['state'] === 'disabled' ||
($delivery_registry_token_file_data['limit_calls'] !== 0 && $delivery_registry_token_file_data['calls'] >= $delivery_registry_token_file_data['limit_calls'])) {
// Set token file
if ($registry_token_file_data = $flextype['serializer']->decode(Filesystem::read($registry_token_file_path), 'yaml')) {
if ($registry_token_file_data['state'] === 'disabled' ||
($registry_token_file_data['limit_calls'] !== 0 && $registry_token_file_data['calls'] >= $registry_token_file_data['limit_calls'])) {
return $response->withJson($api_sys_messages['AccessTokenInvalid'], 401);
}
@@ -76,7 +76,7 @@ $app->get('/api/delivery/registry', function (Request $request, Response $respon
}
// Update calls counter
Filesystem::write($delivery_registry_token_file_path, $flextype['serializer']->encode(array_replace_recursive($delivery_registry_token_file_data, ['calls' => $delivery_registry_token_file_data['calls'] + 1]), 'yaml'));
Filesystem::write($registry_token_file_path, $flextype['serializer']->encode(array_replace_recursive($registry_token_file_data, ['calls' => $registry_token_file_data['calls'] + 1]), 'yaml'));
if ($response_code == 404) {

View File

@@ -249,26 +249,18 @@ media:
# Content APIs
api:
delivery:
entries:
enabled: true
default_token:
registry:
enabled: true
default_token:
config:
enabled: true
default_token:
files:
enabled: true
default_token:
management:
entries:
enabled: true
default_token:
config:
enabled: true
default_token:
entries:
enabled: true
default_token:
registry:
enabled: true
default_token:
config:
enabled: true
default_token:
files:
enabled: true
default_token:
images:
enabled: true
default_token: