Skip to content

Commit

Permalink
Throw InvalidResponseException if the response from myDATA API is empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
firebed committed Oct 12, 2024
1 parent d0cb356 commit 4226120
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/InvalidResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Firebed\AadeMyData\Exceptions;

class InvalidResponseException extends MyDataException
{
public function __construct($message = "Invalid response received from AADE MyData API", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
3 changes: 1 addition & 2 deletions src/Http/CancelInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function handle(string $mark, string $entityVatNumber = null): ResponseDo
}

// Get the response XML
$results = $this->post($query);
$responseXML = $results->getBody()->getContents();
$responseXML = $this->post($query);

// Parse the response XML
$reader = new ResponseDocReader();
Expand Down
9 changes: 4 additions & 5 deletions src/Http/MyDataGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ public function handle(string $mark = '', string $dateFrom = null, string $dateT

$params = compact('dateFrom', 'dateTo', 'receiverVatNumber', 'entityVatNumber', 'invType', 'maxMark', 'nextPartitionKey', 'nextRowKey');

// Συγχώνευση ερωτήματος με παραμέτρους
// Merge query with params
$query = array_merge($query, array_filter($params));


// Get the response XML / Ανάκτηση του XML απόκρισης
$responseXML = $this->get($query)->getBody()->getContents();

// Get the response XML
$responseXML = $this->get($query);

// Parse the response XML
$reader = new RequestedDocReader();
Expand Down
30 changes: 23 additions & 7 deletions src/Http/MyDataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Firebed\AadeMyData\Http;

use Firebed\AadeMyData\Exceptions\InvalidResponseException;
use Firebed\AadeMyData\Exceptions\MyDataAuthenticationException;
use Firebed\AadeMyData\Exceptions\MyDataConnectionException;
use Firebed\AadeMyData\Exceptions\MyDataException;
Expand All @@ -11,7 +12,6 @@
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\ResponseInterface;
use ReflectionClass;

abstract class MyDataRequest
Expand Down Expand Up @@ -140,12 +140,20 @@ private static function validateCredentials(): void
/**
* @throws MyDataAuthenticationException|MyDataException
*/
protected function get(array $query): ResponseInterface
protected function get(array $query): string
{
self::validateCredentials();

try {
return $this->client()->get($this->getUrl(), ['query' => $query]);
$response = $this->client()->get($this->getUrl(), ['query' => $query]);
$responseXml = $response->getBody()->getContents();

// We always expect a response xml from myDATA
if (empty(trim($responseXml))) {
throw new InvalidResponseException("Empty response received from AADE MyData API");
}

return $responseXml;
} catch (GuzzleException $e) {
$this->handleTransmissionException($e);
}
Expand All @@ -154,7 +162,7 @@ protected function get(array $query): ResponseInterface
/**
* @throws MyDataAuthenticationException|MyDataException
*/
protected function post(array $query = null, string $body = null): ResponseInterface
protected function post(array $query = null, string $body = null): string
{
self::validateCredentials();

Expand All @@ -168,7 +176,15 @@ protected function post(array $query = null, string $body = null): ResponseInter
}

try {
return $this->client()->post($this->getUrl(), $params);
$response = $this->client()->post($this->getUrl(), $params);
$responseXml = $response->getBody()->getContents();

// We always expect a response xml from myDATA
if (empty(trim($responseXml))) {
throw new InvalidResponseException("Empty response received from AADE MyData API");
}

return $responseXml;
} catch (GuzzleException $e) {
$this->handleTransmissionException($e);
}
Expand All @@ -177,7 +193,7 @@ protected function post(array $query = null, string $body = null): ResponseInter
/**
* Authorization errors, bad request, communication errors,
* myDATA server errors, rate limits, connection timeout, etc.
*
*
* @throws MyDataAuthenticationException|MyDataException
*/
protected function handleTransmissionException(GuzzleException $exception)
Expand All @@ -190,7 +206,7 @@ protected function handleTransmissionException(GuzzleException $exception)
if ($exception->getCode() === 0) {
throw new MyDataConnectionException($exception->getCode(), $exception);
}

// Rate limit exception
if ($exception->getCode() === 429) {
throw new RateLimitExceededException($exception->getMessage(), $exception->getCode(), $exception);
Expand Down
6 changes: 4 additions & 2 deletions src/Http/MyDataXmlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ protected function request(XMLWriter $writer, XMLReader $reader, mixed $data): R
$this->requestDom = $writer->getDomDocument();

// Get the response XML
$response = $this->post(body: $requestXML);
$responseXML = $response->getBody()->getContents();
$responseXML = $this->post(body: $requestXML);

// Parse the response XML
$responseDoc = $reader->parseXML($responseXML);

$this->responseDom = $reader->getDomDocument();

return $responseDoc;
Expand Down
3 changes: 1 addition & 2 deletions src/Http/RequestBookInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function handle(string $dateFrom, string $dateTo, string $counterVatNumbe
$query = array_filter($query);

// Get the response XML
$response = $this->get($query);
$responseXML = $response->getBody()->getContents();
$responseXML = $this->get($query);

// Parse the response XML
$reader = new BookInfoReader();
Expand Down
3 changes: 1 addition & 2 deletions src/Http/RequestVatInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function handle(string $dateFrom, string $dateTo, string $entityVatNumber
], fn($value) => $value !== null);

// Get the response XML
$response = $this->get($query);
$responseXML = $response->getBody()->getContents();
$responseXML = $this->get($query);

// Parse the response XML
$reader = new VatInfoReader();
Expand Down

0 comments on commit 4226120

Please sign in to comment.