Beispiel #1
0
 /**
  * @param Zend_Http_Response $response
  * @return json string
  * @throws
  */
 public function parseZendResponse(Zend_Http_Response $response)
 {
     if ($response->getStatus() == 200) {
         return $response->getBody();
     } else {
         throw new Exception("Error: Status is: " . $response->getStatus() . " message: " . $response->getMessage());
     }
 }
 /**
  * Sends a request and returns a response
  *
  * @param CartRecover_Request $request
  * @return Cart_Recover_Response
  */
 public function sendRequest(CartRecover_Request $request)
 {
     $this->client->setUri($request->getUri());
     $this->client->setParameterGet($request->getParams());
     $this->client->setMethod($request->getMethod());
     $this->client->setHeaders('Accept', 'application/json');
     $this->response = $this->client->request();
     if ($this->response->getHeader('Content-Type') != 'application/json') {
         throw new CartRecover_Exception_UnexpectedValueException("Unknown response format.");
     }
     $body = json_decode($this->response->getBody(), true);
     $response = new CartRecover_Response();
     $response->setRawResponse($this->response->asString());
     $response->setBody($body);
     $response->setHeaders($this->response->getHeaders());
     $response->setStatus($this->response->getMessage(), $this->response->getStatus());
     return $response;
 }
Beispiel #3
0
 /**
  * (non-PHPdoc)
  * @see Tid_Rest_Processor_Input_Abstract::processInputData()
  */
 public function processInputData(Zend_Http_Response $response = null)
 {
     try {
         return isset($response) ? Zend_Json::decode($response->getBody()) : array();
     } catch (Exception $e) {
         $this->getRestService()->log("Error parsing JSON response: " . $e->getMessage(), Zend_Log::ERR);
         $this->getRestService()->log($e, Zend_Log::ERR);
         throw new App_Rest_Processor_Input_Exception('Error response: [code] ' . $response->getStatus() . ' [error] ' . $response->getMessage());
     }
 }
Beispiel #4
0
 /**
  * Decode request response
  *
  * @param Zend_Http_Response $response response of request
  * @throws Zend_Db_Exception
  * @return array
  */
 protected function _decodeResponse(Zend_Http_Response $response)
 {
     $result = Zend_Json::decode($response->getBody());
     if (is_null($result)) {
         throw new Zend_Db_Exception($response->getMessage());
     }
     if ($result["error"]) {
         throw new Exception($result["reason"], $this->_errors[$result["error"]]);
     }
     return $result;
 }
 /**
  * @param bool $checkForErrors
  * @return $this
  * @throws Mage_Core_Exception
  */
 public function doRequest($checkForErrors = true)
 {
     $helper = Mage::helper('core/translate');
     $this->_applyFilters();
     if (!$this->_http->getUri()) {
         Mage::throwException($helper->__('Undefined request method'));
     }
     try {
         $this->result = $this->_http->request();
     } catch (Exception $e) {
         Mage::throwException('Can\'t process request');
     }
     if (!in_array($this->result->getStatus(), $this->allowedStatuses) && $checkForErrors) {
         $body = json_decode($this->result->getBody());
         Mage::throwException($body->error ? $body->error : $this->result->getMessage());
     }
     return $this;
 }
Beispiel #6
0
 /**
  * Checks ReST response for errors.
  *
  * @param   Zend_Http_Response $response    the ReST response
  * @return  void
  * @throws  Zend_Service_Technorati_Exception
  * @access  protected
  */
 protected static function _checkResponse(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         /**
          * @see Zend_Service_Technorati_Exception
          */
         // require_once 'Zend/Service/Technorati/Exception.php';
         throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage()));
     }
 }
Beispiel #7
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @link   http://developer.ebay.com/DevZone/finding/CallRef/types/ErrorSeverity.html
  * @see    Zend_Service_Ebay_Finding_Abstract::_initXPath()
  * @throws Zend_Service_Ebay_Finding_Exception When any error occurrs during request
  * @return DOMDocument
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     // first trying, loading XML
     $dom = new DOMDocument();
     if (!($dom = @Zend_Xml_Security::scan($response->getBody(), $dom))) {
         $message = 'It was not possible to load XML returned.';
     }
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // third trying, search for error message into XML response
     // only first error that contains severiry=Error is read
     $xpath = new DOMXPath($dom);
     foreach (self::$_xmlNamespaces as $alias => $uri) {
         $xpath->registerNamespace($alias, $uri);
     }
     $ns = self::XMLNS_FINDING;
     $nsMs = self::XMLNS_MS;
     $expression = "//{$nsMs}:errorMessage[1]/{$ns}:error/{$ns}:severity[.='Error']";
     $severityNode = $xpath->query($expression)->item(0);
     if ($severityNode) {
         $errorNode = $severityNode->parentNode;
         // ebay message
         $messageNode = $xpath->query("//{$ns}:message[1]", $errorNode)->item(0);
         if ($messageNode) {
             $message = 'eBay error: ' . $messageNode->nodeValue;
         } else {
             $message = 'eBay error: unknown';
         }
         // ebay error id
         $errorIdNode = $xpath->query("//{$ns}:errorId[1]", $errorNode)->item(0);
         if ($errorIdNode) {
             $message .= ' (#' . $errorIdNode->nodeValue . ')';
         }
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once LIB_DIR . '/Zend/Service/Ebay/Finding/Exception.php';
         throw new Zend_Service_Ebay_Finding_Exception($message);
     }
     // add xpath to dom document
     // it allows service_ebay_finding classes use this
     $dom->ebayFindingXPath = $xpath;
     return $dom;
 }
