private function sync()
 {
     if ($this->synchronized) {
         return;
     }
     /** @var ResponseInterface $clientResponse */
     $clientResponse = $this->promise->wait();
     if (200 !== $clientResponse->getStatusCode()) {
         throw new RemoteCallFailedException();
     }
     $data = (string) $clientResponse->getBody();
     // Null (empty response) is expected if only notifications were sent
     $rawResponses = [];
     if ('' !== $data) {
         $rawResponses = json_decode($data, false);
         if (json_last_error() !== JSON_ERROR_NONE) {
             throw ResponseParseException::notAJsonResponse();
         }
     }
     if (!is_array($rawResponses) && $rawResponses instanceof \stdClass) {
         $rawResponses = [$rawResponses];
     }
     $this->responses = [];
     foreach ($rawResponses as $rawResponse) {
         try {
             $response = new SyncResponse($rawResponse);
         } catch (ResponseParseException $exception) {
             //todo: logging??? (@scaytrase)
             continue;
         }
         $this->responses[$response->getId()] = $response;
     }
     $this->synchronized = true;
 }
 /**
  * {@inheritdoc}
  */
 public function wait($unwrap = true)
 {
     $this->promise->wait(false);
     if ($unwrap) {
         if ($this->getState() == self::REJECTED) {
             throw $this->exception;
         }
         return $this->response;
     }
 }
 /**
  * Gets the relevant data from the Guzzle clients.
  *
  * @param RequestInterface $request
  * @param ResponseInterface|PromiseInterface $response
  */
 public function __construct(RequestInterface $request, $response)
 {
     if ($response instanceof ResponseInterface) {
         $this->httpStatusCode = $response->getStatusCode();
     } elseif ($response instanceof PromiseInterface) {
         $this->httpStatusCode = null;
     } else {
         throw new \InvalidArgumentException('Second constructor argument "response" must be instance of ResponseInterface or PromiseInterface');
     }
     $this->requestUrl = (string) $request->getUri();
 }
 /**
  * Gets the relevant data from the Http client.
  *
  * @param TelegramRequest                    $request
  * @param ResponseInterface|PromiseInterface $response
  */
 public function __construct(TelegramRequest $request, $response)
 {
     if ($response instanceof ResponseInterface) {
         $this->httpStatusCode = $response->getStatusCode();
         $this->body = $response->getBody();
         $this->headers = $response->getHeaders();
         $this->decodeBody();
     } elseif ($response instanceof PromiseInterface) {
         $this->httpStatusCode = null;
     } else {
         throw new \InvalidArgumentException('Second constructor argument "response" must be instance of ResponseInterface or PromiseInterface');
     }
     $this->request = $request;
     $this->endPoint = (string) $request->getEndpoint();
 }
 /**
  * Request asynchronously using the current HTTP client, preparing the
  * success and exception callbacks to transfer results in.
  *
  * @param string   $method          HTTP method
  * @param callable $uriCallable     Must return array of uri parts
  * @param callable $optionsCallable Must return array of request options
  * @param callable $onFulfilled     Option callback to chain on fulfillment
  * @param callable $onRejected      Option callback to chain on rejection
  *
  * @return PromiseInterface
  */
 protected function requestAsync($method, callable $uriCallable = null, callable $optionsCallable = null, callable $onFulfilled = null, callable $onRejected = null)
 {
     // clear previous responses and settle any async operation
     $this->clearResponse();
     // define request options
     $uri = null;
     $options = [];
     try {
         // uri
         if ($uriCallable) {
             $uri = (array) $uriCallable($this);
             // sanitize uri parts, RFC 3986
             $uri = implode('/', array_map('rawurlencode', $uri));
         }
         // options
         if ($optionsCallable) {
             $options = (array) $optionsCallable($this);
             // safely build query string
             if (isset($options['query']) && is_array($options['query'])) {
                 $options['query'] = http_build_query($options['query'], null, '&', PHP_QUERY_RFC3986);
             }
         }
     } catch (MissingPropertyException $e) {
         $this->setException($e);
         return new RejectedPromiseException($this->getReasonPhrase(), $this);
     }
     // enforce http exceptions
     $options['http_errors'] = true;
     // request
     $promise = $this->getHttpClient()->requestAsync($method, $uri, $options);
     $self = $this;
     $this->_promise = $promise->then(static function (ResponseInterface $response) use($self) {
         // clear out
         $self->_promise = null;
         // set response
         $self->transferResponseData($response);
         // return the item itself
         return $self;
     }, static function (RequestException $e) use($self) {
         // clear out
         $self->_promise = null;
         // set exception
         $self->setException($e);
         // set response, if there is one
         if ($e->hasResponse()) {
             $self->transferResponseData($e->getResponse());
             // honor the Orchestrate error messages
             if (!empty($self->_bodyArray['message'])) {
                 $self->_reasonPhrase = $self->_bodyArray['message'];
             }
         }
         // return exception, with possibility to get the item back
         return new RejectedPromise(new RejectedPromiseException($self->getReasonPhrase(), $self));
     });
     // chain
     if ($onFulfilled || $onRejected) {
         $this->_promise = $this->_promise->then($onFulfilled, $onRejected);
     }
     return $this->_promise;
 }
Example #6
0
 private function withTracking(PromiseInterface $promise, $uri)
 {
     return $promise->then(function (ResponseInterface $response) use($uri) {
         // Note that we are pushing to the front of the list as this
         // would be an earlier response than what is currently present
         // in the history header.
         $header = $response->getHeader(self::HISTORY_HEADER);
         array_unshift($header, $uri);
         return $response->withHeader(self::HISTORY_HEADER, $header);
     });
 }
Example #7
0
 /**
  * Waits for the operation to complete, when dealing with an asynchronous and incomplete operation.
  *
  * @throws \LogicException For unexpected exception not related to the SDK itself.
  */
 public function wait()
 {
     /* The operation already completed, nothing to do. */
     if ($this->isOperationComplete()) {
         return;
     }
     /* Wait for the response and unwrap it. */
     try {
         $this->result = $this->wrapResult($this->promise->wait(true));
     } catch (AwsException $e) {
         /* Set the response and exception. */
         $this->result = null;
         $this->setupClientException($e);
     }
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function wait()
 {
     $this->promise->wait(false);
 }
 /**
  * This method registers an onFulfilled and an onRejected handler to proxy the result to the deferred
  *
  * Note: The Guzzle\Promise will only be resolved when the global task queue `run` is invoked:
  * This task queue MUST be run in an event loop in order for promises to be
  * settled asynchronously.
  *
  * <code>
  * while ($eventLoop->isRunning()) {
  *     GuzzleHttp\Promise\queue()->run();
  * }
  * </code>
  *
  * @param Deferred $deferred
  * @param PromiseInterface $promise
  */
 private function resolveOrRejectDeferredAsync(Deferred $deferred, PromiseInterface $promise)
 {
     $promise->then(function (ResponseInterface $response) use($deferred) {
         try {
             $payload = $this->getPayloadFromResponse($response);
             $deferred->resolve($payload);
         } catch (\Exception $e) {
             $deferred->reject($e);
         }
     }, function ($reason) use($deferred) {
         $deferred->reject($reason);
     });
 }
Example #10
0
 public function cancel()
 {
     $this->currentPromise->cancel();
     $this->result->cancel();
 }