/**
  * {@inheritDoc}
  */
 public function execute(Request $request)
 {
     $uri = $this->getUri($request->getMethod(), $request->getParam('apikey'));
     $request = $this->client->post($uri)->addPostFields($request->getParams());
     try {
         $rawResponse = $request->send();
         $response = unserialize($rawResponse->getBody());
         if (false === $response) {
             // bad response
             $response = array('error' => 'Bad Response. Got this: ' . $rawResponse->getBody(), 'code' => -99);
         }
     } catch (\Exception $e) {
         if ($e instanceof TimeoutException) {
             // timeout exception
             $response = array('error' => 'Could not read response (timed out)', 'code' => -98);
         } else {
             // unknown exception
             $response = array('error' => 'An error occured: ' . $e->getMessage(), 'code' => -99);
         }
     }
     if (is_array($response) && isset($response['error'])) {
         // return an error response
         return new Response($response, Response::STATUS_ERROR);
     }
     // return a success response
     return new Response($response);
 }
Example #2
0
 /**
  * Perform the given Request
  *
  * @param Request $request
  *
  * @return Response
  */
 private function callServer(Request $request)
 {
     $this->errorMessage = null;
     $this->errorCode = null;
     $request->setParam('apikey', $this->apikey);
     $response = $this->connection->execute($request);
     if ($response->isError()) {
         $content = $response->getContent();
         $this->errorMessage = $content['error'];
         $this->errorCode = $content['code'];
     }
     $this->lastRequest = $request;
     $this->lastResponse = $response;
     return $response;
 }
 /**
  * {@inheritDoc}
  */
 public function execute(Request $request)
 {
     $uri = $this->getUri($request->getMethod(), $request->getParam('apikey'));
     $httpRequest = $this->client->post($uri, array('Content-Type' => 'application/json'))->setBody(json_encode($request->getParams()));
     $rawResponse = false;
     try {
         $rawResponse = $httpRequest->send();
         $response = $this->parseResponse($rawResponse);
     } catch (BadResponseException $e) {
         $body = $this->parseResponse($e->getResponse());
         $response = array('error' => isset($body['error']) ? $body['error'] : 'An error occured: ' . $e->getMessage(), 'code' => isset($body['code']) ? $body['code'] : self::INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR);
     } catch (\Exception $e) {
         $response = array('error' => 'An error occurred: ' . $e->getMessage(), 'code' => self::INTERNAL_CODE_UNKNOWN_EXCEPTION_ERROR);
     }
     if (false === $response) {
         $response = $this->handleEdgeCase($rawResponse);
     }
     if (is_array($response) && isset($response['error'])) {
         // return an error response
         return new Response($response, Response::STATUS_ERROR);
     }
     // return a success response
     return new Response($response);
 }