Beispiel #8
0
 /**
  * Returns data of the call if there was no error
  *
  * @throws Robo47_Service_Bitly_Exception
  * @param Zend_Http_Response $response
  * @return string|array|object
  */
 protected function _getData(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         $message = 'Error on api-call: ' . $response->getMessage();
         throw new Robo47_Service_Bitly_Exception($message);
     }
     // find errors without parsing xml / json
     // @todo parse formats always and move tests into the other switch
     //       and refactor a bit, method gets big and ugly
     switch ($this->_format) {
         case self::FORMAT_XML:
             $regex = preg_quote('<errorCode>0</errorCode>', '/');
             break;
         case self::FORMAT_JSON:
             $regex = preg_quote('"errorCode": 0', '/');
             break;
     }
     if (!preg_match('/' . $regex . '/i', $response->getBody())) {
         $message = 'Error on api-call: no errorCode=0 found';
         throw new Robo47_Service_Bitly_Exception($message);
     }
     // @todo checking for errors ALWAYS!
     switch ($this->_resultFormat) {
         case self::FORMAT_RESULT_NATIVE:
             $result = $response->getBody();
             break;
         case self::FORMAT_RESULT_ARRAY:
             switch ($this->_format) {
                 case self::FORMAT_XML:
                     $result = $this->xmlToArray($response->getBody());
                     break;
                 case self::FORMAT_JSON:
                     $result = json_decode($response->getBody(), true);
                     break;
             }
             break;
         case self::FORMAT_RESULT_OBJECT:
             switch ($this->_format) {
                 case self::FORMAT_XML:
                     $result = $this->xmlToObject($response->getBody());
                     break;
                 case self::FORMAT_JSON:
                     $result = json_decode($response->getBody(), false);
                     break;
             }
             break;
     }
     return $result;
 }
Beispiel #9
0
 /**
  * Prepare a solarium response from the given request and client
  * response
  *
  * @throws HttpException
  * @param  Request             $request
  * @param  \Zend_Http_Response $response
  * @return Response
  */
 protected function prepareResponse($request, $response)
 {
     if ($response->isError()) {
         throw new HttpException($response->getMessage(), $response->getStatus());
     }
     if ($request->getMethod() == Request::METHOD_HEAD) {
         $data = '';
     } else {
         $data = $response->getBody();
     }
     // this is used because getHeaders doesn't return the HTTP header...
     $headers = explode("\n", $response->getHeadersAsString());
     return new Response($data, $headers);
 }
Beispiel #10
0
 /**
  * Checks ReST response for errors.
  *
  * @param   Zend_Http_Response $response    the ReST response
  * @return  void
  * @throws  Zend_Service_Technorati_Exception
  * @access  protected
  */
 protected static function _checkResponse(Zend_Http_Response $response)
 {
     if ($response->isError()) {
         throw new Zend_Service_Technorati_Exception(sprintf('Invalid response status code (HTTP/%s %s %s)', $response->getVersion(), $response->getStatus(), $response->getMessage()));
     }
 }
Beispiel #11
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request
  * @return SimpleXMLElement
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     // first trying, loading XML
     $xml = simplexml_load_string($response->getBody());
     if (!$xml) {
         $message = 'It was not possible to load XML returned.';
     }
     if ($xml->Ack != 'Success') {
         $message = 'Ack not success.';
     }
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once 'Zend/Service/Ebay/Trading/Exception.php';
         throw new Zend_Service_Ebay_Trading_Exception($message);
     }
     return $xml;
 }
Beispiel #12
0
 /**
  * Initializes response object.
  *
  * @param   Zend_Http_Response  $response
  *
  * @return  Jirafe_HttpConnection_Response
  */
 protected function initializeResponse(Zend_Http_Response $response)
 {
     return new Jirafe_HttpConnection_Response($response->getBody(), $response->getHeaders(), $response->isError() ? $response->getStatus() : 0, $response->getMessage());
 }
Beispiel #13
0
 /**
  * Search for error from request.
  *
  * If any error is found a DOMDocument is returned, this object contains a
  * DOMXPath object as "ebayFindingXPath" attribute.
  *
  * @param  Zend_Http_Response $response
  * @throws Zend_Service_Ebay_Trading_Exception When any error occurrs during request
  * @return SimpleXMLElement
  */
 protected function _parseResponse(Zend_Http_Response $response)
 {
     // error message
     $message = '';
     $xml = simplexml_load_string($response->getBody());
     if (!$xml) {
         $message = 'It was not possible to load XML returned.';
     }
     if ($xml->Ack != 'Success' && $xml->Ack != 'Warning') {
         $message = "Ack not success: " . print_r($xml, true);
     }
     //var_dump($dom->Ack);die;
     // second trying, check request status
     if ($response->isError()) {
         $message = $response->getMessage() . ' (HTTP status code #' . $response->getStatus() . ')';
     }
     // throw exception when an error was detected
     if (strlen($message) > 0) {
         /**
          * @see Zend_Service_Ebay_Finding_Exception
          */
         require_once 'Zend/Service/Ebay/Shopping/Exception.php';
         throw new Zend_Service_Ebay_Shopping_Exception($message);
     }
     /*
     $dom = new DOMDocument();
     $dom->loadXML($response->getBody());
     Zend_Debug::dump($dom->saveXML());
     
     Zend_Debug::dump($xml);
     */
     return $xml;
 }
 /**
  * Check status code and if appropriate return the decoded JSON
  *
  * @param Zend_Http_Response $response
  * @return array
  * @throws Zend_Service_Exception
  */
 protected function _processResult(Zend_Http_Response $response)
 {
     if ($response->getStatus() != 200) {
         require_once 'Zend/Service/Exception.php';
         throw new Zend_Service_Exception('Postmark returned ' . $response->getStatus() . ' - ' . $response->getMessage());
     }
     return json_decode($response->getBody());
 }