Parse request for backend settings.
This commit is contained in:
@@ -4,15 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\API\Backends;
|
||||
|
||||
use App\Backends\Common\Cache as BackendCache;
|
||||
use App\Backends\Common\ClientInterface;
|
||||
use App\Backends\Common\Context;
|
||||
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\Exceptions\RuntimeException;
|
||||
use App\Libs\HTTP_STATUS;
|
||||
use App\Libs\Options;
|
||||
use App\Libs\Traits\APITraits;
|
||||
use App\Libs\Uri;
|
||||
use Psr\Http\Message\ResponseInterface as iResponse;
|
||||
use Psr\Http\Message\ServerRequestInterface as iRequest;
|
||||
|
||||
@@ -48,16 +53,28 @@ final class Add
|
||||
}
|
||||
|
||||
$instance = Container::getNew($class);
|
||||
assert($instance instanceof ClientInterface, new \RuntimeException('Invalid client class.'));
|
||||
assert($instance instanceof ClientInterface, new RuntimeException('Invalid client class.'));
|
||||
|
||||
try {
|
||||
$context = $instance->fromRequest($request);
|
||||
$config = DataUtil::fromArray($this->fromRequest($type, $request, $instance));
|
||||
|
||||
$context = new Context(
|
||||
clientName: $type,
|
||||
backendName: $name,
|
||||
backendUrl: new Uri($config->get('url')),
|
||||
cache: Container::get(BackendCache::class),
|
||||
backendId: $config->get('uuid', null),
|
||||
backendToken: $config->get('token'),
|
||||
backendUser: $config->get('user', null),
|
||||
options: $config->get('options', []),
|
||||
);
|
||||
|
||||
if (false === $instance->validateContext($context)) {
|
||||
throw new InvalidContextException('Invalid context.');
|
||||
return api_error('Invalid context information was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$configFile = ConfigFile::open(Config::get('backends_file'), 'yaml', autoSave: false);
|
||||
$configFile->set($name, $context);
|
||||
$configFile = ConfigFile::open(Config::get('backends_file'), 'yaml');
|
||||
$configFile->set($name, $config);
|
||||
$configFile->persist();
|
||||
} catch (InvalidContextException $e) {
|
||||
return api_error($e->getMessage(), HTTP_STATUS::HTTP_BAD_REQUEST);
|
||||
@@ -70,4 +87,53 @@ final class Add
|
||||
|
||||
return api_response(HTTP_STATUS::HTTP_OK, $response);
|
||||
}
|
||||
|
||||
private function fromRequest(string $type, iRequest $request, ClientInterface|null $client = null): array
|
||||
{
|
||||
$data = DataUtil::fromArray($request->getParsedBody());
|
||||
|
||||
$config = [
|
||||
'type' => $type,
|
||||
'url' => $data->get('url'),
|
||||
'token' => $data->get('token'),
|
||||
'user' => $data->get('user'),
|
||||
'uuid' => $data->get('uuid'),
|
||||
'export' => [
|
||||
'enabled' => (bool)$data->get('export.enabled', false),
|
||||
'lastSync' => (int)$data->get('export.lastSync', 0),
|
||||
],
|
||||
'import' => [
|
||||
'enabled' => (bool)$data->get('import.enabled', false),
|
||||
'lastSync' => (int)$data->get('import.lastSync', 0),
|
||||
],
|
||||
'webhook' => [
|
||||
'token' => $data->get('webhook.token'),
|
||||
'match' => [
|
||||
'user' => (bool)$data->get('webhook.match.user', false),
|
||||
'uuid' => (bool)$data->get('webhook.match.uuid', false),
|
||||
],
|
||||
],
|
||||
'options' => [],
|
||||
];
|
||||
|
||||
$optionals = [
|
||||
Options::DUMP_PAYLOAD => 'bool',
|
||||
Options::LIBRARY_SEGMENT => 'int',
|
||||
Options::IGNORE => 'string',
|
||||
];
|
||||
|
||||
foreach ($optionals as $key => $type) {
|
||||
if (null !== ($value = $data->get($key))) {
|
||||
$val = $data->get($value, $type);
|
||||
settype($val, $type);
|
||||
$config = ag_set($config, "options.{$key}", $val);
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $client) {
|
||||
$config = ag_set($config, 'options', $client->fromRequest($request));
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user