diff --git a/app/Http/Clients/TikTokHttpClient.php b/app/Http/Clients/TikTokHttpClient.php index 60a3a006..b0ad10e3 100644 --- a/app/Http/Clients/TikTokHttpClient.php +++ b/app/Http/Clients/TikTokHttpClient.php @@ -4,13 +4,28 @@ namespace App\Http\Clients; +use App\Dtos\TikTokUserDto; use GuzzleHttp\Client; +use Illuminate\Support\Arr; class TikTokHttpClient { protected array $config; protected Client $client; + public static array $claims = [ + 'cover_image_url', + 'id', + 'title', + 'video_description', + 'duration', + 'height', + 'width', + 'title', + 'embed_html', + 'embed_link' + ]; + public function __construct(Client $client) { $this->config = config('services.tiktok'); @@ -21,7 +36,7 @@ public function getAccessToken(string $code) { $response = $this->client->request( 'POST', - $this->getUri() . '/oauth/access_token/', + $this->getV1BaseUri() . '/oauth/access_token/', [ 'form_params' => [ 'client_key' => $this->getClientId(), @@ -44,7 +59,7 @@ public function getAccessToken(string $code) public function getUserInfo(string $open_id, string $access_token) { $userInfoResponse = $this->client->request('POST', - $this->getUri() . '/user/info/', + $this->getV1BaseUri() . '/user/info/', [ 'json' => [ 'open_id' => $open_id, @@ -70,18 +85,38 @@ public function getUserInfo(string $open_id, string $access_token) return json_decode($userInfoResponse->getBody()->getContents(), true); } - private function getUri(): string + public function listVideos(TikTokUserDto $userDto): array { - return $this->config['uri'] ?? ''; + $response = $this->client->request('POST', + self::getVideoListEndpoint() . implode( ',', self::$claims), + [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $userDto->getCode(), + 'Content-Type' => 'application/json' + ] + ] + ); + + return json_decode($response->getBody()->getContents(), true); + } + + private function getV1BaseUri(): string + { + return Arr::get($this->config, 'uri'); } private function getClientId(): string { - return $this->config['client_id'] ?? ''; + return Arr::get($this->config, 'client_id'); } private function getClientSecret(): string { - return $this->config['client_secret'] ?? ''; + return Arr::get($this->config, 'client_secret'); + } + + public static function getVideoListEndpoint(): string + { + return 'https://open.tiktokapis.com/v2/video/list/?fields='; } } diff --git a/app/Listeners/GetTikTokUserVideos.php b/app/Listeners/GetTikTokUserVideos.php index f2006eef..251ce230 100644 --- a/app/Listeners/GetTikTokUserVideos.php +++ b/app/Listeners/GetTikTokUserVideos.php @@ -2,6 +2,7 @@ namespace App\Listeners; +use App\Http\Clients\TikTokHttpClient; use GuzzleHttp\Client; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; @@ -14,37 +15,10 @@ class GetTikTokUserVideos */ public function handle(object $event): void { - $client = new Client(); - $tikTokUser = $event->tikTokUserDto; - - $claims = [ - 'cover_image_url', - 'id', - 'title', - 'video_description', - 'duration', - 'height', - 'width', - 'title', - 'embed_html', - 'embed_link' - ]; - - $endpoint = 'https://open.tiktokapis.com/v2/video/list/?fields='; + $client = new TikTokHttpClient(new Client()); try { - $response = $client->request('POST', - $endpoint . implode( ',', $claims), - [ - 'headers' => [ - 'Authorization' => 'Bearer ' . $tikTokUser->getCode(), - 'Content-Type' => 'application/json' - ] - ] - ); - - $decoded = json_decode($response->getBody()->getContents(), true); - + $decoded = $client->listVideos($event->tikTokUserDto); $db = DB::table('tiktok_users'); $tiktok_user = $db->where(['user_id' => $event->tikTokUserDto->getUserId()])->first(); @@ -65,8 +39,8 @@ public function handle(object $event): void 'Error listing TikTok Videos', [ 'errorMsg' => $exception->getMessage(), - 'claims' => $claims, - 'endpoint' => $endpoint + 'claims' => TikTokHttpClient::$claims, + 'endpoint' => TikTokHttpClient::getVideoListEndpoint() ] ); }