예제 #1
0
파일: Api.php 프로젝트: recensus/php-sdk
 /**
  * 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();
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 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;
 }
예제 #5
0
 /**
  * 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;
 }
예제 #6
0
 /**
  * Did an error occur in the request?
  *
  * @return bool
  */
 public function isError()
 {
     return !$this->httpResponse->isSuccessful();
 }
예제 #7
0
 /**
  * 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';
 }