feat: refactor API client to communicate via socket
This commit is contained in:
@@ -44,7 +44,8 @@ APP_BASE_URI=https://myep-team.ddev.site
|
|||||||
|
|
||||||
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
|
||||||
|
|
||||||
# This hotel code will be assigned to admin users together with ROLE_HOTEL_MANAGER
|
# This hotel code will be assigned to admin users together with ROLE_HOTEL_MANAGER
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
"ext-ctype": "*",
|
"ext-ctype": "*",
|
||||||
"ext-iconv": "*",
|
"ext-iconv": "*",
|
||||||
"ext-simplexml": "*",
|
"ext-simplexml": "*",
|
||||||
|
"ext-sockets": "*",
|
||||||
"beberlei/doctrineextensions": "^1.5",
|
"beberlei/doctrineextensions": "^1.5",
|
||||||
"doctrine/doctrine-bundle": "^2.10",
|
"doctrine/doctrine-bundle": "^2.10",
|
||||||
"doctrine/doctrine-migrations-bundle": "^3.2",
|
"doctrine/doctrine-migrations-bundle": "^3.2",
|
||||||
|
|||||||
@@ -65,7 +65,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\BusProNet\ResponseParser:
|
App\BusProNet\ResponseParser:
|
||||||
|
|||||||
+25
-17
@@ -11,10 +11,11 @@ use Psr\Log\LoggerInterface;
|
|||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Component\Uid\Uuid;
|
use Symfony\Component\Uid\Uuid;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
||||||
|
|
||||||
class ApiClient
|
class ApiClient
|
||||||
{
|
{
|
||||||
|
use ApiClientTrait;
|
||||||
|
|
||||||
public const TYPE_NOTIFICATION = 'HINWEIS';
|
public const TYPE_NOTIFICATION = 'HINWEIS';
|
||||||
public const TYPE_CUSTOMER_DATA = 'KUNDENKONTO';
|
public const TYPE_CUSTOMER_DATA = 'KUNDENKONTO';
|
||||||
public const TYPE_BASE_DATA_COUNTRIES = 'STAMMLAENDER';
|
public const TYPE_BASE_DATA_COUNTRIES = 'STAMMLAENDER';
|
||||||
@@ -24,7 +25,6 @@ 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,
|
||||||
@@ -35,6 +35,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getProfile(string $email, string $password): NotificationResponse|ProfileResponse
|
public function getProfile(string $email, string $password): NotificationResponse|ProfileResponse
|
||||||
{
|
{
|
||||||
@@ -54,6 +55,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function updateProfile(User $user, string $password): NotificationResponse|ProfileResponse
|
public function updateProfile(User $user, string $password): NotificationResponse|ProfileResponse
|
||||||
{
|
{
|
||||||
@@ -79,6 +81,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function resetPassword(string $email): NotificationResponse
|
public function resetPassword(string $email): NotificationResponse
|
||||||
{
|
{
|
||||||
@@ -97,6 +100,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getCrmAttributes(string $email, string $password): NotificationResponse|CrmAttributesResponse
|
public function getCrmAttributes(string $email, string $password): NotificationResponse|CrmAttributesResponse
|
||||||
{
|
{
|
||||||
@@ -116,6 +120,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
public function getBaseData(string $type): NotificationResponse|BaseDataResponse
|
public function getBaseData(string $type): NotificationResponse|BaseDataResponse
|
||||||
{
|
{
|
||||||
@@ -132,6 +137,7 @@ class ApiClient
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ApiClientException
|
* @throws ApiClientException
|
||||||
|
* @throws ResponseParserException
|
||||||
*/
|
*/
|
||||||
private function sendRequest(string $type, array $data): mixed
|
private function sendRequest(string $type, array $data): mixed
|
||||||
{
|
{
|
||||||
@@ -149,16 +155,17 @@ class ApiClient
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
$socket = $this->connect(
|
||||||
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
|
$this->config['bpn_api_ip'],
|
||||||
'query' => [
|
$this->config['bpn_api_port'],
|
||||||
'operation' => $body,
|
$this->config['max_retries']
|
||||||
],
|
);
|
||||||
'verify_peer' => false,
|
$this->send($socket, $body);
|
||||||
'verify_host' => false,
|
$response = $this->receive($socket);
|
||||||
]);
|
$this->disconnect($socket);
|
||||||
|
|
||||||
$xml = $response->getContent();
|
// message length (10 bytes) is prepended to actual message
|
||||||
|
$xml = substr($response, 10);
|
||||||
|
|
||||||
if (true === $this->config['debug']) {
|
if (true === $this->config['debug']) {
|
||||||
$this->logger->info('Response received', [
|
$this->logger->info('Response received', [
|
||||||
@@ -168,11 +175,6 @@ class ApiClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 createKey(string $username, string $password, string $type): string
|
private function createKey(string $username, string $password, string $type): string
|
||||||
@@ -185,8 +187,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,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\BusProNet;
|
||||||
|
|
||||||
|
trait ApiClientTrait
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @throws ApiClientException
|
||||||
|
*/
|
||||||
|
private function connect(string $host, int $port, int $maxRetries = 25)
|
||||||
|
{
|
||||||
|
$tries = 1;
|
||||||
|
$errNo = $errStr = '';
|
||||||
|
$errorCodesForRetry = [
|
||||||
|
SOCKET_ECONNREFUSED,
|
||||||
|
SOCKET_EBADF,
|
||||||
|
];
|
||||||
|
|
||||||
|
$openSocket = function (&$errNo, &$errStr) use ($host, $port) {
|
||||||
|
return @fsockopen(
|
||||||
|
$host,
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user