Skip to content

Commit

Permalink
small improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Oct 7, 2023
1 parent 94134e7 commit e1018f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
47 changes: 41 additions & 6 deletions app/Http/Clients/TikTokHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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=';
}
}
36 changes: 5 additions & 31 deletions app/Listeners/GetTikTokUserVideos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Listeners;

use App\Http\Clients\TikTokHttpClient;
use GuzzleHttp\Client;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
Expand All @@ -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();
Expand All @@ -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()
]
);
}
Expand Down

0 comments on commit e1018f5

Please sign in to comment.