/** * Send asynchronous guzzle request * * @param Psr7Request $request * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(Psr7Request $request, Callback $callback) { $request = new Request($request->getMethod(), (string) $request->getUri(), $request->getHeaders(), $request->getBody(), ['future' => true]); /** @var FutureInterface $response */ $response = $this->client->send($request); $this->promises[] = $response->then(function (ResponseInterface $response) { return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); })->then($callback->success(), $callback->failure()); }
/** * Send asynchronous guzzle request * * @param RequestInterface $psrRequest * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(RequestInterface $psrRequest, Callback $callback) { $request = $this->createRequest($psrRequest, true); /** @var FutureInterface $response */ $response = $this->client->send($request); $response->then(function (ResponseInterface $response) { return new Psr7Response($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); }, function (Exception $exception) use($callback, $psrRequest) { $request = $psrRequest; $response = null; if ($exception instanceof \GuzzleHttp\Exception\RequestException) { $request = $exception->getRequest(); $response = $exception->getResponse(); } $requestException = new RequestException($exception->getMessage(), $exception->getCode(), $exception->getPrevious(), $request, $response); if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(ApiExceptionEvent::NAME, new ApiExceptionEvent($requestException, $request)); } $callback->onFailure($requestException); })->then(function (Psr7Response $response) use($callback, $psrRequest) { if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(AfterSendEvent::NAME, new AfterSendEvent($psrRequest, $response)); } $callback->onResponse($response); }); $this->responses[] = $response; }
/** * Send asynchronous guzzle request * * @param RequestInterface $request * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(RequestInterface $request, Callback $callback) { $this->promises[] = $this->client->sendAsync($request)->then(function (ResponseInterface $response) use($callback, $request) { if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(AfterSendEvent::NAME, new AfterSendEvent($request, $response)); } $callback->onResponse($response); }, function (Exception $exception) use($callback, $request) { $response = null; $context = []; if ($exception instanceof \GuzzleHttp\Exception\RequestException) { $request = $exception->getRequest(); $response = $exception->getResponse(); $context = $exception->getHandlerContext(); } $requestException = new RequestException($exception->getMessage(), $exception->getCode(), $exception->getPrevious(), $request, $response, $context); if (null !== $this->eventDispatcher) { $this->eventDispatcher->dispatch(ApiExceptionEvent::NAME, new ApiExceptionEvent($requestException, $request)); } $callback->onFailure($requestException); }); }
/** * Send asynchronous guzzle request * * @param Request $request * @param \Tebru\Retrofit\Http\Callback $callback * @return null */ public function sendAsync(Request $request, Callback $callback) { $this->promises[] = $this->client->sendAsync($request)->then($callback->success(), $callback->failure()); }