You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.5 KiB
48 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Domains\Channel\Actions\Api;
|
|
|
|
use App\Domains\Channel\Enums\Api\ApiType;
|
|
use App\Domains\Channel\Models\ApiEndpoint;
|
|
use App\Domains\Channel\Support\Mapper\ApiMapperRegistry;
|
|
use App\Domains\System\Actions\Integration\RunGenericImportPipeline;
|
|
use Exception;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class SynchronizeWithApi
|
|
{
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function execute(ApiType $apiType): void
|
|
{
|
|
try {
|
|
$mapper = ApiMapperRegistry::resolve($apiType);
|
|
$endpoint = ApiEndpoint::query()
|
|
->with('client')
|
|
->where('key', $apiType)
|
|
->first();
|
|
|
|
$domain = $endpoint->client->domain;
|
|
$url = $endpoint->value;
|
|
$credentials = $endpoint->client->credentials;
|
|
$authType = $endpoint->client->auth_type;
|
|
$url = "$domain$url";
|
|
$header = $authType->buildCredentials($credentials);
|
|
|
|
$response = Http::withHeaders($header)->get($url);
|
|
$result = $response->json()['data'];
|
|
|
|
|
|
app(RunGenericImportPipeline::class)->execute(
|
|
rows: collect($result),
|
|
mapper: $mapper,
|
|
);
|
|
} catch (Exception $e) {
|
|
Log::error('Failed to synchronize with API: '.$e->getMessage());
|
|
throw new Exception(__('domains/channel/messages.clients.fail_to_sync', ['error' => $e->getMessage()]));
|
|
}
|
|
}
|
|
}
|
|
|