feat: refactor API client to connect via socket
This commit is contained in:
@@ -42,7 +42,8 @@ MAILER_DSN=null://null
|
|||||||
|
|
||||||
APP_BPN_USER=
|
APP_BPN_USER=
|
||||||
APP_BPN_PASSWORD=
|
APP_BPN_PASSWORD=
|
||||||
APP_BPN_ENDPOINT=
|
APP_BPN_IP=
|
||||||
|
APP_BPN_PORT=
|
||||||
APP_BPN_DEBUG=false
|
APP_BPN_DEBUG=false
|
||||||
|
|
||||||
API_KEYS=
|
API_KEYS=
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
"ext-ctype": "*",
|
"ext-ctype": "*",
|
||||||
"ext-iconv": "*",
|
"ext-iconv": "*",
|
||||||
"ext-simplexml": "*",
|
"ext-simplexml": "*",
|
||||||
|
"ext-sockets": "*",
|
||||||
"doctrine/dbal": "^3",
|
"doctrine/dbal": "^3",
|
||||||
"doctrine/doctrine-bundle": "^2.13",
|
"doctrine/doctrine-bundle": "^2.13",
|
||||||
"doctrine/doctrine-migrations-bundle": "^3.3",
|
"doctrine/doctrine-migrations-bundle": "^3.3",
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ services:
|
|||||||
$options:
|
$options:
|
||||||
bpn_username: '%env(APP_BPN_USER)%'
|
bpn_username: '%env(APP_BPN_USER)%'
|
||||||
bpn_password: '%env(APP_BPN_PASSWORD)%'
|
bpn_password: '%env(APP_BPN_PASSWORD)%'
|
||||||
bpn_url: '%env(APP_BPN_ENDPOINT)%'
|
bpn_api_ip: '%env(APP_BPN_IP)%'
|
||||||
|
bpn_api_port: '%env(APP_BPN_PORT)%'
|
||||||
debug: '%env(bool:APP_BPN_DEBUG)%'
|
debug: '%env(bool:APP_BPN_DEBUG)%'
|
||||||
|
|
||||||
App\Twig\AppRuntime:
|
App\Twig\AppRuntime:
|
||||||
|
|||||||
+103
-25
@@ -5,11 +5,11 @@ namespace App\BusProNet;
|
|||||||
use App\BusProNet\ApiResponseParser\ResponseParser;
|
use App\BusProNet\ApiResponseParser\ResponseParser;
|
||||||
use App\BusProNet\DataProcessor\BookingDataProcessor;
|
use App\BusProNet\DataProcessor\BookingDataProcessor;
|
||||||
use App\BusProNet\Exception\ApiClientException;
|
use App\BusProNet\Exception\ApiClientException;
|
||||||
|
use App\BusProNet\Exception\ResponseParserException;
|
||||||
use App\BusProNet\Model\BaseData;
|
use App\BusProNet\Model\BaseData;
|
||||||
use App\BusProNet\Model\Booking;
|
use App\BusProNet\Model\Booking;
|
||||||
use App\BusProNet\Model\BookingUpdate;
|
use App\BusProNet\Model\BookingUpdate;
|
||||||
use App\BusProNet\Model\CrmAttributes;
|
use App\BusProNet\Model\CrmAttributes;
|
||||||
use App\BusProNet\Model\File;
|
|
||||||
use App\BusProNet\Model\Notification;
|
use App\BusProNet\Model\Notification;
|
||||||
use App\BusProNet\Model\PersonalData;
|
use App\BusProNet\Model\PersonalData;
|
||||||
use App\Form\Model\BookingData;
|
use App\Form\Model\BookingData;
|
||||||
@@ -18,7 +18,6 @@ use Psr\Log\LoggerInterface;
|
|||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
||||||
|
|
||||||
class ApiClient
|
class ApiClient
|
||||||
{
|
{
|
||||||
@@ -32,18 +31,17 @@ class ApiClient
|
|||||||
private array $config;
|
private array $config;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly HttpClientInterface $httpClient,
|
|
||||||
private readonly SerializerInterface $serializer,
|
private readonly SerializerInterface $serializer,
|
||||||
private readonly ResponseParser $responseParser,
|
private readonly ResponseParser $responseParser,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
array $options
|
array $options
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
$this->config = $this->resolveOptions($options);
|
$this->config = $this->resolveOptions($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getPersonalData(string $email, string $password): Notification|PersonalData
|
public function getPersonalData(string $email, string $password): Notification|PersonalData
|
||||||
{
|
{
|
||||||
@@ -61,6 +59,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function register(RegistrationData $registrationData): Notification
|
public function register(RegistrationData $registrationData): Notification
|
||||||
{
|
{
|
||||||
@@ -84,6 +83,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function resetPassword(string $email): Notification
|
public function resetPassword(string $email): Notification
|
||||||
{
|
{
|
||||||
@@ -100,6 +100,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function updatePersonalData(string $email, string $password, PersonalData $personalData): Notification|PersonalData
|
public function updatePersonalData(string $email, string $password, PersonalData $personalData): Notification|PersonalData
|
||||||
{
|
{
|
||||||
@@ -119,6 +120,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function updateNewsletterRegistration(string $email, string $password, PersonalData $personalData): Notification|PersonalData
|
public function updateNewsletterRegistration(string $email, string $password, PersonalData $personalData): Notification|PersonalData
|
||||||
{
|
{
|
||||||
@@ -141,6 +143,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getBookings(string $email, string $password): Notification|BaseData
|
public function getBookings(string $email, string $password): Notification|BaseData
|
||||||
{
|
{
|
||||||
@@ -156,9 +159,6 @@ class ApiClient
|
|||||||
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ApiClientException
|
|
||||||
*/
|
|
||||||
public function getBooking(string $email, string $password, int $id): Notification|Booking
|
public function getBooking(string $email, string $password, int $id): Notification|Booking
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
@@ -174,6 +174,10 @@ class ApiClient
|
|||||||
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
return $this->sendRequest(static::TYPE_CUSTOMER_DATA, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
|
*/
|
||||||
public function updateBooking(BookingData $formData, bool $dryRun = true): Notification|BookingUpdate
|
public function updateBooking(BookingData $formData, bool $dryRun = true): Notification|BookingUpdate
|
||||||
{
|
{
|
||||||
$mode = $dryRun ? 'Anfrage' : 'Buchung';
|
$mode = $dryRun ? 'Anfrage' : 'Buchung';
|
||||||
@@ -192,6 +196,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getMutableData(int $id): Notification|BaseData
|
public function getMutableData(int $id): Notification|BaseData
|
||||||
{
|
{
|
||||||
@@ -208,6 +213,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getAvailabilities(int $id): Notification|BaseData
|
public function getAvailabilities(int $id): Notification|BaseData
|
||||||
{
|
{
|
||||||
@@ -223,6 +229,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getCrmAttributes(string $email, string $password): Notification|CrmAttributes
|
public function getCrmAttributes(string $email, string $password): Notification|CrmAttributes
|
||||||
{
|
{
|
||||||
@@ -240,6 +247,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getBaseData(string $type): Notification|BaseData
|
public function getBaseData(string $type): Notification|BaseData
|
||||||
{
|
{
|
||||||
@@ -253,8 +261,8 @@ class ApiClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Notification|File|null
|
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getDocuments(string $email, string $password, int $id, string $type): mixed
|
public function getDocuments(string $email, string $password, int $id, string $type): mixed
|
||||||
{
|
{
|
||||||
@@ -274,6 +282,78 @@ class ApiClient
|
|||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
*/
|
*/
|
||||||
|
private function connect()
|
||||||
|
{
|
||||||
|
$tries = 1;
|
||||||
|
$errNo = $errStr = '';
|
||||||
|
$maxRetries = $this->config['max_retries'];
|
||||||
|
$errorCodesForRetry = [
|
||||||
|
SOCKET_ECONNREFUSED,
|
||||||
|
SOCKET_EBADF,
|
||||||
|
];
|
||||||
|
|
||||||
|
$openSocket = function (&$errNo, &$errStr) {
|
||||||
|
return @fsockopen(
|
||||||
|
$this->config['bpn_api_ip'],
|
||||||
|
$this->config['bpn_api_port'],
|
||||||
|
$errNo,
|
||||||
|
$errStr,
|
||||||
|
10
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
$socket = $openSocket($errNo, $errStr);
|
||||||
|
|
||||||
|
while (false === $socket && true === in_array($errNo, $errorCodesForRetry) && $maxRetries > $tries) {
|
||||||
|
$this->logger->warning('Could not connect to socket, retrying', [
|
||||||
|
'error_message' => $errStr,
|
||||||
|
'error_number' => $errNo,
|
||||||
|
]);
|
||||||
|
++$tries;
|
||||||
|
sleep(1);
|
||||||
|
$socket = $openSocket($errNo, $errStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false !== $socket) {
|
||||||
|
stream_set_timeout($socket, 60);
|
||||||
|
} else {
|
||||||
|
$this->logger->error('Unable to open socket', [
|
||||||
|
'error_message' => $errStr,
|
||||||
|
'error_number' => $errNo,
|
||||||
|
]);
|
||||||
|
throw new ApiClientException('Unable to open socket');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function send($socket, string $data): void
|
||||||
|
{
|
||||||
|
// message length is prepended to actual message
|
||||||
|
$send = sprintf('%010s', strlen($data)) . $data;
|
||||||
|
fwrite($socket, $send);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function receive($socket): string
|
||||||
|
{
|
||||||
|
$response = '';
|
||||||
|
|
||||||
|
while (false === feof($socket)) {
|
||||||
|
$response .= fread($socket, 4096);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function disconnect($socket): void
|
||||||
|
{
|
||||||
|
@fclose($socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
|
*/
|
||||||
private function sendRequest(string $type, array $data): mixed
|
private function sendRequest(string $type, array $data): mixed
|
||||||
{
|
{
|
||||||
$requestId = date(DATE_ATOM).uniqid();
|
$requestId = date(DATE_ATOM).uniqid();
|
||||||
@@ -286,31 +366,23 @@ class ApiClient
|
|||||||
])
|
])
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
if (true === $this->config['debug']) {
|
if (true === $this->config['debug']) {
|
||||||
$this->dumpXmlToFile('request', $requestId, $body);
|
$this->dumpXmlToFile('request', $requestId, $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
$socket = $this->connect();
|
||||||
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
|
$this->send($socket, $body);
|
||||||
'query' => [
|
$response = $this->receive($socket);
|
||||||
'operation' => $body,
|
// message length (10 bytes) is prepended to actual message
|
||||||
],
|
$xml = substr($response, 10);
|
||||||
'verify_peer' => false,
|
$this->disconnect($socket);
|
||||||
'verify_host' => false,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$xml = $response->getContent();
|
|
||||||
|
|
||||||
if (true === $this->config['debug']) {
|
if (true === $this->config['debug']) {
|
||||||
$this->dumpXmlToFile('response', $requestId, $xml);
|
$this->dumpXmlToFile('response', $requestId, $xml);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->responseParser->parseXmlString($type, $xml);
|
return $this->responseParser->parseXmlString($type, $xml);
|
||||||
} catch (\Throwable $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->logger->error('API error', ['error' => $e->getMessage()]);
|
|
||||||
throw new ApiClientException($e->getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function dumpXmlToFile(string $type, string $requestId, string $body): void
|
private function dumpXmlToFile(string $type, string $requestId, string $body): void
|
||||||
@@ -332,8 +404,14 @@ class ApiClient
|
|||||||
private function resolveOptions(array $options): array
|
private function resolveOptions(array $options): array
|
||||||
{
|
{
|
||||||
$optionsResolver = new OptionsResolver();
|
$optionsResolver = new OptionsResolver();
|
||||||
$optionsResolver->setRequired(['bpn_url', 'bpn_username', 'bpn_password']);
|
$optionsResolver->setRequired([
|
||||||
|
'bpn_username',
|
||||||
|
'bpn_password',
|
||||||
|
'bpn_api_ip',
|
||||||
|
'bpn_api_port',
|
||||||
|
]);
|
||||||
$optionsResolver->setDefaults([
|
$optionsResolver->setDefaults([
|
||||||
|
'max_retries' => 25,
|
||||||
'debug' => false,
|
'debug' => false,
|
||||||
'target_folder_dumps' => '/var/www/html/var/bpn',
|
'target_folder_dumps' => '/var/www/html/var/bpn',
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user