feat: refactor API client to communicate via socket
This commit is contained in:
+32
-24
@@ -11,10 +11,11 @@ use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class ApiClient
|
||||
{
|
||||
use ApiClientTrait;
|
||||
|
||||
public const TYPE_NOTIFICATION = 'HINWEIS';
|
||||
public const TYPE_CUSTOMER_DATA = 'KUNDENKONTO';
|
||||
public const TYPE_BASE_DATA_COUNTRIES = 'STAMMLAENDER';
|
||||
@@ -24,7 +25,6 @@ class ApiClient
|
||||
private array $config;
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $httpClient,
|
||||
private readonly SerializerInterface $serializer,
|
||||
private readonly ResponseParser $responseParser,
|
||||
private readonly LoggerInterface $logger,
|
||||
@@ -35,6 +35,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
public function getProfile(string $email, string $password): NotificationResponse|ProfileResponse
|
||||
{
|
||||
@@ -54,6 +55,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
public function updateProfile(User $user, string $password): NotificationResponse|ProfileResponse
|
||||
{
|
||||
@@ -79,6 +81,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
public function resetPassword(string $email): NotificationResponse
|
||||
{
|
||||
@@ -97,6 +100,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
public function getCrmAttributes(string $email, string $password): NotificationResponse|CrmAttributesResponse
|
||||
{
|
||||
@@ -116,6 +120,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
public function getBaseData(string $type): NotificationResponse|BaseDataResponse
|
||||
{
|
||||
@@ -132,6 +137,7 @@ class ApiClient
|
||||
|
||||
/**
|
||||
* @throws ApiClientException
|
||||
* @throws ResponseParserException
|
||||
*/
|
||||
private function sendRequest(string $type, array $data): mixed
|
||||
{
|
||||
@@ -149,30 +155,26 @@ class ApiClient
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request('GET', $this->config['bpn_url'], [
|
||||
'query' => [
|
||||
'operation' => $body,
|
||||
],
|
||||
'verify_peer' => false,
|
||||
'verify_host' => false,
|
||||
$socket = $this->connect(
|
||||
$this->config['bpn_api_ip'],
|
||||
$this->config['bpn_api_port'],
|
||||
$this->config['max_retries']
|
||||
);
|
||||
$this->send($socket, $body);
|
||||
$response = $this->receive($socket);
|
||||
$this->disconnect($socket);
|
||||
|
||||
// message length (10 bytes) is prepended to actual message
|
||||
$xml = substr($response, 10);
|
||||
|
||||
if (true === $this->config['debug']) {
|
||||
$this->logger->info('Response received', [
|
||||
'id' => $requestId,
|
||||
'response' => $xml,
|
||||
]);
|
||||
|
||||
$xml = $response->getContent();
|
||||
|
||||
if (true === $this->config['debug']) {
|
||||
$this->logger->info('Response received', [
|
||||
'id' => $requestId,
|
||||
'response' => $xml,
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->responseParser->parseXmlString($type, $xml);
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
|
||||
$this->logger->error('API error', ['error' => $e->getMessage()]);
|
||||
throw new ApiClientException($e->getMessage());
|
||||
return $this->responseParser->parseXmlString($type, $xml);
|
||||
}
|
||||
|
||||
private function createKey(string $username, string $password, string $type): string
|
||||
@@ -185,8 +187,14 @@ class ApiClient
|
||||
private function resolveOptions(array $options): array
|
||||
{
|
||||
$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([
|
||||
'max_retries' => 25,
|
||||
'debug' => false,
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user