diff --git a/README.md b/README.md index 52f2bca..4162303 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,12 @@ $config = [ "url" => "https://www.plentymarkets-system.tld", ]; -$client = new PlentymarketsRestClient($configFilePath, $config); +// Handle (Guzzle) Exceptions yourself - optional 3rd parameter +$handleExceptions = PlentymarketsRestClient::HANDLE_EXCEPTIONS; // true +$handleExceptions = PlentymarketsRestClient::DONT_HANDLE_EXCEPTIONS; // false (default) + +// Init +$client = new PlentymarketsRestClient($configFilePath, $config, $handleExceptions); // After that just use it like this: $client = new PlentymarketsRestClient($configFilePath); @@ -81,6 +86,7 @@ $client->singleCall("GET", $guzzleParameterArray); * see [LICENSE](https://github.com/repat/plentymarkets-rest-client/blob/master/LICENSE) file ## Changelog +* 0.1.10 Allow dealing with Exceptions yourself by passing `true` as 3rd parameter * 0.1.9 Bugfix `isAccessTokenValid()` (thx [hochdruckspezialist](https://github.com/repat/plentymarkets-rest-client/pull/14)) * 0.1.8 Update, so Carbon 2.0 can be used (thx [stefnats](https://github.com/repat/plentymarkets-rest-client/pull/12)) * 0.1.7 Fix constructor according to README (thx [daniel-mannheimer](https://github.com/repat/plentymarkets-rest-client/pull/11)) diff --git a/composer.json b/composer.json index b8bf194..b4d6d2d 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "keywords": ["plentymarkets", "pm", "rest", "api", "client", "sdk"], "homepage": "https://github.com/repat/plentymarkets-rest-client", "license": "MIT", - "version" : "0.1.9", + "version" : "0.1.10", "authors": [ { "name": "repat", diff --git a/src/PlentymarketsRestClient/PlentymarketsRestClient.php b/src/PlentymarketsRestClient/PlentymarketsRestClient.php index e99e3ec..ac6c4c8 100644 --- a/src/PlentymarketsRestClient/PlentymarketsRestClient.php +++ b/src/PlentymarketsRestClient/PlentymarketsRestClient.php @@ -15,34 +15,41 @@ class PlentymarketsRestClient const METHOD_PUT = 'PUT'; const METHOD_PATCH = 'PATCH'; const METHOD_DELETE = 'DELETE'; - + const THROTTLING_PREFIX_LONG_PERIOD = 'X-Plenty-Global-Long-Period'; const THROTTLING_PREFIX_SHORT_PERIOD = 'X-Plenty-Global-Short-Period'; const THROTTLING_PREFIX_ROUTE = 'X-Plenty-Route'; + const NO_CONFIG = null; + const HANDLE_EXCEPTIONS = true; + const DONT_HANDLE_EXCEPTIONS = false; + private $client; private $config; private $configFile; private $rateLimitingEnabled = true; private $throttledOnLastRequest = false; + private $handleExceptions = false; - public function __construct($configFile, $config = null) + public function __construct($configFile, $config = null, $handleExceptions = false) { + $this->handleExceptions = $handleExceptions; + $this->client = new Client(); if ($config !== null) { $this->config = $config; } else { $this->config = $this->readConfigFile($configFile); } - - if (!file_exists($configFile)) { + + if (! file_exists($configFile)) { $this->configFile = $configFile; $this->saveConfigFile(); } $this->setConfigFile($configFile); - if (!$this->isAccessTokenValid()) { + if (! $this->isAccessTokenValid()) { $this->login(); } } @@ -67,7 +74,7 @@ public function singleCall($method, $path, $params = []) { $path = ltrim($path, '/'); - if (!($path == self::PATH_LOGIN)) { + if (! ($path == self::PATH_LOGIN)) { $params = array_merge($params, [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->config['access_token'], @@ -79,6 +86,9 @@ public function singleCall($method, $path, $params = []) /* @var $response ResponseInterface */ $response = $this->client->request($method, $this->config['url'] . $path, $params); } catch (\Exception $e) { + if ($this->handleExceptions === true) { + throw $e; + } return false; } @@ -118,7 +128,7 @@ public function delete($path, $array = []) private function isAccessTokenValid() { - if (!array_key_exists('valid_until', $this->config)) { + if (! array_key_exists('valid_until', $this->config)) { return false; } return Carbon::parse($this->config['valid_until'])->gt(Carbon::now()); @@ -148,12 +158,12 @@ private function readConfigFile($configFile) { return unserialize(file_get_contents($configFile)); } - + private function correctURL($url) { $sUrl = new s($url); - if (!($sUrl->contains('https'))) { + if (! ($sUrl->contains('https'))) { $url = str_replace('http://', 'https://.', $url); } @@ -166,15 +176,15 @@ private function setConfigFile($configFile) { $this->configFile = $configFile; - if (!file_exists($configFile)) { + if (! file_exists($configFile)) { throw new \Exception('config file does not exists.'); } $this->config = unserialize(file_get_contents($this->configFile)); - if (!array_key_exists('username', $this->config) - || !array_key_exists('password', $this->config) - || !array_key_exists('url', $this->config)) { + if (! array_key_exists('username', $this->config) + || ! array_key_exists('password', $this->config) + || ! array_key_exists('url', $this->config)) { throw new \Exception('username and/or password and/or url not in config(file)'); }