mirror of
https://github.com/flextype/flextype.git
synced 2025-08-14 00:54:03 +02:00
refactor(core): updates for delivery api's
This commit is contained in:
@@ -18,28 +18,38 @@ use function count;
|
||||
/**
|
||||
* Validate delivery entries token
|
||||
*/
|
||||
function validate_delivery_entries_token($request, $flextype) : bool
|
||||
function validate_delivery_entries_token($token) : bool
|
||||
{
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml');
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/entries/' . $token . '/token.yaml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch entry(entries)
|
||||
*
|
||||
* endpoint: /api/delivery/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) {
|
||||
|
||||
// Get Query Params
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
// Set variables
|
||||
$id = $query['id'];
|
||||
$data = $query['args'] ?? null;
|
||||
$id = $query['id'];
|
||||
$token = $query['token'];
|
||||
$filter = $query['filter'] ?? null;
|
||||
|
||||
if ($flextype['registry']->get('flextype.settings.api.entries.enabled')) {
|
||||
// Validate delivery token
|
||||
if (validate_delivery_entries_token($request, $flextype)) {
|
||||
$delivery_entries_token_file_path = PATH['site'] . '/tokens/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml';
|
||||
if (validate_delivery_entries_token($token)) {
|
||||
$delivery_entries_token_file_path = PATH['site'] . '/tokens/delivery/entries/' . $token. '/token.yaml';
|
||||
|
||||
// Set delivery token file
|
||||
if ($delivery_entries_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) {
|
||||
@@ -49,10 +59,10 @@ $app->get('/api/delivery/entries', function (Request $request, Response $respons
|
||||
}
|
||||
|
||||
// Fetch entry
|
||||
$data = $flextype['entries']->fetch($id, $data);
|
||||
$data['data'] = $flextype['entries']->fetch($id, $filter);
|
||||
|
||||
// Set response code
|
||||
$response_code = count($data) > 0 ? 200 : 404;
|
||||
$response_code = count($data['data']) > 0 ? 200 : 404;
|
||||
|
||||
// Update calls counter
|
||||
Filesystem::write($delivery_entries_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml'));
|
||||
|
@@ -18,24 +18,38 @@ use function header;
|
||||
/**
|
||||
* Validate delivery images token
|
||||
*/
|
||||
function validate_delivery_images_token($request, $flextype) : bool
|
||||
function validate_delivery_images_token($token) : bool
|
||||
{
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml');
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/images/' . $token . '/token.yaml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch image
|
||||
*
|
||||
* endpoint: /api/delivery/images
|
||||
* endpoint: GET /api/delivery/images
|
||||
*
|
||||
* Parameters:
|
||||
* path - [REQUIRED] - The file path with valid params for image manipulations.
|
||||
*
|
||||
* Query:
|
||||
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
|
||||
*
|
||||
* Returns:
|
||||
* Image file
|
||||
*/
|
||||
$app->get('/api/delivery/images/{path:.+}', function (Request $request, Response $response, array $args) use ($flextype) {
|
||||
$app->get('/api/delivery/images/{path:.+}', function (Request $request, Response $response, $args) use ($flextype) {
|
||||
|
||||
// Get Query Params
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
// Set variables
|
||||
$token = $query['token'];
|
||||
|
||||
if ($flextype['registry']->get('flextype.settings.api.images.enabled')) {
|
||||
|
||||
// Validate delivery image token
|
||||
if (validate_delivery_images_token($request, $flextype)) {
|
||||
$delivery_images_token_file_path = PATH['site'] . '/site/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml';
|
||||
if (validate_delivery_images_token($token)) {
|
||||
$delivery_images_token_file_path = PATH['site'] . '/site/delivery/images/' . $token . '/token.yaml';
|
||||
|
||||
// Set delivery token file
|
||||
if ($delivery_images_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_images_token_file_path), 'yaml')) {
|
||||
|
@@ -17,27 +17,37 @@ use function array_replace_recursive;
|
||||
/**
|
||||
* Validate delivery registry token
|
||||
*/
|
||||
function validate_delivery_registry_token($request, $flextype) : bool
|
||||
function validate_delivery_registry_token($token) : bool
|
||||
{
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml');
|
||||
return Filesystem::has(PATH['site'] . '/tokens/delivery/registry/' . $token . '/token.yaml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch registry
|
||||
* Fetch registry item
|
||||
*
|
||||
* endpoint: /api/delivery/registry
|
||||
* endpoint: GET /api/delivery/registry
|
||||
*
|
||||
* Query:
|
||||
* id - [REQUIRED] - Unique identifier of the registry item.
|
||||
* token - [REQUIRED] - Valid Content Delivery API token for Entries.
|
||||
*
|
||||
* Returns:
|
||||
* An array of registry item objects.
|
||||
*/
|
||||
$app->get('/api/delivery/registry', function (Request $request, Response $response) use ($flextype) {
|
||||
|
||||
// Get Query Params
|
||||
$query = $request->getQueryParams();
|
||||
|
||||
// Set variables
|
||||
$id = $query['id'];
|
||||
$id = $query['id'];
|
||||
$token = $query['token'];
|
||||
|
||||
if ($flextype['registry']->get('flextype.settings.api.registry.enabled')) {
|
||||
|
||||
// Validate delivery token
|
||||
if (validate_delivery_registry_token($request, $flextype)) {
|
||||
$delivery_registry_token_file_path = PATH['site'] . '/tokens/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml';
|
||||
if (validate_delivery_registry_token($token)) {
|
||||
$delivery_registry_token_file_path = PATH['site'] . '/tokens/delivery/registry/' . $token . '/token.yaml';
|
||||
|
||||
// Set delivery token file
|
||||
if ($delivery_registry_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_registry_token_file_path), 'yaml')) {
|
||||
@@ -47,7 +57,12 @@ $app->get('/api/delivery/registry', function (Request $request, Response $respon
|
||||
}
|
||||
|
||||
// Fetch registry
|
||||
$data = $flextype['registry']->get($id);
|
||||
if ($flextype['registry']->has($id)) {
|
||||
$data['data']['key'] = $id;
|
||||
$data['data']['value'] = $flextype['registry']->get($id);
|
||||
} else {
|
||||
$data = [];
|
||||
}
|
||||
|
||||
// Update calls counter
|
||||
Filesystem::write($delivery_registry_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_registry_token_file_data, ['calls' => $delivery_registry_token_file_data['calls'] + 1]), 'yaml'));
|
||||
|
Reference in New Issue
Block a user