vid-queue/app/Services/Trakt.php

45 lines
1.4 KiB
PHP
Raw Normal View History

2023-03-22 20:39:09 -05:00
<?php
namespace App\Services;
use App\Data\WatchData;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Contracts\Config\Repository;
2023-03-22 20:39:09 -05:00
use Symfony\Component\Console\Output\OutputInterface;
class Trakt
{
protected array $headers = [];
2023-03-22 20:39:09 -05:00
public function __construct(Repository $config)
2023-03-22 20:39:09 -05:00
{
$this->headers = [
'trakt-api-version' => 2,
'trakt-api-key' => $config->get('trakt.app_id'),
'Authorization' => 'Bearer ' . $config->get('trakt.login.oauth.access_token'),
2023-03-22 20:39:09 -05:00
];
}
public function request(array $headers = []): PendingRequest
{
return Http::withHeaders($this->headers + $headers)->baseUrl('https://api.trakt.tv');
}
2025-04-07 11:05:40 -05:00
public function requestNoAuth(array $headers = []): PendingRequest
{
return Http::withHeaders(['trakt-api-version' => 2] + $headers)->baseUrl('https://api.trakt.tv');
}
2025-10-27 08:56:35 -05:00
public function syncHistory(WatchData $data, ?OutputInterface $output = null): Response
2023-03-22 20:39:09 -05:00
{
$output?->writeln(sprintf('Submitting %s to trakt with headers %s', json_encode($data->structuredData), json_encode($this->headers)));
$resp = $this->request()->post('/sync/history', $data->structuredData);
$output?->writeln('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT));
return $resp;
}
}