/** * Determines if the last request was a success (1** - 2** code). * * @return boolean */ protected function wasSuccess() { if (!isset($this->lastResponse)) { $this->handleError("Request has not been sent to the API"); return false; } return $this->lastResponse->isSuccessful(); }
/** * Retrieve latitude and longitude from the API response * * @param Zend_Http_Response|boolean $response * @return array|boolean */ protected function _parseResponse($response) { if ($response->isSuccessful() && $response->getStatus() == 200) { $_response = json_decode($response->getBody()); $_coordinates = $_response->results[0]->geometry->location; $geo = array('lat' => $_coordinates->lat, 'lng' => $_coordinates->lng); return $geo; } return false; }
public function processResponse(Zend_Http_Response $response) { $serviceName = $this->getRestService()->getServiceName(); if (!$serviceName) { throw new \Application\Exceptions\UnexpectedException("Service Name doesn't exist"); } $this->checkAlarmTimeSpent($serviceName); if ($response->isSuccessful()) { App::alarm()->notifyInvalidHttpCode($serviceName); } else { App::alarm()->notifyInvalidHttpCode($serviceName, $response->getStatus()); } }
/** * Retrieve latitude and longitude from the API response * * @param Zend_Http_Response|boolean $response * @return array|boolean */ protected function _parseResponse($response) { if ($response->isSuccessful() && $response->getStatus() == 200) { $_response = json_decode($response->getBody()); if (is_array($_response->postalcodes)) { $_response = array_shift($_response->postalcodes); } if ($_response) { $geo = array('lat' => $_response->lat, 'lng' => $_response->lng); } else { $geo = false; } return $geo; } return false; }
/** * Extract the API response data from the given HTTP response object. * * @param Zend_Http_Response $response * * @return $this */ protected function parseRawResponse(Zend_Http_Response $response) { if ($response->isSuccessful()) { $content = $response->getBody(); if (strlen($content) > 0) { try { $xml = simplexml_load_string($response->getBody()); } catch (Exception $e) { // Failed to parse XML $this->successful = false; $this->setMessage("Failed to parse a response from Klevu."); Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Failed to parse XML response: %s", $e->getMessage())); return $this; } $this->xml = $xml; $this->successful = true; } else { // Response contains no content $this->successful = false; $this->setMessage('Failed to parse a response from Klevu.'); Mage::helper('klevu_search')->log(Zend_Log::ERR, "API response content is empty."); } } else { // Unsuccessful HTTP response $this->successful = false; switch ($response->getStatus()) { case 403: $message = "Incorrect API keys."; break; case 500: $message = "API server error."; break; case 503: $message = "API server unavailable."; break; default: $message = "Unexpected error."; } $this->setMessage(sprintf("Failed to connect to Klevu: %s", $message)); Mage::helper('klevu_search')->log(Zend_Log::ERR, sprintf("Unsuccessful HTTP response: %s %s", $response->getStatus(), $response->responseCodeAsText($response->getStatus()))); } return $this; }
/** * Did an error occur in the request? * * @return bool */ public function isError() { return !$this->httpResponse->isSuccessful(); }
/** * Throw an exception if error occured * * @param SimpleXMLElement $xml * @return SimpleXMLElement */ private function parseResponse(\Zend_Http_Response $response) { if (!$response->isSuccessful()) { throw new \RuntimeException("Response not successful."); } $xml = simplexml_load_string($response->getBody()); if ($xml->getName() !== 'newsMessage' && !in_array((int) $xml->status['code'], array(self::STATUS_SUCCESS, self::STATUS_PARTIAL_SUCCESS))) { throw new \RuntimeException((string) $xml->status->error); } return $xml; }
/** * return a string that is a key to the handler config for the class of status codes. * @param Zend_Http_Response $response * @return string */ protected function _getHandlerKey(Zend_Http_Response $response = null) { if ($response) { $code = $response->getStatus(); if ($response->isSuccessful() || $response->isRedirect()) { return 'success'; } elseif ($code < 500 && $code >= 400) { return 'client_error'; } elseif ($code >= 500) { return 'server_error'; } // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd return 'no_response'; }