Refactor Plex headers handling

This commit is contained in:
arabcoders
2025-05-16 18:01:10 +03:00
parent 157bcd45ad
commit fc903856f3
6 changed files with 35 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ use App\Backends\Common\Context;
use App\Backends\Common\Error;
use App\Backends\Common\Levels;
use App\Backends\Common\Response;
use App\Backends\Plex\PlexClient;
use App\Libs\Container;
use App\Libs\Enums\Http\Method;
use App\Libs\Enums\Http\Status;
@@ -299,7 +300,7 @@ final class GetUserToken
$response = $this->http->request($method->value, (string)$url, [
'headers' => array_replace_recursive([
'X-Plex-Token' => $adminToken,
'X-Plex-Client-Identifier' => $context->backendId,
...PlexClient::getHeaders()
], ag($opts, 'headers', [])),
...ag($opts, 'options', []),
]);
@@ -311,7 +312,7 @@ final class GetUserToken
$response = $this->http->request($method->value, (string)$url, [
'headers' => array_replace_recursive([
'X-Plex-Token' => $context->backendToken,
'X-Plex-Client-Identifier' => $context->backendId,
...PlexClient::getHeaders()
], ag($opts, 'headers', [])),
...ag($opts, 'options', []),
]);

View File

@@ -9,6 +9,7 @@ use App\Backends\Common\Context;
use App\Backends\Common\Error;
use App\Backends\Common\Levels;
use App\Backends\Common\Response;
use App\Backends\Plex\PlexClient;
use App\Libs\Container;
use App\Libs\Enums\Http\Status;
use App\Libs\Exceptions\Backends\InvalidArgumentException;
@@ -463,7 +464,7 @@ final class GetUsersList
$response = $this->http->request(ag($opts, 'method', 'GET'), (string)$url, [
'headers' => array_replace_recursive([
'X-Plex-Token' => $adminToken,
'X-Plex-Client-Identifier' => $context->backendId,
...PlexClient::getHeaders()
], ag($opts, 'headers', [])),
]);
if (Status::OK === Status::from($response->getStatusCode())) {
@@ -479,7 +480,7 @@ final class GetUsersList
$response = $this->http->request(ag($opts, 'method', 'GET'), (string)$url, [
'headers' => array_replace_recursive([
'X-Plex-Token' => $context->backendToken,
'X-Plex-Client-Identifier' => $context->backendId,
...PlexClient::getHeaders()
], ag($opts, 'headers', [])),
]);

View File

@@ -247,7 +247,6 @@ class Progress
'time' => $entity->getPlayProgress(),
// -- Without duration & client identifier plex ignore watch progress update.
'duration' => ag($remoteData, 'duration', 0),
'X-Plex-Client-Identifier' => $context->backendId,
]));
$logContext['remote']['url'] = (string)$url;

View File

@@ -162,7 +162,7 @@ class PlexClient implements iClient
'headers' => [
'Accept' => 'application/json',
'X-Plex-Token' => $context->backendToken,
'X-Plex-Container-Size' => 0,
...self::getHeaders()
],
], ag($context->options, 'client', [])),
trace: true === ag($context->options, Options::DEBUG_TRACE),
@@ -180,6 +180,24 @@ class PlexClient implements iClient
return $cloned;
}
/**
* Return the headers for the Plex request.
*
* @return array The headers to be used in the request.
*/
public static function getHeaders(): array
{
return [
'X-Plex-Container-Size' => 0,
'X-Plex-Product' => Config::get('name'),
'X-Plex-Version' => getAppVersion(),
'X-Plex-Platform' => php_uname('s'),
'X-Plex-Platform-Version' => php_uname('r'),
'X-Plex-Device-Name' => Config::get('name'),
'X-Plex-Client-Identifier' => PlexClient::clientId(),
];
}
/**
* @inheritdoc
*/
@@ -936,6 +954,11 @@ class PlexClient implements iClient
}
}
public static function clientId(): string
{
return md5(Config::get('name') . '/PlexClient');
}
/**
* @inheritdoc
*/

View File

@@ -64,7 +64,7 @@ final class Initializer
}
// -- This is the official place where users are supposed to store .env file.
$dataPath = env('WS_DATA_PATH', fn () => inContainer() ? '/config' : __DIR__ . '/../../var');
$dataPath = env('WS_DATA_PATH', fn() => inContainer() ? '/config' : __DIR__ . '/../../var');
if (file_exists($dataPath . '/config/.env')) {
loadEnvFile(file: $dataPath . '/config/.env', usePutEnv: true, override: true);
}
@@ -107,7 +107,7 @@ final class Initializer
$path = Config::get('path') . '/config/config.yaml';
if (file_exists($path)) {
Config::init(fn () => array_replace_recursive(Config::getAll(), Yaml::parseFile($path)));
Config::init(fn() => array_replace_recursive(Config::getAll(), Yaml::parseFile($path)));
}
$path = Config::get('path') . '/config/servers.yaml';

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Libs\Middlewares;
use App\API\Backends\PlexToken;
use App\API\Player\Index as PlayerIndex;
use App\API\System\Auth;
use App\API\System\HealthCheck;
@@ -29,6 +30,7 @@ final class AuthorizationMiddleware implements MiddlewareInterface
HealthCheck::URL,
Auth::URL,
PlayerIndex::URL,
PlexToken::URL,
];
/**
@@ -97,7 +99,7 @@ final class AuthorizationMiddleware implements MiddlewareInterface
*
* @param string|null $token The token to validate.
*
* @return bool True if the tken is valid. False otherwise.
* @return bool true if valid, false otherwise.
*/
public static function validateToken(?string $token): bool
{