Skip to content

Commit

Permalink
Merge branch 'throw-exception-on-throttle'
Browse files Browse the repository at this point in the history
  • Loading branch information
najamhaq committed Aug 28, 2019
2 parents 83e5dcb + 62b8ecf commit c5759ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cpigroup/php-amazon-mws",
"name": "najamhaq/php-amazon-mws",
"type": "library",
"description": "An open-source library to connect to Amazon's MWS web services in an object-oriented manner, with a focus on intuitive usage.",
"license": "Apache-2.0",
Expand Down
51 changes: 45 additions & 6 deletions includes/classes/AmazonCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ abstract class AmazonCore{
protected $env;
protected $rawResponses = array();
protected $disableSslVerify = false;
protected $headers = [];
protected $proxy;

/**
Expand Down Expand Up @@ -186,6 +187,9 @@ public function setMock($b = true,$files = null){
/**
* set Proxy server to be used for all Amazon calls.
* @param $proxy
* set Proxy address and auth
* should be of the form : tcp://user:pass@ip-address:port
* or : http://user:pass@ip-address:port
*/
public function setProxy($proxy){
$this->proxy = $proxy;
Expand Down Expand Up @@ -464,6 +468,15 @@ public function setStore($s=null){
public function setThrottleStop($b=true) {
$this->throttleStop=!empty($b);
}

/**
* For now returns the header saved at throttling.
* @return array
*/
public function getThrottleDetails()
{
return $this->headers;
}

/**
* Writes a message to the log.
Expand Down Expand Up @@ -616,25 +629,45 @@ protected function genQuery(){
* @param string $url <p>URL to feed to cURL</p>
* @param array $param <p>parameter array to feed to cURL</p>
* @return array cURL response array
* @throws Exception
*/
protected function sendRequest($url,$param){
$this->log("Making request to Amazon: ".$this->options['Action']);
$response = $this->fetchURL($url,$param);
$this->headers = $response['head'];

if ( $response['ok']) {
if ($response['ok']) {
$this->rawResponses[] = $response;
return $response;
}

if ( ! isset($response['code'])) {
// we dont know what went wrong
$this->rawResponses[] = $response;
return $response;
}
if ($response['code'] != 503) {
// we have some other problem ,
$this->rawResponses[] = $response;
return $response;
}

/**
* At this point we know the call is throttled and we SHOULD raise an exception when that happens
*/
if ($this->throttleStop) {
throw new Exception("Api Call Throttled.", $response);
$this->log("Api Call Throttled.: ".$this->options['Action']);
throw new Exception('Api Call Throttled.');
}

while (isset($response['code']) && $response['code'] == '503'){
/**
* At this point we know the call is throttled and we MUST continute calling after designated sleep time
*/
while (isset($response['code']) && $response['code'] == '503') {
$this->sleep();
$response = $this->fetchURL($url,$param);
$response = $this->fetchURL($url, $param);
}

$this->rawResponses[]=$response;
return $response;
}
Expand Down Expand Up @@ -828,10 +861,16 @@ function fetchURL ($url, $param) {
$return['error'] = curl_error($ch);
return $return;
}

if (is_numeric(strpos($data, 'HTTP/1.1 100 Continue'))) {
$data=str_replace('HTTP/1.1 100 Continue', '', $data);
}
if ($this->proxy ) {
// this needs to be more resilient for other http version
if (is_numeric(strpos($data, 'HTTP/1.1 200 Connection established'))) {
$data = str_replace('HTTP/1.1 200 Connection established', '', $data);
}
}
$data = preg_split("/\r\n\r\n/",$data, 2, PREG_SPLIT_NO_EMPTY);
if (!empty($data)) {
$return['head'] = ( isset($data[0]) ? $data[0] : null );
Expand Down
4 changes: 4 additions & 0 deletions includes/classes/AmazonFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ protected function parseXML($xml){
*/
protected function genHeader(){
$return[0] = "Content-MD5:".$this->feedMD5;
$return[1] = 'Content-Type: text/xml';
if (strpos($this->options['FeedType'], "_FLAT_FILE_") !== false){
$return[1] = 'Content-Type: text/tab-separated-values';
}
return $return;
}

Expand Down
12 changes: 8 additions & 4 deletions includes/classes/AmazonProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,19 @@ public function loadXML($xml){
}
}
} else {
$this->data['AttributeSets'][$anum][$x->getName()][$y->getName()] = (string)$y;
$nodeValue = (string) $y;
if($x->getName() === 'ItemDimensions' || $x->getName() === 'PackageDimensions'){
$nodeValue .= ' ' . (string) $y->attributes()['Units'];
}
$this->data['AttributeSets'][$anum][$x->getName()][$y->getName()] = $nodeValue;
}
}

} else {
//Check for duplicates
if (array_key_exists('AttributeSets', $this->data) &&
array_key_exists($anum, $this->data['AttributeSets']) &&
array_key_exists($x->getName(), $this->data['AttributeSets'][$anum])){
if (array_key_exists('AttributeSets', $this->data) &&
array_key_exists($anum, $this->data['AttributeSets']) &&
array_key_exists($x->getName(), $this->data['AttributeSets'][$anum])){

//check for previous cases of duplicates
if (is_array($this->data['AttributeSets'][$anum][$x->getName()])){
Expand Down

0 comments on commit c5759ff

Please sign in to comment.