Skip to content

Commit

Permalink
Merge pull request #6 from gmorel/fix/add_ability_to_manage_timeout
Browse files Browse the repository at this point in the history
Fix Timeout management
  • Loading branch information
shouze committed Sep 25, 2014
2 parents eab3a92 + b881320 commit 8fb586f
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions Connection/HttpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HttpConnection implements ConnectionInterface
const INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR = -100;
const INTERNAL_CODE_GENERAL_ERROR = -99;
const INTERNAL_CODE_TIMEOUT = -98;
const INTERNAL_CODE_PARSE_EXCEPTION = 0;
const INTERNAL_CODE_PARSE_EXCEPTION = -101;

const API_URL = 'http://api.mailchimp.com/1.3/?output=php';

Expand Down Expand Up @@ -50,9 +50,10 @@ public function execute(Request $request)
->post($uri)
->addPostFields($request->getParams());

$response = false;
$rawResponse = false;
try {
$rawResponse = $request->send();

$response = $this->parseResponse($rawResponse);
} catch (\Exception $e) {
// unknown exception
Expand All @@ -63,7 +64,7 @@ public function execute(Request $request)
}

if (false === $response) {
$response = $this->handleValidResponse($rawResponse);
$response = $this->handleEdgeCase($rawResponse);
}

if (is_array($response) && isset($response['error'])) {
Expand Down Expand Up @@ -107,16 +108,8 @@ private function getUri($method, $apiKey)
*
* @return array
*/
private function handleValidResponse($rawResponse)
private function handleEdgeCase($rawResponse)
{
if ($this->isReponseTimeout($rawResponse)) {
// timeout exception
return array(
'error' => 'Could not read response (timed out)',
'code' => self::INTERNAL_CODE_TIMEOUT
);
}

// bad response
return array(
'error' => 'Bad Response. Got this: ' . $rawResponse->getBody(),
Expand All @@ -143,12 +136,20 @@ private function isSerialized($data)
*/
private function parseResponse($rawResponse)
{
if (false === $rawResponse ){
if (false === $rawResponse ) {
return false;
}

if ($this->isReponseTimeout($rawResponse)) {
// timeout exception
return array(
'error' => 'Could not read response (timed out)',
'code' => self::INTERNAL_CODE_TIMEOUT
);
}

if ($this->isSerialized($rawResponse->getBody())) {
return unserialize($rawResponse->getBody());
return @unserialize($rawResponse->getBody());
}

return array(
Expand Down

0 comments on commit 8fb586f

Please sign in to comment.