/**
* 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);
});
}