Skip to content

Commit

Permalink
Enable / Disable json_decode
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Jun 1, 2022
1 parent 4d0ff2b commit a717aba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
19 changes: 14 additions & 5 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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);
```

Expand All @@ -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

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
49 changes: 28 additions & 21 deletions src/PlentymarketsRestClient/PlentymarketsRestClient.php
Expand Up @@ -27,19 +27,21 @@ 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;
private $configFile;
private $rateLimitingEnabled = true;
private $throttledOnLastRequest = false;
private $handleExceptions = false;
private $jsonEncodeEnabled = true;
private $jsonDecodeEnabled = true;

public function __construct($configFile, $config = null, $handleExceptions = false)
{
Expand All @@ -63,38 +65,42 @@ public function __construct($configFile, $config = null, $handleExceptions = fal
$this->login();
}
}


public function getThrottledOnLastRequest()
{
return $this->throttledOnLastRequest;
}

public function getRateLimitingEnabled()
{
return $this->rateLimitingEnabled;
}

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, '/');
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit a717aba

Please sign in to comment.