Skip to content

Commit

Permalink
Merge pull request #11 from rhurling/patch-issue-10
Browse files Browse the repository at this point in the history
Implementing automatic KasFloodDelay management (fixes #10)
  • Loading branch information
waza-ari authored Jul 9, 2019
2 parents f023375 + 7109f56 commit fe983f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/KasApi/KasApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
Expand All @@ -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 : ""));
Expand Down
11 changes: 10 additions & 1 deletion src/KasApi/KasConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
}

0 comments on commit fe983f9

Please sign in to comment.