Make backend uuid optional if we are adding via API, after context validation it's easy to get the id.

This commit is contained in:
Abdulmhsen B. A. A
2024-04-20 20:27:19 +03:00
parent 8632e3044c
commit 7149540c74
2 changed files with 10 additions and 2 deletions

View File

@@ -68,6 +68,10 @@ final class Add
return api_error('Invalid url was given.', HTTP_STATUS::HTTP_BAD_REQUEST); return api_error('Invalid url was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
} }
if (null === ($token = $data->get('token'))) {
return api_error('No access token was given.', HTTP_STATUS::HTTP_BAD_REQUEST);
}
if (null === ($class = Config::get("supported.{$type}", null))) { if (null === ($class = Config::get("supported.{$type}", null))) {
throw api_error(r("Unexpected client type '{type}' was given.", [ throw api_error(r("Unexpected client type '{type}' was given.", [
'type' => $type 'type' => $type
@@ -86,7 +90,7 @@ final class Add
backendUrl: new Uri($config->get('url')), backendUrl: new Uri($config->get('url')),
cache: Container::get(BackendCache::class), cache: Container::get(BackendCache::class),
backendId: $config->get('uuid', null), backendId: $config->get('uuid', null),
backendToken: $config->get('token'), backendToken: $token,
backendUser: $config->get('user', null), backendUser: $config->get('user', null),
options: $config->get('options', []), options: $config->get('options', []),
); );
@@ -95,6 +99,10 @@ final class Add
return api_error('Context information validation failed.', HTTP_STATUS::HTTP_BAD_REQUEST); return api_error('Context information validation failed.', HTTP_STATUS::HTTP_BAD_REQUEST);
} }
if (!$config->get('uuid')) {
$config = $config->with('uuid', $instance->withContext($context)->getIdentifier());
}
if (!$config->has('webhook.token')) { if (!$config->has('webhook.token')) {
$config = $config->with('webhook.token', bin2hex(random_bytes(Config::get('webhook.tokenLength')))); $config = $config->with('webhook.token', bin2hex(random_bytes(Config::get('webhook.tokenLength'))));
} }

View File

@@ -37,7 +37,7 @@ final readonly class PlexValidateContext
throw new InvalidContextException('Failed to get backend id.'); throw new InvalidContextException('Failed to get backend id.');
} }
if ($backendId !== $context->backendId) { if (null !== $context->backendId && $backendId !== $context->backendId) {
throw new InvalidContextException( throw new InvalidContextException(
r("Backend id mismatch. Expected '{expected}', server responded with '{actual}'.", [ r("Backend id mismatch. Expected '{expected}', server responded with '{actual}'.", [
'expected' => $context->backendId, 'expected' => $context->backendId,