1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

feat(endpoints): add basic Api class instead of Endpoints #565

This commit is contained in:
Awilum
2021-08-13 19:03:41 +03:00
parent 9befc1488f
commit a15a5963cd
2 changed files with 119 additions and 73 deletions

View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Endpoints;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Psr7\Response;
use function count;
use function registry;
use function serializers;
class Api
{
/**
* Status code messages.
*
* @var array
* @access public
*/
private array $statusCodeMessages = [
400 => [
'title' => 'Bad Request',
'message' => 'Validation for this particular item failed',
'http_status_code' => 400
],
401 => [
'title' => 'Unauthorized',
'message' => 'Token is wrong',
'http_status_code' => 401
],
404 => [
'title' => 'Not Found',
'message' => 'Not Found',
'http_status_code' => 404
],
];
/**
* Get Status Code Message.
*
* @param int $status Status Code.
*
* @return array Message.
*/
public function getStatusCodeMessage(int $status): array
{
return $this->statusCodeMessages[$status];
}
/**
* Validate Api Request.
*/
public function validateApiRequest(array $options)
{
if (! isset($options['api']) && ! is_string($options['api'])) {
return $this->getStatusCodeMessage(400);
}
if (! isset($options['request'])) {
return $this->getStatusCodeMessage(400);
}
$data = array_merge($options['request']->getQueryParams() ?? [], $options['request']->getParsedBody() ?? []);
// Check is api enabled
if (! registry()->get('flextype.settings.api.' . $options['api'] . '.enabled')) {
return $this->getStatusCodeMessage(400);
}
// Fetch token
$tokenData = tokens()->fetch($data['token']);
if (! isset($tokenData['state']) ||
! isset($tokenData['limit_calls']) ||
! isset($tokenData['calls'])) {
return $this->getStatusCodeMessage(400);
}
if (
$tokenData['state'] === 'disabled' ||
($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls'])
) {
return $this->getStatusCodeMessage(400);
}
// Update token calls
tokens()->update($data['token'], ['calls' => $tokenData['calls'] + 1]);
return [];
}
/**
* Get API response.
*
* @param array $body Response body.
* @param int $status Status code.
*
* @return ResponseInterface Response.
*/
public function getApiResponse($response, array $body = [], int $status = 200)
{
if (count($body) > 0) {
$response->getBody()->write(serializers()->json()->encode($body));
}
$response->withStatus($status);
$response->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset'));
return $response;
}
}

View File

@@ -1,73 +0,0 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Endpoints;
use Psr\Http\Message\ResponseInterface;
use function count;
use function registry;
use function serializers;
class Endpoints
{
/**
* Status code messages.
*
* @var array
* @access public
*/
private array $statusCodeMessages = [
400 => [
'title' => 'Bad Request',
'message' => 'Validation for this particular item failed',
],
401 => [
'title' => 'Unauthorized',
'message' => 'Token is wrong',
],
404 => [
'title' => 'Not Found',
'message' => 'Not Found',
],
];
/**
* Get Status Code Message.
*
* @param int $status Status Code.
*
* @return array Message.
*/
public function getStatusCodeMessage(int $status): array
{
return $this->statusCodeMessages[$status];
}
/**
* Get API responce
*
* @param ResponseInterface $response PSR7 response.
* @param array $body Response body.
* @param int $status Status code.
*
* @return ResponseInterface Response.
*/
public function getApiResponse(ResponseInterface $response, array $body = [], int $status = 200): ResponseInterface
{
if (count($body) > 0) {
$response->getBody()->write(serializers()->json()->encode($body));
}
$response->withStatus($status);
$response->withHeader('Content-Type', 'application/json;charset=' . registry()->get('flextype.settings.charset'));
return $response;
}
}