diff --git a/README.md b/README.md index e05b0bd..e841910 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ You can find the Plentymarkets documentation [here](https://developers.plentymar * Functions for the 4 HTTP verbs: GET, POST, PUT, DELETE * Automatic login and refresh if login is not valid anymore * Simple one-time configuration with PHP array (will be saved serialized in a file) -* Functions return an associative array +* Functions return an associative array by default or raw data (e.g. for files) * Handle rate limiting (thanks [hepisec](http://github.com/hepisec)) ## Installation @@ -44,7 +44,7 @@ $handleExceptions = PlentymarketsRestClient::DONT_HANDLE_EXCEPTIONS; // false (d // Init $client = new PlentymarketsRestClient($configFilePath, $config, $handleExceptions); -// After that just use it like this: +// After that just use it like so $client = new PlentymarketsRestClient($configFilePath); ``` @@ -68,17 +68,25 @@ $path = "rest/orders/"; ``` It's also possible to use the function like this. It gives you more freedom, since -you can specify the method and the $parameters given are directly given to the [guzzle +you can specify the method and the $parameters given are directly given to the [Guzzle object](http://docs.guzzlephp.org/en/latest/quickstart.html). ```php $client->singleCall("GET", $guzzleParameterArray); ``` +### Raw Data + +Some endpoints, such as `/rest/bi/raw-data/file` don't return JSON but a raw file. However, by default a single call tries to [`json_decode()`](https://www.php.net/manual/en/function.json-decode.php) the response. You can disable it by setting the `$jsonDecodeEnabled` flag to `false`. + +```php +$client->setJsonDecodeEnabled(PlentymarketsRestClient::JSON_DECODE_DISABLED); +``` + ### Errors -* If there was an error with the call (=> guzzle throws an exception) all functions will return false -* If the specified config file doesn't exist or doesn't include username/password/url, an exception will be thrown +* If there was an error with the call (=> guzzle throws an exception) all methods will return false, unless `$handleExceptions` has been set to `true`, in which case the exception will be handed through +* If the specified config file doesn't exist or doesn't include username / password / url, an exception will be thrown ## TODO @@ -95,6 +103,7 @@ $client->singleCall("GET", $guzzleParameterArray); ## Changelog +* 0.1.16 Enable / Disable `json_decode()` to allow for querying for raw files (thx dark-cms) * 0.1.15 Enable returning PDFs (thx ewaldmedia) * 0.1.14 Add _short period write limit reached_ error handling (thx resslinger) * 0.1.13 Change from [Laravels `str_contains`](https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Str.php#L181) to [PHPs `stripos()`](https://www.php.net/manual/de/function.stripos.php) because [it doesn't require `ext-mbstring`](https://github.com/repat/plentymarkets-rest-client/pull/16#issuecomment-880731813) (thx DanMan) diff --git a/composer.json b/composer.json index c75e372..8b7bb68 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.15", + "version" : "0.1.16", "authors": [ { "name": "repat", @@ -14,7 +14,7 @@ } ], "require": { - "php": "^5.6 | ^7.0 | ^8.0", + "php": "^5.6 | ^7.0 | ^8.0 | ^8.1", "guzzlehttp/guzzle": "^6.0 | ^7.0", "nesbot/carbon": "^1.22.1 | ^2.0" }, diff --git a/src/PlentymarketsRestClient/PlentymarketsRestClient.php b/src/PlentymarketsRestClient/PlentymarketsRestClient.php index 1826f56..7df1e5b 100644 --- a/src/PlentymarketsRestClient/PlentymarketsRestClient.php +++ b/src/PlentymarketsRestClient/PlentymarketsRestClient.php @@ -27,11 +27,13 @@ class PlentymarketsRestClient const JSON_DECODE_ENABLED = true; const JSON_DECODE_DISABLED = false; - + + const RATE_LIMITING_ENABLED = true; + const RATE_LIMITING_DISABLED = false; + const NO_CONFIG = null; const HANDLE_EXCEPTIONS = true; const DONT_HANDLE_EXCEPTIONS = false; - private $client; private $config; @@ -39,7 +41,7 @@ class PlentymarketsRestClient private $rateLimitingEnabled = true; private $throttledOnLastRequest = false; private $handleExceptions = false; - private $jsonEncodeEnabled = true; + private $jsonDecodeEnabled = true; public function __construct($configFile, $config = null, $handleExceptions = false) { @@ -63,7 +65,12 @@ public function __construct($configFile, $config = null, $handleExceptions = fal $this->login(); } } - + + public function getThrottledOnLastRequest() + { + return $this->throttledOnLastRequest; + } + public function getRateLimitingEnabled() { return $this->rateLimitingEnabled; @@ -71,30 +78,29 @@ public function getRateLimitingEnabled() public function setRateLimitingEnabled($rateLimitingEnabled) { + if (! is_bool($rateLimitingEnabled)) { + throw new \Exception('rateLimitingEnabled must be a boolean, ' . gettype($rateLimitingEnabled) . ' given'); + } + $this->rateLimitingEnabled = $rateLimitingEnabled; return $this; } - public function getThrottledOnLastRequest() - { - return $this->throttledOnLastRequest; - } - - public function enableJsonDecode() - { - $this->jsonEncodeEnabled = self::JSON_DECODE_ENABLED; - return $this; - } - public function disableJsonDecode() + public function getJsonDecodeEnabled() { - $this->jsonEncodeEnabled = self::JSON_DECODE_DISABLED; - return $this; + return $this->jsonDecodeEnabled; } - public function setJsonDecodeDisabled() + + public function setJsonDecodeEnabled($jsonDecodeEnabled) { - $this->jsonEncodeEnabled = self::JSON_DECODE_DISABLED; + if (! is_bool($jsonDecodeEnabled)) { + throw new \Exception('jsonDecodeEnabled must be a boolean, ' . gettype($jsonDecodeEnabled) . ' given'); + } + + $this->jsonDecodeEnabled = $jsonDecodeEnabled; return $this; } + public function singleCall($method, $path, $params = []) { $path = ltrim($path, '/'); @@ -143,10 +149,11 @@ public function singleCall($method, $path, $params = []) && $headers['Content-Type'][0] === 'application/pdf') { return $response->getBody()->getContents(); } - if($this->jsonEncodeEnabled === self::JSON_DECODE_DISABLED) - { + + if (! $this->jsonDecodeEnabled) { return $response->getBody(); } + return json_decode($response->getBody(), true); }