Skip to content

Commit 00b5518

Browse files
authored
Merge pull request #174 from Insti/Timeout_Improvements
Obvious timeout error message.
2 parents bd53053 + ff11c8a commit 00b5518

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/MailChimp.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class MailChimp
1515
private $api_key;
1616
private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0';
1717

18+
const TIMEOUT = 10;
19+
1820
/* SSL Verification
1921
Read before disabling:
2022
http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
@@ -113,7 +115,7 @@ public function getLastRequest()
113115
* @param int $timeout Timeout limit for request in seconds
114116
* @return array|false Assoc array of API response, decoded from JSON
115117
*/
116-
public function delete($method, $args = array(), $timeout = 10)
118+
public function delete($method, $args = array(), $timeout = self::TIMEOUT)
117119
{
118120
return $this->makeRequest('delete', $method, $args, $timeout);
119121
}
@@ -125,7 +127,7 @@ public function delete($method, $args = array(), $timeout = 10)
125127
* @param int $timeout Timeout limit for request in seconds
126128
* @return array|false Assoc array of API response, decoded from JSON
127129
*/
128-
public function get($method, $args = array(), $timeout = 10)
130+
public function get($method, $args = array(), $timeout = self::TIMEOUT)
129131
{
130132
return $this->makeRequest('get', $method, $args, $timeout);
131133
}
@@ -137,7 +139,7 @@ public function get($method, $args = array(), $timeout = 10)
137139
* @param int $timeout Timeout limit for request in seconds
138140
* @return array|false Assoc array of API response, decoded from JSON
139141
*/
140-
public function patch($method, $args = array(), $timeout = 10)
142+
public function patch($method, $args = array(), $timeout = self::TIMEOUT)
141143
{
142144
return $this->makeRequest('patch', $method, $args, $timeout);
143145
}
@@ -149,7 +151,7 @@ public function patch($method, $args = array(), $timeout = 10)
149151
* @param int $timeout Timeout limit for request in seconds
150152
* @return array|false Assoc array of API response, decoded from JSON
151153
*/
152-
public function post($method, $args = array(), $timeout = 10)
154+
public function post($method, $args = array(), $timeout = self::TIMEOUT)
153155
{
154156
return $this->makeRequest('post', $method, $args, $timeout);
155157
}
@@ -161,7 +163,7 @@ public function post($method, $args = array(), $timeout = 10)
161163
* @param int $timeout Timeout limit for request in seconds
162164
* @return array|false Assoc array of API response, decoded from JSON
163165
*/
164-
public function put($method, $args = array(), $timeout = 10)
166+
public function put($method, $args = array(), $timeout = self::TIMEOUT)
165167
{
166168
return $this->makeRequest('put', $method, $args, $timeout);
167169
}
@@ -175,7 +177,7 @@ public function put($method, $args = array(), $timeout = 10)
175177
* @return array|false Assoc array of decoded result
176178
* @throws \Exception
177179
*/
178-
private function makeRequest($http_verb, $method, $args = array(), $timeout = 10)
180+
private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT)
179181
{
180182
if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
181183
throw new \Exception("cURL support is required, but can't be found.");
@@ -245,10 +247,10 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = 10
245247

246248
$responseContent = curl_exec($ch);
247249

250+
$response['headers'] = curl_getinfo($ch);
248251
if ($responseContent === false) {
249252
$this->last_error = curl_error($ch);
250253
} else {
251-
$response['headers'] = curl_getinfo($ch);
252254
$headerSize = $response['headers']['header_size'];
253255

254256
$response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize));
@@ -263,7 +265,7 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = 10
263265

264266
$formattedResponse = $this->formatResponse($response);
265267

266-
$this->determineSuccess($response, $formattedResponse);
268+
$this->determineSuccess($response, $formattedResponse, $timeout);
267269

268270
return $formattedResponse;
269271
}
@@ -371,9 +373,10 @@ private function formatResponse($response)
371373
* Check if the response was successful or a failure. If it failed, store the error.
372374
* @param array $response The response from the curl request
373375
* @param array|false $formattedResponse The response body payload from the curl request
376+
* @param int $timeout The timeout supplied to the curl request.
374377
* @return bool If the request was successful
375378
*/
376-
private function determineSuccess($response, $formattedResponse)
379+
private function determineSuccess($response, $formattedResponse, $timeout)
377380
{
378381
$status = $this->findHTTPStatus($response, $formattedResponse);
379382

@@ -387,6 +390,11 @@ private function determineSuccess($response, $formattedResponse)
387390
return false;
388391
}
389392

393+
if( $timeout > 0 && $response['headers'] && $response['headers']['total_time'] >= $timeout ) {
394+
$this->last_error = sprintf('Request timed out after %f seconds.', $response['headers']['total_time'] );
395+
return false;
396+
}
397+
390398
$this->last_error = 'Unknown error, call getLastResponse() to find out what happened.';
391399
return false;
392400
}

tests/MailChimpTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,29 @@ public function testResponseState()
7373
$this->assertTrue($MailChimp->success());
7474
}
7575

76-
}
76+
/* This test requires that your test list have:
77+
* a) a list
78+
* b) enough entries that the curl request will timeout after 1 second.
79+
* How many this is may depend on your network connection to the Mailchimp servers.
80+
*/
81+
public function testRequestTimeout()
82+
{
83+
$MC_API_KEY = getenv('MC_API_KEY');
84+
85+
if (!$MC_API_KEY) {
86+
$this->markTestSkipped('No API key in ENV');
87+
}
88+
89+
$MailChimp = new MailChimp($MC_API_KEY);
90+
$result = $MailChimp->get('lists');
91+
$list_id = $result['lists'][0]['id'];
92+
93+
$args = array( 'count' => 1000 );
94+
$timeout = 1;
95+
$result = $MailChimp->get("lists/$list_id/members", $args, $timeout );
96+
$this->assertFalse( $result );
97+
98+
$error = $MailChimp->getLastError();
99+
$this->assertRegExp( '/Request timed out after 1.\d+ seconds/', $error );
100+
}
101+
}

0 commit comments

Comments
 (0)