Added new Client method to generate and validate context from API.
This commit is contained in:
73
src/API/Backends/Add.php
Normal file
73
src/API/Backends/Add.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\API\Backends;
|
||||
|
||||
use App\Backends\Common\ClientInterface;
|
||||
use App\Libs\Attributes\Route\Post;
|
||||
use App\Libs\Config;
|
||||
use App\Libs\ConfigFile;
|
||||
use App\Libs\Container;
|
||||
use App\Libs\DataUtil;
|
||||
use App\Libs\Exceptions\Backends\InvalidContextException;
|
||||
use App\Libs\HTTP_STATUS;
|
||||
use App\Libs\Traits\APITraits;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
|
||||
final class Add
|
||||
{
|
||||
use APITraits;
|
||||
|
||||
#[Post(Index::URL . '[/]', name: 'backends.add')]
|
||||
public function BackendAdd(iRequest $request): iResponse
|
||||
{
|
||||
$data = DataUtil::fromArray($request->getParsedBody());
|
||||
|
||||
if (null === ($type = $data->get('type'))) {
|
||||
return api_error('No type was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (null === ($name = $data->get('name'))) {
|
||||
return api_error('No name was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$backend = $this->getBackends(name: $name);
|
||||
|
||||
if (!empty($backend)) {
|
||||
return api_error(r("Backend '{backend}' already exists.", [
|
||||
'backend' => $name
|
||||
]), HTTP_STATUS::HTTP_CONFLICT);
|
||||
}
|
||||
|
||||
if (null === ($class = Config::get("supported.{$type}", null))) {
|
||||
throw api_error(r("Unexpected client type '{type}' was given.", [
|
||||
'type' => $type
|
||||
]), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$instance = Container::getNew($class);
|
||||
assert($instance instanceof ClientInterface, new \RuntimeException('Invalid client class.'));
|
||||
|
||||
try {
|
||||
$context = $instance->fromRequest($request);
|
||||
if (false === $instance->validateContext($context)) {
|
||||
throw new InvalidContextException('Invalid context.');
|
||||
}
|
||||
|
||||
$configFile = ConfigFile::open(Config::get('backends_file'), 'yaml', autoSave: false);
|
||||
$configFile->set($name, $context);
|
||||
$configFile->persist();
|
||||
} catch (InvalidContextException $e) {
|
||||
return api_error($e->getMessage(), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$response = [
|
||||
'backends' => [],
|
||||
'links' => [],
|
||||
];
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, $response);
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,16 @@ declare(strict_types=1);
|
||||
namespace App\API\Backends;
|
||||
|
||||
use App\Libs\Attributes\Route\Get;
|
||||
use App\Libs\Config;
|
||||
use App\Libs\ConfigFile;
|
||||
use App\Libs\HTTP_STATUS;
|
||||
use App\Libs\Options;
|
||||
use App\Libs\Traits\APITraits;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
|
||||
final class Index
|
||||
{
|
||||
use APITraits;
|
||||
|
||||
public const string URL = '%{api.prefix}/backends';
|
||||
|
||||
public const array BLACK_LIST = [
|
||||
@@ -35,7 +36,7 @@ final class Index
|
||||
],
|
||||
];
|
||||
|
||||
foreach (self::getBackends() as $backend) {
|
||||
foreach ($this->getBackends() as $backend) {
|
||||
$backend = array_filter(
|
||||
$backend,
|
||||
fn($key) => false === in_array($key, ['options', 'webhook'], true),
|
||||
@@ -51,54 +52,4 @@ final class Index
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, $response);
|
||||
}
|
||||
|
||||
#[Get(self::URL . '/{id:backend}[/]', name: 'backends.view')]
|
||||
public function backendsView(iRequest $request, array $args = []): iResponse
|
||||
{
|
||||
if (null === ($id = ag($args, 'id'))) {
|
||||
return api_error('Invalid value for id path parameter.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$data = Index::getBackends(name: $id);
|
||||
if (empty($data)) {
|
||||
return api_error('Backend not found.', HTTP_STATUS::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('');
|
||||
$data = array_pop($data);
|
||||
|
||||
$response = [
|
||||
...$data,
|
||||
'links' => [
|
||||
'self' => (string)$apiUrl,
|
||||
'list' => (string)$apiUrl->withPath(parseConfigValue(self::URL)),
|
||||
],
|
||||
];
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, ['backend' => $response]);
|
||||
}
|
||||
|
||||
private function getBackends(string|null $name = null): array
|
||||
{
|
||||
$backends = [];
|
||||
|
||||
foreach (ConfigFile::open(Config::get('backends_file'), 'yaml')->getAll() as $backendName => $backend) {
|
||||
$backend = ['name' => $backendName, ...$backend];
|
||||
|
||||
if (null !== ag($backend, 'import.lastSync')) {
|
||||
$backend = ag_set($backend, 'import.lastSync', makeDate(ag($backend, 'import.lastSync')));
|
||||
}
|
||||
|
||||
if (null !== ag($backend, 'export.lastSync')) {
|
||||
$backend = ag_set($backend, 'export.lastSync', makeDate(ag($backend, 'export.lastSync')));
|
||||
}
|
||||
|
||||
$backends[] = $backend;
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
return array_filter($backends, fn($backend) => $backend['name'] === $name);
|
||||
}
|
||||
return $backends;
|
||||
}
|
||||
}
|
||||
|
||||
43
src/API/Backends/View.php
Normal file
43
src/API/Backends/View.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\API\Backends;
|
||||
|
||||
use App\Libs\Attributes\Route\Get;
|
||||
use App\Libs\HTTP_STATUS;
|
||||
use App\Libs\Traits\APITraits;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
|
||||
final class View
|
||||
{
|
||||
use APITraits;
|
||||
|
||||
#[Get(Index::URL . '/{id:backend}[/]', name: 'backends.view')]
|
||||
public function backendsView(iRequest $request, array $args = []): iResponse
|
||||
{
|
||||
if (null === ($id = ag($args, 'id'))) {
|
||||
return api_error('Invalid value for id path parameter.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$data = $this->getBackends(name: $id);
|
||||
|
||||
if (empty($data)) {
|
||||
return api_error(r("Backend '{backend}' not found.", ['backend ' => $id]), HTTP_STATUS::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$apiUrl = $request->getUri()->withHost('')->withPort(0)->withScheme('');
|
||||
$data = array_pop($data);
|
||||
|
||||
$response = [
|
||||
...$data,
|
||||
'links' => [
|
||||
'self' => (string)$apiUrl,
|
||||
'list' => (string)$apiUrl->withPath(parseConfigValue(Index::URL)),
|
||||
],
|
||||
];
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, ['backend' => $response]);
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ final class GenerateAccessToken
|
||||
}
|
||||
|
||||
try {
|
||||
$client = $this->getBackend($backend);
|
||||
$client = $this->getClient($backend);
|
||||
} catch (RuntimeException $e) {
|
||||
return api_error($e->getMessage(), HTTP_STATUS::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user