本文整理汇总了PHP中GuzzleHttp\ClientInterface::sendAsync方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::sendAsync方法的具体用法?PHP ClientInterface::sendAsync怎么用?PHP ClientInterface::sendAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::sendAsync方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: it_fails_when_try_to_send_requests_in_bath_with_wrong_method
/** @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']);
}
示例2: __invoke
/**
* @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);
}
示例3: __invoke
/**
* @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);
});
}
示例4: __construct
/**
* @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);
}
示例5: run
/**
* {@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);
}
示例6: invoke
/**
* {@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);
}
}
示例7: sendAsync
/**
* 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);
});
}
示例8: getRequestWorkerFn
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));
}
}
}
示例9: sendAsyncRequest
/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$promise = $this->client->sendAsync($request);
return new Guzzle6Promise($promise, $request);
}
示例10: sendAsync
/**
* 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());
}
示例11: send
/**
* @param RequestInterface $request
* @return PromiseInterface|ResponseInterface
*/
protected function send(RequestInterface $request)
{
return $this->async ? $this->client->sendAsync($request) : $this->client->send($request);
}
示例12: sendAsync
/**
* {@inheritdoc}
*/
public function sendAsync(RequestInterface $request, array $options = []) : Promise
{
$promise = $this->client->sendAsync($request, $options);
return new GuzzlePromise($promise, $request);
}
示例13: sendAllAsync
/**
* {@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);
});
}