Skip to content

Commit 4f9c7b8

Browse files
committed
fix: SD code 2055 error
1 parent d6264c2 commit 4f9c7b8

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

app/Services/SchedulesDirectService.php

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,23 @@ public function authenticateFromEpg(Epg $epg): array
211211
'password' => hash('sha1', $epg->sd_password),
212212
]);
213213

214-
if ($response->failed()) {
215-
throw new Exception('Authentication failed: '.$response->body());
216-
}
217-
218214
$data = $response->json();
219215

220-
// Handle code 2055: debug not enabled - disable sd_debug to prevent user from being blocked
216+
// Handle code 2055: debug not enabled - disable sd_debug and retry without debug header
221217
if (isset($data['code']) && $data['code'] === self::DEBUG_NOT_ENABLED_CODE) {
222218
$this->handleDebugNotEnabledError();
219+
220+
// Retry authentication without the debug header
221+
Log::debug('Retrying authentication without debug header');
222+
$response = Http::withHeaders($this->buildHeaders())->post(self::BASE_URL.'/'.self::API_VERSION.'/token', [
223+
'username' => $epg->sd_username,
224+
'password' => hash('sha1', $epg->sd_password),
225+
]);
226+
$data = $response->json();
227+
}
228+
229+
if ($response->failed()) {
230+
throw new Exception('Authentication failed: '.$response->body());
223231
}
224232

225233
if (isset($data['code']) && $data['code'] !== 0) {
@@ -412,13 +420,17 @@ public function getProgramArtwork(string $token, array $programIds, ?string $epg
412420

413421
$artworkData = $response->json();
414422

415-
// Handle code 2055: debug not enabled - disable sd_debug to prevent user from being blocked
423+
// Handle code 2055: debug not enabled - disable sd_debug and retry without debug header
416424
if (isset($artworkData['code']) && $artworkData['code'] === self::DEBUG_NOT_ENABLED_CODE) {
417425
$this->handleDebugNotEnabledError();
426+
427+
// Retry the request without the debug header
428+
Log::debug('Retrying artwork request without debug header');
429+
$response = Http::withHeaders($this->buildHeaders($token))->timeout(30)->post(self::BASE_URL.'/'.self::API_VERSION.'/metadata/programs/', $batch);
430+
$artworkData = $response->json();
418431
}
419432

420433
if ($response->successful()) {
421-
422434
foreach ($artworkData as $programArtwork) {
423435
$programId = $programArtwork['programID'] ?? null;
424436
$artworkItems = $programArtwork['data'] ?? [];
@@ -1103,6 +1115,20 @@ private function processProgramBatchDirectly(array $programBatch, int $batchInde
11031115

11041116
// Stream the API response directly to a file
11051117
$response = Http::withHeaders($this->buildHeaders($token))->timeout(300)->sink($tempResponseFile)->post(self::BASE_URL.'/'.self::API_VERSION.'/programs', $programBatch);
1118+
1119+
// Check for error code 2055 in the response file (API returns error as JSON even on failure)
1120+
if (! $response->successful() && file_exists($tempResponseFile)) {
1121+
$errorContent = file_get_contents($tempResponseFile);
1122+
$errorData = json_decode($errorContent, true);
1123+
if (isset($errorData['code']) && $errorData['code'] === self::DEBUG_NOT_ENABLED_CODE) {
1124+
$this->handleDebugNotEnabledError();
1125+
1126+
// Retry the request without the debug header
1127+
Log::debug('Retrying program batch request without debug header');
1128+
$response = Http::withHeaders($this->buildHeaders($token))->timeout(300)->sink($tempResponseFile)->post(self::BASE_URL.'/'.self::API_VERSION.'/programs', $programBatch);
1129+
}
1130+
}
1131+
11061132
if ($response->successful()) {
11071133
// Stream through the program response and match with schedules immediately
11081134
$programs = Items::fromFile($tempResponseFile);
@@ -1332,9 +1358,39 @@ private function makeRequest(string $method, string $endpoint, array $data = [],
13321358
$body = $response->json();
13331359
$responseCode = $body['code'] ?? null;
13341360

1335-
// Handle code 2055: debug not enabled - disable sd_debug to prevent user from being blocked
1361+
// Handle code 2055: debug not enabled - disable sd_debug and retry without debug header
13361362
if ($responseCode === self::DEBUG_NOT_ENABLED_CODE) {
13371363
$this->handleDebugNotEnabledError();
1364+
1365+
// Retry the request without the debug header
1366+
Log::debug('Retrying request without debug header', [
1367+
'method' => $method,
1368+
'endpoint' => $endpoint,
1369+
]);
1370+
1371+
$headers = $this->buildHeaders($token);
1372+
$request = Http::withHeaders($headers)
1373+
->timeout($timeout)
1374+
->retry(2, 1000)
1375+
->withOptions([
1376+
'verify' => true,
1377+
'stream' => false,
1378+
'max_redirects' => 3,
1379+
'allow_redirects' => ['strict' => true],
1380+
]);
1381+
1382+
if ($method === 'GET' && ! empty($data)) {
1383+
$response = $request->get($url);
1384+
} elseif ($method === 'POST') {
1385+
$response = $request->post($url, $data);
1386+
} elseif ($method === 'PUT') {
1387+
$response = $request->put($url, $data);
1388+
} else {
1389+
$response = $request->send($method, $url, ['json' => $data]);
1390+
}
1391+
1392+
$body = $response->json();
1393+
$responseCode = $body['code'] ?? null;
13381394
}
13391395

13401396
if ($response->failed()) {

0 commit comments

Comments
 (0)