Skip to content

Commit

Permalink
Merge pull request repat#8 from hepisec/master
Browse files Browse the repository at this point in the history
Handle rate limiting of PlentyMarkets REST API automatically
  • Loading branch information
repat authored Feb 21, 2019
2 parents a2204ab + c31130a commit 8bbaadc
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/PlentymarketsRestClient/PlentymarketsRestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Stringy\Stringy as s;

class PlentymarketsRestClient {
Expand All @@ -13,10 +14,15 @@ class PlentymarketsRestClient {
const METHOD_POST = "POST";
const METHOD_PUT = "PUT";
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";

private $client;
private $config;
private $configFile;
private $rateLimitingEnabled = true;
private $throttledOnLastRequest = false;

public function __construct($configFile, $config) {
$this->client = new Client();
Expand All @@ -34,6 +40,19 @@ public function __construct($configFile, $config) {
}
}

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

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

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

public function singleCall($method, $path, $params = []) {

$path = ltrim($path, "/");
Expand All @@ -47,11 +66,17 @@ public function singleCall($method, $path, $params = []) {
}

try {
/* @var $response ResponseInterface */
$response = $this->client->request($method, $this->config["url"] . $path, $params);

} catch (\Exception $e) {
return false;
}

$this->throttledOnLastRequest = false;

if ($this->rateLimitingEnabled) {
$this->handleRateLimiting($response);
}

return json_decode($response->getBody(), true);
}
Expand Down Expand Up @@ -138,4 +163,35 @@ private function setConfigFile($configFile) {

$this->saveConfigFile();
}

private function handleRateLimiting(ResponseInterface $response) {
$prefixes = array(
self::THROTTLING_PREFIX_LONG_PERIOD,
self::THROTTLING_PREFIX_SHORT_PERIOD,
self::THROTTLING_PREFIX_ROUTE
);

$throttled = 0;

foreach ($prefixes as $prefix) {
$throttled += $this->handleThrottling($response, $prefix, $throttled);
}
}

private function handleThrottling(ResponseInterface $response, $prefix, $throttled = 0) {
$callsLeft = $response->getHeader($prefix . '-Calls-Left');
$decay = $response->getHeader($prefix . '-Decay');

if (count($callsLeft) < 1 || count($decay) < 1) {
return 0;
}

if ($callsLeft[0] < 1 && $decay[0] > $throttled) {
sleep($decay[0] - $throttled);
$this->throttledOnLastRequest = true;
return $decay[0];
}

return 0;
}
}

0 comments on commit 8bbaadc

Please sign in to comment.