<?php
declare(strict_types=1);
namespace App\Client\Tapfiliate\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\SerializerInterface;
abstract class ClientAbstract
{
private const BASE_URI = 'https://api.tapfiliate.com';
private string $baseUri;
private string $token;
private Client $client;
private SerializerInterface $serializer;
private LoggerInterface $logger;
public function __construct(string $token, SerializerInterface $serializer, LoggerInterface $logger)
{
$this->baseUri = self::BASE_URI;
$this->token = $token;
$this->client = new Client(
[
'headers' => [
'X-Api-Key' => $this->token,
],
'connect_timeout' => 10,
'base_uri' => $this->baseUri,
]
);
$this->serializer = $serializer;
$this->logger = $logger;
}
/**
* @throws GuzzleException|\Throwable
*/
protected function get(string $endpoint, array $requestData = []): ResponseInterface
{
return $this->client->request('GET', $endpoint, $requestData);
}
/**
* @throws GuzzleException
*/
protected function post(string $endpoint, array $bodyRequestData = []): ResponseInterface
{
$bodyRequestContents = $this->serializer->serialize($bodyRequestData, 'json');
return $this->client->request('POST', $endpoint, ['body' => $bodyRequestContents]);
}
/**
* @throws GuzzleException
*/
protected function patch(string $endpoint, array $bodyRequestData = []): ResponseInterface
{
$bodyRequestContents = $this->serializer->serialize($bodyRequestData, 'json');
return $this->client->request('PATCH', $endpoint, ['body' => $bodyRequestContents]);
}
public function getSerializer(): SerializerInterface
{
return $this->serializer;
}
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}