♻️ DRY sync//history

This commit is contained in:
Dan Jones 2023-03-22 20:39:09 -05:00
commit fda7c13926
4 changed files with 48 additions and 30 deletions

39
app/Services/Trakt.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace App\Services;
use App\Data\WatchData;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Symfony\Component\Console\Output\OutputInterface;
class Trakt
{
protected $headers = [];
public function __construct()
{
$this->headers = [
'trakt-api-version' => 2,
'trakt-api-key' => env('TRAKT_APP_ID'),
'Authorization' => 'Bearer ' . env('TRAKT_OAUTH_TOKEN'),
];
}
public function request(array $headers = []): PendingRequest
{
return Http::withHeaders($this->headers + $headers)->baseUrl('https://api.trakt.tv');
}
public function syncHistory(WatchData $data, OutputInterface $output = null): Response
{
$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;
}
}