1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-17 10:30:46 +02:00

refactor(core): delivery api - rename api's

This commit is contained in:
Awilum
2020-02-10 16:34:18 +03:00
parent dd9e894213
commit 9f69738049
2 changed files with 0 additions and 92 deletions

View File

@@ -1,67 +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;
/**
* Validate delivery token
*/
function validate_delivery_token($request, $flextype) : bool
{
return Filesystem::has(PATH['tokens'] . '/delivery/' . $request->getQueryParams()['delivery_token'] . '/token.yaml');
}
/**
* Fetch entry(entries)
*
* endpoint: /api/entries
*/
$app->get('/api/entries', function (Request $request, Response $response) use ($flextype) {
// Get Query Params
$query = $request->getQueryParams();
// Set variables
$id = $query['id'];
$args = isset($query['args']) ? $query['args'] : null;
// Validate delivery token
if (validate_delivery_token($request, $flextype)) {
$delivery_token_file_path = PATH['tokens'] . '/delivery/' . $request->getQueryParams()['delivery_token'] . '/token.yaml';
// Set delivery token file
if ($delivery_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_token_file_path), 'yaml')) {
if ($delivery_token_file_data['state'] == 'disabled' ||
($delivery_token_file_data['limit_calls'] != 0 && $delivery_token_file_data['calls'] >= $delivery_token_file_data['limit_calls'])) {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
} else {
// Fetch entry
$data = $flextype['entries']->fetch($id, $args);
// Set response code
$response_code = (count($data) > 0) ? 200 : 404 ;
// Update calls counter
Filesystem::write($delivery_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_token_file_data, ['calls' => $delivery_token_file_data['calls'] + 1]), 'yaml'));
// Return response
return $response->withJson($data, $response_code);
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
});

View File

@@ -1,25 +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;
/**
* Generates and returns the image response
*/
$app->get('/api/image/{path:.+}', function (Request $request, Response $response, array $args) use ($flextype) {
if (Filesystem::has(PATH['entries'] . '/' . $args['path'])) {
return $flextype['images']->getImageResponse($args['path'], $_GET);
}
return $response->withStatus(404);
});