/** @test */ public function it_fails_when_try_to_send_requests_in_bath_with_wrong_method() { $this->expectException(\RuntimeException::class); $expectedRequest1 = new Request('GET', 'http://some-absolute/uri1', ['Accept' => ['application/vnd.eventstore.atom+json']]); $this->guzzle->sendAsync($expectedRequest1, [])->willReturn(new Response(200, [], '{"some": "data1"}')); $this->client->requestsBatch('PUT', ['http://some-absolute/uri1']); }
/** * @param Psr7Request $request * @param Array $options Http options for the client. * * @return GuzzleHttp\Promise\PromiseInterface Promise that will be resolved with a * Psr\Http\Message\ResponseInterface * response object. */ public function __invoke(RequestInterface $request, array $options) { // Remove options that are not supported. foreach (array_keys($options) as $key) { if (!isset(self::$validOptions[$key])) { unset($options[$key]); } } return $this->client->sendAsync($request, $options); }
/** * @param Psr7Request $request * @param array $options * * @return Promise\Promise */ public function __invoke(Psr7Request $request, array $options = []) { $request = $request->withHeader('User-Agent', $request->getHeaderLine('User-Agent') . ' ' . \GuzzleHttp\default_user_agent()); return $this->client->sendAsync($request, $this->parseOptions($options))->otherwise(static function (\Exception $e) { $error = ['exception' => $e, 'connection_error' => $e instanceof ConnectException, 'response' => null]; if ($e instanceof RequestException && $e->getResponse()) { $error['response'] = $e->getResponse(); } return new Promise\RejectedPromise($error); }); }
/** * @param ClientInterface $client Client used to send the requests. * @param array|\Iterator $requests Requests or functions that return * requests to send concurrently. * @param array $config Associative array of options * - concurrency: (int) Maximum number of requests to send concurrently * - options: Array of request options to apply to each request. * - fulfilled: (callable) Function to invoke when a request completes. * - rejected: (callable) Function to invoke when a request is rejected. */ public function __construct(ClientInterface $client, $requests, array $config = []) { // Backwards compatibility. if (isset($config['pool_size'])) { $config['concurrency'] = $config['pool_size']; } elseif (!isset($config['concurrency'])) { $config['concurrency'] = 25; } if (isset($config['options'])) { $opts = $config['options']; unset($config['options']); } else { $opts = []; } $iterable = \GuzzleHttp\Promise\iter_for($requests); $requests = function () use($iterable, $client, $opts) { foreach ($iterable as $key => $rfn) { if ($rfn instanceof RequestInterface) { (yield $key => $client->sendAsync($rfn, $opts)); } elseif (is_callable($rfn)) { (yield $key => $rfn($opts)); } else { throw new \InvalidArgumentException('Each value yielded by ' . 'the iterator must be a Psr7\\Http\\Message\\RequestInterface ' . 'or a callable that returns a promise that fulfills ' . 'with a Psr7\\Message\\Http\\ResponseInterface object.'); } } }; $this->each = new EachPromise($requests(), $config); }
/** * {@inheritdoc} * * @return PromiseInterface */ public function run(CommandInterface $command, $result = null) { $key = $command->getRequestMethod() === 'GET' ? 'query' : 'body'; $options = [$key => []]; if (!empty($params = $command->getParams())) { $options[$key] = array_merge($options[$key], $params); } if ($config = $this->getConfig()) { if (!empty($config->getSteamKey())) { $options[$key]['key'] = $config->getSteamKey(); } $this->urlBuilder->setBaseUrl($config->getBaseSteamApiUrl()); } $request = new Request($command->getRequestMethod(), $this->urlBuilder->build($command)); return $this->client->sendAsync($request, $options); }
/** * {@inheritdoc} */ public function invoke($calls) { try { if (!is_array($calls) && $calls instanceof RpcRequestInterface) { $transformedCall = $this->transformCall($calls); return new JsonRpcResponseCollection($this->client->sendAsync($this->createHttpRequest($transformedCall)), [new RequestTransformation($calls, $transformedCall)]); } $requests = []; $batchRequest = []; foreach ($calls as $key => $call) { $transformedCall = $this->transformCall($call); $requests[spl_object_hash($call)] = new RequestTransformation($call, $transformedCall); $batchRequest[] = $transformedCall; } return new JsonRpcResponseCollection($this->client->sendAsync($this->createHttpRequest($batchRequest)), $requests); } catch (GuzzleException $exception) { throw new RemoteCallFailedException($exception->getMessage(), 0, $exception); } }
/** * 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); }); }
private function getRequestWorkerFn() { while ($request = $this->queue->pop()) { try { $this->dispatchSending($request); $promise = $this->client->sendAsync($request)->then($this->getRequestFulfilledFn($request), $this->getRequestRejectedFn($request)); (yield $promise); } catch (\Exception $e) { // Mark the request as complete so we don't get stuck on it. $this->queue->complete($request); $this->dispatchException($request, $e, null); (yield \GuzzleHttp\Promise\rejection_for($e)); } } }
/** * {@inheritdoc} */ public function sendAsyncRequest(RequestInterface $request) { $promise = $this->client->sendAsync($request); return new Guzzle6Promise($promise, $request); }
/** * 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()); }
/** * @param RequestInterface $request * @return PromiseInterface|ResponseInterface */ protected function send(RequestInterface $request) { return $this->async ? $this->client->sendAsync($request) : $this->client->send($request); }
/** * {@inheritdoc} */ public function sendAsync(RequestInterface $request, array $options = []) : Promise { $promise = $this->client->sendAsync($request, $options); return new GuzzlePromise($promise, $request); }
/** * {@inheritdoc} */ public function sendAllAsync(array $requests) { return $this->httpClient->sendAsync($this->createRequest(RequestInterface::BATCH, $this->getBatchRequestOptions($requests)))->then(function (ResponseInterface $response) { return $this->getBatchResponses($response); }); }