diff --git a/composer.json b/composer.json index 386a0f46..53aae30c 100755 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "myparcelnl/sdk", - "version": "v1.1.6", + "version": "v1.1.7", "description": "This package is designed to send and receive data from MyParcel by means of an API.", "homepage": "https://www.myparcel.nl", "tags": ["MyParcel", "My Parcel", "Post NL", "PostNL"], diff --git a/src/AutoLoader.php b/src/AutoLoader.php index 73687d13..20b18e2c 100755 --- a/src/AutoLoader.php +++ b/src/AutoLoader.php @@ -1,6 +1,7 @@ checkConfigForRequest()) { return false; @@ -198,7 +198,6 @@ public function sendRequest($method = 'POST', $uri = self::REQUEST_TYPE_CREATE_C $request->close(); if ($this->getError()) { - throw new \Exception('Error in MyParcel API request: ' . $this->getError() . '. Url: ' . $url . ' Request: ' . $this->body); } return $this; diff --git a/src/Services/CheckApiKeyService.php b/src/Services/CheckApiKeyService.php new file mode 100644 index 00000000..4a9d8e11 --- /dev/null +++ b/src/Services/CheckApiKeyService.php @@ -0,0 +1,67 @@ + + * @copyright 2010-2017 MyParcel + * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL + * @link https://github.com/myparcelnl/sdk + * @since File available since Release v1.1.7 + */ + +namespace MyParcelNL\Sdk\src\Services; + +use MyParcelNL\Sdk\src\Model\MyParcelRequest; + +class CheckApiKeyService +{ + private $api_key; + + /** + * @return mixed + */ + public function getApiKey() + { + return $this->api_key; + } + + /** + * @param mixed $api_key + */ + public function setApiKey($api_key) + { + $this->api_key = $api_key; + + return $this; + } + + public function apiKeyIsCorrect() + { + try { + $request = (new MyParcelRequest()); + + $userAgent = $request->getUserAgentFromComposer(); + $request + ->setUserAgent($userAgent) + ->setRequestParameters( + $this->getApiKey(), + '', + MyParcelRequest::REQUEST_HEADER_RETRIEVE_SHIPMENT + ) + ->sendRequest('GET'); + + if ($request->getResult() === null) { + throw new \Exception('Unable to connect to MyParcel.'); + } + } catch (\Exception $exception) { + if (strpos($exception, 'Access Denied') > 1) { + return false; + } + } + + return true; + } +}