Skip to content

Commit

Permalink
Handle Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Aug 26, 2020
1 parent 195c484 commit 515bff2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion 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.9",
"version" : "0.1.10",
"authors": [
{
"name": "repat",
Expand Down
36 changes: 23 additions & 13 deletions src/PlentymarketsRestClient/PlentymarketsRestClient.php
Expand Up @@ -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();
}
}
Expand All @@ -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'],
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand All @@ -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)');
}

Expand Down

0 comments on commit 515bff2

Please sign in to comment.