Example #1
0
 private function exec(\Guzzle\Http\Message\RequestInterface $request)
 {
     $start = microtime(true);
     $this->responseCode = 0;
     // Get snapshot of request headers.
     $request_headers = $request->getRawHeaders();
     // Mask authorization for logs.
     $request_headers = preg_replace('!\\nAuthorization: (Basic|Digest) [^\\r\\n]+\\r!i', "\nAuthorization: \$1 [**masked**]\r", $request_headers);
     try {
         $response = $request->send();
     } catch (\Guzzle\Http\Exception\BadResponseException $e) {
         $response = $e->getResponse();
     } catch (\Guzzle\Http\Exception\CurlException $e) {
         // Timeouts etc.
         DebugData::$raw = '';
         DebugData::$code = $e->getErrorNo();
         DebugData::$code_status = $e->getError();
         DebugData::$code_class = 0;
         DebugData::$exception = $e->getMessage();
         DebugData::$opts = array('request_headers' => $request_headers);
         DebugData::$data = null;
         $exception = new ResponseException($e->getError(), $e->getErrorNo(), $request->getUrl(), DebugData::$opts);
         $exception->requestObj = $request;
         // Log Exception
         $headers_array = $request->getHeaders();
         unset($headers_array['Authorization']);
         $headerString = '';
         foreach ($headers_array as $value) {
             $headerString .= $value->getName() . ': ' . $value . " ";
         }
         $log_message = '{code_status} ({code}) Request Details:[ {r_method} {r_resource} {r_scheme} {r_headers} ]';
         $httpScheme = strtoupper(str_replace('https', 'http', $request->getScheme())) . $request->getProtocolVersion();
         $log_params = array('code' => $e->getErrorNo(), 'code_status' => $e->getError(), 'r_method' => $request->getUrl(), 'r_resource' => $request->getRawHeaders(), 'r_scheme' => $httpScheme, 'r_headers' => $headerString);
         self::$logger->emergency($log_message, $log_params);
         throw $exception;
     }
     $this->responseCode = $response->getStatusCode();
     $this->responseText = trim($response->getBody(true));
     $this->responseLength = $response->getContentLength();
     $this->responseMimeType = $response->getContentType();
     $this->responseObj = array();
     $content_type = $response->getContentType();
     $firstChar = substr($this->responseText, 0, 1);
     if (strpos($content_type, '/json') !== false && ($firstChar == '{' || $firstChar == '[')) {
         $response_obj = @json_decode($this->responseText, true);
         if (is_array($response_obj)) {
             $this->responseObj = $response_obj;
         }
     }
     $status = self::getStatusMessage($this->responseCode);
     $code_class = floor($this->responseCode / 100);
     DebugData::$raw = $this->responseText;
     DebugData::$opts = array('request_headers' => $request_headers, 'response_headers' => $response->getRawHeaders());
     if ($request instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
         DebugData::$opts['request_body'] = (string) $request->getBody();
     }
     DebugData::$opts['request_type'] = class_implements($request);
     DebugData::$data = $this->responseObj;
     DebugData::$code = $this->responseCode;
     DebugData::$code_status = $status;
     DebugData::$code_class = $code_class;
     DebugData::$exception = null;
     DebugData::$time_elapsed = microtime(true) - $start;
     if ($code_class != 2) {
         $uri = $request->getUrl();
         if (!empty($this->responseCode) && isset($this->responseObj['message'])) {
             $message = 'Code: ' . $this->responseCode . '; Message: ' . $this->responseObj['message'];
         } else {
             $message = 'API returned HTTP code of ' . $this->responseCode . ' when fetching from ' . $uri;
         }
         DebugData::$exception = $message;
         $this->debugCallback(DebugData::toArray());
         self::$logger->error($this->responseText);
         // Create better status to show up in logs
         $status .= ': ' . $request->getMethod() . ' ' . $uri;
         if ($request instanceof \Guzzle\Http\Message\EntityEnclosingRequestInterface) {
             $body = $request->getBody();
             if ($body instanceof \Guzzle\Http\EntityBodyInterface) {
                 $status .= ' with Content-Length of ' . $body->getContentLength() . ' and Content-Type of ' . $body->getContentType();
             }
         }
         $exception = new ResponseException($status, $this->responseCode, $uri, DebugData::$opts, $this->responseText);
         $exception->requestObj = $request;
         $exception->responseObj = $response;
         throw $exception;
     }
     $this->debugCallback(DebugData::toArray());
 }