Beispiel #1
0
 /**
  * Process a received response
  *
  * @param array $context Contextual information
  * @throws RequestException|BadResponseException on unsuccessful responses
  */
 protected function processResponse(array $context = array())
 {
     if (!$this->response) {
         // If no response, then processResponse shouldn't have been called
         $e = new RequestException('Error completing request');
         $e->setRequest($this);
         throw $e;
     }
     $this->state = self::STATE_COMPLETE;
     // A request was sent, but we don't know if we'll send more or if the final response will be successful
     $this->dispatch('request.sent', $this->getEventArray() + $context);
     // Some response processors will remove the response or reset the state (example: ExponentialBackoffPlugin)
     if ($this->state == RequestInterface::STATE_COMPLETE) {
         // The request completed, so the HTTP transaction is complete
         $this->dispatch('request.complete', $this->getEventArray());
         // If the response is bad, allow listeners to modify it or throw exceptions. You can change the response by
         // modifying the Event object in your listeners or calling setResponse() on the request
         if ($this->response->isError()) {
             $event = new Event($this->getEventArray());
             $this->getEventDispatcher()->dispatch('request.error', $event);
             // Allow events of request.error to quietly change the response
             if ($event['response'] !== $this->response) {
                 $this->response = $event['response'];
             }
         }
         // If a successful response was received, dispatch an event
         if ($this->response->isSuccessful()) {
             $this->dispatch('request.success', $this->getEventArray());
         }
     }
 }
Beispiel #2
0
 /**
  * @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;
 }