diff --git a/src/KasApi/KasApi.php b/src/KasApi/KasApi.php index 2825b88..f32a2c8 100644 --- a/src/KasApi/KasApi.php +++ b/src/KasApi/KasApi.php @@ -94,6 +94,11 @@ class KasApi { */ protected $kasFloodDelay; + /** + * @var int $nextCallTimestamp Timestamp of when the next api call is allowed + */ + protected $nextCallTimestamp = 0; + /** * @return String */ @@ -207,6 +212,9 @@ function __construct($kas_configuration) { */ protected function call($function, $params = array()) { try { + if ($this->kasConfiguration->_autoDelayApiCalls && ($now = time()) < $this->nextCallTimestamp) { + sleep($this->nextCallTimestamp - $now); + } $data = array('KasUser' => $this->kasConfiguration->_username, 'KasAuthType' => $this->kasConfiguration->_authType, 'KasAuthData' => $this->kasConfiguration->_authData, @@ -216,6 +224,7 @@ protected function call($function, $params = array()) { $client = $kasSoapClient->getInstance(); $result = $client->KasApi(json_encode($data)); $this->kasFloodDelay = $result['Response']['KasFloodDelay']; + $this->nextCallTimestamp = time() + (int)$this->kasFloodDelay; return $result['Response']['ReturnInfo']; } catch (SoapFault $fault) { throw new KasApiException('Unable to execute SOAP call '.$function.': '.(isset($fault->faultstring) ? $fault->faultstring : ""), (isset($fault->faultcode) ? $fault->faultcode : ""), (isset($fault->faultstring) ? $fault->faultstring : ""), (isset($fault->faultfactor) ? $fault->faultfactor : ""), (isset($fault->detail) ? $fault->detail : "")); diff --git a/src/KasApi/KasConfiguration.php b/src/KasApi/KasConfiguration.php index a3c5a4a..3f21d59 100644 --- a/src/KasApi/KasConfiguration.php +++ b/src/KasApi/KasConfiguration.php @@ -30,6 +30,13 @@ class KasConfiguration { */ public $_authType = "sha1"; + /** + * Automatic Delay for Api Calls + * + * Manages whether KasApi should use sleep to automagically manage kasFloodDelay + */ + public $_autoDelayApiCalls; + /** * WSDL file for KAS API * @@ -43,10 +50,12 @@ class KasConfiguration { * @param string $username * @param string $authData * @param string $authType + * @param bool $autoDelayApiCalls */ - function __construct($username, $authData, $authType) { + function __construct($username, $authData, $authType, $autoDelayApiCalls = false) { $this->_username = $username; $this->_authData = $authData; $this->_authType = $authType; + $this->_autoDelayApiCalls = $autoDelayApiCalls; } } \ No newline at end of file