/** * Write data to the response body of a request * * @param resource $curl Curl handle * @param string $write Data that was received * * @return int */ public function writeResponseBody($curl, $write) { if ($this->emitIo) { $this->request->dispatch('curl.callback.write', array('request' => $this->request, 'write' => $write)); } return $this->request->getResponse()->getBody()->write($write); }
/** * Add a request to the history * * @param RequestInterface $request Request to add * @param Response $response Response of the request * * @return HistoryPlugin */ public function add(RequestInterface $request, Response $response = null) { if (!$response && $request->getResponse()) { $response = $request->getResponse(); } $this->transactions[] = array('request' => $request, 'response' => $response); if (count($this->transactions) > $this->getlimit()) { array_shift($this->transactions); } return $this; }
/** * Write data to the response body of a request * * @param resource $curl Curl handle * @param string $write Data that was received * * @return int */ public function writeResponseBody($curl, $write) { if ($this->emitIo) { $this->request->dispatch('curl.callback.write', array('request' => $this->request, 'write' => $write)); } if ($response = $this->request->getResponse()) { return $response->getBody()->write($write); } else { // Unexpected data received before response headers - abort transfer return 0; } }
/** * Create a new response object from a Clickatell request and response. * * @param \Guzzle\Http\Message\RequestInterface $request The request object * associated with the response. * @throws \UnexpectedValueException when the request does not have a response. */ public function __construct($request) { $response = $request->getResponse(); if (!$response) { throw new \UnexpectedValueException('Request must have have a response.'); } $this->request = $request; $this->parsedResponse = $this->parseBody($response->getBody()); }
/** * Add a request to the history * * @param RequestInterface $request Request to add * * @return HistoryPlugin */ public function add(RequestInterface $request) { if ($request->getResponse()) { $this->requests[] = $request; if (count($this->requests) > $this->getlimit()) { array_shift($this->requests); } } return $this; }
/** * Sends a request * * @param RequestInterface $request * @return Response * @throws ForbiddenException * @throws MetricaException */ protected function sendRequest(RequestInterface $request) { try { $request->setHeader('User-Agent', $this->getUserAgent()); $response = $request->send(); } catch (ClientErrorResponseException $ex) { $result = $request->getResponse(); $code = $result->getStatusCode(); $message = $result->getReasonPhrase(); if ($code === 403) { throw new ForbiddenException($message); } throw new MetricaException('Service responded with error code: "' . $code . '" and message: "' . $message . '"'); } return $response; }
/** * Update a request based on the log messages of the CurlHandle * * @param RequestInterface $request Request to update */ public function updateRequestFromTransfer(RequestInterface $request) { if (!$request->getResponse()) { return; } // Update the transfer stats of the response $request->getResponse()->setInfo($this->getInfo()); if (!($log = $this->getStderr(true))) { return; } // Parse the cURL stderr output for outgoing requests $headers = ''; fseek($log, 0); while (($line = fgets($log)) !== false) { if ($line && $line[0] == '>') { $headers = substr(trim($line), 2) . "\r\n"; while (($line = fgets($log)) !== false) { if ($line[0] == '*' || $line[0] == '<') { break; } else { $headers .= trim($line) . "\r\n"; } } } } // Add request headers to the request exactly as they were sent if ($headers) { $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($headers); if (!empty($parsed['headers'])) { $request->setHeaders(array()); foreach ($parsed['headers'] as $name => $value) { $request->setHeader($name, $value); } } if (!empty($parsed['version'])) { $request->setProtocolVersion($parsed['version']); } } }
/** * @link https://github.com/guzzle/guzzle/issues/710 */ private function validateResponseWasSet(RequestInterface $request) { if ($request->getResponse()) { return true; } $body = $request instanceof EntityEnclosingRequestInterface ? $request->getBody() : null; if (!$body) { $rex = new RequestException('No response was received for a request with no body. This' . ' could mean that you are saturating your network.'); $rex->setRequest($request); $this->removeErroredRequest($request, $rex); } elseif (!$body->isSeekable() || !$body->seek(0)) { // Nothing we can do with this. Sorry! $rex = new RequestException('The connection was unexpectedly closed. The request would' . ' have been retried, but attempting to rewind the' . ' request body failed.'); $rex->setRequest($request); $this->removeErroredRequest($request, $rex); } else { $this->remove($request); // Add the request back to the batch to retry automatically. $this->requests[] = $request; $this->addHandle($request); } return false; }
/** * Collect time for a Guzzle request * * @param Guzzle\Http\Message\RequestInterface $request * * @return array */ private function collectTime(GuzzleRequestInterface $request) { $response = $request->getResponse(); return array('total' => $response->getInfo('total_time'), 'connection' => $response->getInfo('connect_time')); }
/** * @param $request RequestInterface * @return mixed * @throws Exception */ private function sendRequest(RequestInterface $request) { try { $request->send(); } catch (ClientErrorResponseException $e) { $data = json_decode($request->getResponse()->getBody(true), true); if ($e->getResponse()->getStatusCode() == 404) { throw new CredentialsNotFoundException($data["message"], null, $e); } else { throw new Exception('Error from Provisioning API: ' . $data["message"], null, $e); } } catch (BadResponseException $e) { throw new Exception('Error receiving response from Provisioning API', null, $e); } $result = $this->parseResponse($request->getResponse()->getBody(true)); return $result; }
/** * Throw a too many redirects exception for a request * * @param RequestInterface $request Request * @throws TooManyRedirectsException when too many redirects have been issued */ protected function throwTooManyRedirectsException(RequestInterface $request) { $lines = array(); $response = $request->getResponse(); do { $lines[] = '> ' . $response->getRequest()->getRawHeaders() . "\n\n< " . $response->getRawHeaders(); $response = $response->getPreviousResponse(); } while ($response); throw new TooManyRedirectsException("Too many redirects were issued for this transaction:\n" . implode("* Sending redirect request\n", array_reverse($lines))); }
/** * @param RequestInterface $request * @return array * @throws CommunicationError * @throws ExpiredAuthRequestError * @throws InvalidCredentialsError * @throws InvalidRequestError * @throws InvalidResponseError * @throws LaunchKeyEngineError * @throws NoPairedDevicesError * @throws NoSuchUserError * @throws RateLimitExceededError */ private function sendRequest(RequestInterface $request) { try { $response = $request->send(); $this->debugLog("Response received", array("response" => $response->getMessage())); } catch (ClientErrorResponseException $e) { $message = $e->getMessage(); $code = $e->getCode(); try { $data = $this->jsonDecodeData($request->getResponse()->getBody()); $this->throwExceptionForErrorResponse($data, $e); } catch (InvalidResponseError $de) { throw new InvalidRequestError($message, $code, $e); } } catch (ServerErrorResponseException $e) { throw new CommunicationError("Error performing request", $e->getCode(), $e); } $data = $this->jsonDecodeData($response->getBody(true)); // If debug response with data in the "response" attribute return that return isset($data["response"]) ? $data["response"] : $data; }
/** * Throw a too many redirects exception for a request * * @param RequestInterface $request Request * @throws TooManyRedirectsException when too many redirects have been issued */ protected function throwTooManyRedirectsException(RequestInterface $request) { $responses = array(); // Create a nice message to use when throwing the exception that shows each request/response transaction do { $response = $request->getResponse(); $responses[] = '> ' . $request->getRawHeaders() . "\n\n< " . $response->getRawHeaders(); $request = $response->getPreviousResponse() ? $response->getPreviousResponse()->getRequest() : null; } while ($request); $transaction = implode("* Sending redirect request\n", array_reverse($responses)); throw new TooManyRedirectsException("Too many redirects were issued for this transaction:\n{$transaction}"); }
/** * Run guzzle request * * @param RequestInterface $request * * @return array */ private function runRequest(RequestInterface $request) { try { $request->send(); //send the req and return the json return $request->getResponse()->json(); } catch (Exception $e) { return array('error' => $request->getResponse()->json()); } }