invalid response improvements
Showing
2 changed files
with
49 additions
and
8 deletions
| ... | @@ -4,16 +4,15 @@ namespace fehrlich\ImmoScoutAPI; | ... | @@ -4,16 +4,15 @@ namespace fehrlich\ImmoScoutAPI; |
| 4 | 4 | ||
| 5 | use function GuzzleHttp\json_decode; | 5 | use function GuzzleHttp\json_decode; |
| 6 | use function GuzzleHttp\json_encode; | 6 | use function GuzzleHttp\json_encode; |
| 7 | use fehrlich\ScoutAPI\exceptions\AuthException; | 7 | use fehrlich\ImmoScoutAPI\exceptions\AuthException; |
| 8 | use fehrlich\ScoutAPI\exceptions\InvalidResponse; | 8 | use fehrlich\ImmoScoutAPI\exceptions\InvalidResponse; |
| 9 | use fehrlich\ScoutAPI\exceptions\InvalidTokenException; | 9 | use fehrlich\ImmoScoutAPI\exceptions\InvalidTokenException; |
| 10 | use GuzzleHttp\Client; | 10 | use GuzzleHttp\Client; |
| 11 | use GuzzleHttp\Command\Guzzle\Description; | 11 | use GuzzleHttp\Command\Guzzle\Description; |
| 12 | use GuzzleHttp\Command\Guzzle\GuzzleClient; | 12 | use GuzzleHttp\Command\Guzzle\GuzzleClient; |
| 13 | use GuzzleHttp\Exception\ClientException; | 13 | use GuzzleHttp\Exception\ClientException; |
| 14 | use GuzzleHttp\HandlerStack; | 14 | use GuzzleHttp\HandlerStack; |
| 15 | use GuzzleHttp\Subscriber\Oauth\Oauth1; | 15 | use GuzzleHttp\Subscriber\Oauth\Oauth1; |
| 16 | use InvalidArgumentException; | ||
| 17 | use Psr\Http\Message\ResponseInterface; | 16 | use Psr\Http\Message\ResponseInterface; |
| 18 | 17 | ||
| 19 | /** | 18 | /** |
| ... | @@ -377,7 +376,16 @@ abstract class ImmoScoutAPI extends GuzzleClient | ... | @@ -377,7 +376,16 @@ abstract class ImmoScoutAPI extends GuzzleClient |
| 377 | 376 | ||
| 378 | if (isset($json['common.messages']) && is_array($json['common.messages'])) { | 377 | if (isset($json['common.messages']) && is_array($json['common.messages'])) { |
| 379 | foreach ($json['common.messages'] as $message) { | 378 | foreach ($json['common.messages'] as $message) { |
| 380 | if (isset($message['message']) && isset($message['message']['messageCode'])) { | 379 | if (isset($message['message']) && isset($message['message'][0])) { |
| 380 | foreach ($message['message'] as $msg) { | ||
| 381 | if (isset($msg['messageCode'])) { | ||
| 382 | if (!isset($return[$msg['messageCode']])) { | ||
| 383 | $return[$msg['messageCode']] = []; | ||
| 384 | } | ||
| 385 | $return[$msg['messageCode']][] = $msg['message']; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } elseif (isset($message['message']) && isset($message['message']['messageCode'])) { | ||
| 381 | if (!isset($return[$message['message']['messageCode']])) { | 389 | if (!isset($return[$message['message']['messageCode']])) { |
| 382 | $return[$message['message']['messageCode']] = []; | 390 | $return[$message['message']['messageCode']] = []; |
| 383 | } | 391 | } |
| ... | @@ -387,15 +395,23 @@ abstract class ImmoScoutAPI extends GuzzleClient | ... | @@ -387,15 +395,23 @@ abstract class ImmoScoutAPI extends GuzzleClient |
| 387 | } | 395 | } |
| 388 | 396 | ||
| 389 | $parsedErrors = []; | 397 | $parsedErrors = []; |
| 398 | |||
| 390 | foreach ($return as $errorCode => $msgArr) { | 399 | foreach ($return as $errorCode => $msgArr) { |
| 391 | foreach ($msgArr as $msg) { | 400 | foreach ($msgArr as $msg) { |
| 392 | $add = null; | 401 | $add = null; |
| 393 | switch ($errorCode) { | 402 | switch ($errorCode) { |
| 394 | case 'ERROR_RESOURCE_VALIDATION': | 403 | case 'ERROR_RESOURCE_VALIDATION': |
| 395 | preg_match('/MESSAGE: (.*?) :/', $msg, $matches); | 404 | preg_match('/MESSAGE: (.*?) :/', $msg, $matches); |
| 396 | |||
| 397 | if (isset($matches[1])) { | 405 | if (isset($matches[1])) { |
| 398 | $add = $matches[1]; | 406 | $add = $matches[1]; |
| 407 | } else { | ||
| 408 | preg_match('/MESSAGE: (.*?)\]/', $msg, $matches); | ||
| 409 | if (isset($matches[1])) { | ||
| 410 | $add = $matches[1]; | ||
| 411 | if ($matches[1] == 'RealEstate already exists for external id.') { | ||
| 412 | $parsedErrors['ERROR_RESOURCE_DUPLICATE'] = true; | ||
| 413 | } | ||
| 414 | } | ||
| 399 | } | 415 | } |
| 400 | break; | 416 | break; |
| 401 | case 'MESSAGE_RESOURCE_CREATED': | 417 | case 'MESSAGE_RESOURCE_CREATED': |
| ... | @@ -480,7 +496,7 @@ abstract class ImmoScoutAPI extends GuzzleClient | ... | @@ -480,7 +496,7 @@ abstract class ImmoScoutAPI extends GuzzleClient |
| 480 | $msgs = $this->parseMessages($res); | 496 | $msgs = $this->parseMessages($res); |
| 481 | if (!isset($msgs['parsed'][$expectedResponse])) { | 497 | if (!isset($msgs['parsed'][$expectedResponse])) { |
| 482 | $code = isset($msgs['parsed']['ERROR_RESOURCE_NOT_FOUND']) ? 404 : null; | 498 | $code = isset($msgs['parsed']['ERROR_RESOURCE_NOT_FOUND']) ? 404 : null; |
| 483 | throw new InvalidResponse('Did not get expected response: '.$res->getBody(), $code); | 499 | throw new InvalidResponse('Did not get expected response: '.$res->getBody(), $code, null, $res, $msgs); |
| 484 | } | 500 | } |
| 485 | if ($expectedResponse == 'MESSAGE_RESOURCE_CREATED') { | 501 | if ($expectedResponse == 'MESSAGE_RESOURCE_CREATED') { |
| 486 | return $msgs['parsed'][$expectedResponse][0]; | 502 | return $msgs['parsed'][$expectedResponse][0]; |
| ... | @@ -506,7 +522,11 @@ abstract class ImmoScoutAPI extends GuzzleClient | ... | @@ -506,7 +522,11 @@ abstract class ImmoScoutAPI extends GuzzleClient |
| 506 | 522 | ||
| 507 | private function parseGetResponse($res) | 523 | private function parseGetResponse($res) |
| 508 | { | 524 | { |
| 525 | try { | ||
| 509 | return json_decode((string) $res->getBody(), true); | 526 | return json_decode((string) $res->getBody(), true); |
| 527 | } catch (Exception $ex) { | ||
| 528 | throw new InvalidResponse('invalid json response'); | ||
| 529 | } | ||
| 510 | } | 530 | } |
| 511 | 531 | ||
| 512 | /** | 532 | /** | ... | ... |
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | namespace fehrlich\ScoutAPI\exceptions; | 3 | namespace fehrlich\ImmoScoutAPI\exceptions; |
| 4 | |||
| 5 | use Exception; | ||
| 4 | 6 | ||
| 5 | class InvalidResponse extends Exception | 7 | class InvalidResponse extends Exception |
| 6 | { | 8 | { |
| 9 | private $response; | ||
| 10 | private $msgs; | ||
| 11 | |||
| 12 | public function __construct($msg, $code = 0, $prev = null, $res = null, $msgs = null) | ||
| 13 | { | ||
| 14 | parent::__construct($msg, $code, $prev); | ||
| 15 | $this->msgs = $msgs; | ||
| 16 | $this->response = $res; | ||
| 17 | } | ||
| 18 | |||
| 19 | public function getResponse() | ||
| 20 | { | ||
| 21 | return $this->response; | ||
| 22 | } | ||
| 23 | |||
| 24 | public function getMessages() | ||
| 25 | { | ||
| 26 | return $this->msgs; | ||
| 27 | } | ||
| 7 | } | 28 | } | ... | ... |
-
Please register or sign in to post a comment