/** * @param array $criteria Criterias for querying geocoding * @return GuzzlePromise The promise for guzzle request * * @throws InvalidArgumentException */ public function lookup($criteria) { // TODO: Discuss criteria validation if (!is_array($criteria)) { throw new InvalidArgumentException('Criteria must be an array with keys and values'); } $lookup_promise = null; $lookup_promise = new GuzzlePromise(function () use($criteria, &$lookup_promise) { $this->client_interface->sendAsync(new Request("GET", sprintf(self::GEOCODEURL, $this->response_type, $this->arrayToQueryParams($criteria))))->then(function ($response) use($lookup_promise) { if ($response->getStatusCode() === 200) { $geo_location_response = json_decode($response->getBody()); if ($geo_location_response->status !== "OK") { $lookup_promise->reject(sprintf("Server responded with status %s", $geo_location_response->status)); } else { $lookup_promise->resolve(Factory::create('GoogleGeoCoder\\GeoCollection', $geo_location_response->results)); } } else { $lookup_promise->reject(sprintf("Server responded with code %s", $response->getStatusCode())); } }, function ($response) use($lookup_promise) { $lookup_promise->reject(sprintf("Server responded with failure")); })->wait(); }); return $lookup_promise; }
/** * @internal */ public function _handleFailure($reason) { unset($this->currentPromise); try { $nextYield = $this->generator->throw(exception_for($reason)); // The throw was caught, so keep iterating on the coroutine $this->nextCoroutine($nextYield); } catch (Exception $exception) { $this->result->reject($exception); } catch (Throwable $throwable) { $this->result->reject($throwable); } }
/** * @param \SplFileInfo|string $cwd * @return PromiseInterface */ public function enqueue($cwd = null) { $process = $this->factory->make($cwd); /** @var Promise $promise */ $promise = new Promise(function () use($process, &$promise) { if ($process->isStarted()) { $process->wait(); } $process->isSuccessful() ? $promise->resolve($process) : $promise->reject($process); }); $this->queue->add($process); $process->setOptions([ProcessQueue::PROMISE_KEY => $promise]); return $promise; }
public function run(callable $callback, $time) { $promise = new Promise(); $loop = Runner::getLoop(); $loop->addTimer($time, function () use($promise, $callback) { if ($promise->getState() === PromiseInterface::PENDING) { try { $promise->resolve($callback()); } catch (\Exception $e) { $promise->reject($e); } } }); return $promise; }
/** * @test */ public function it_rejects_deferred_with_json_response_asynchronous() { $producerInAsyncMode = new HttpMessageProducer($this->guzzleClient, new NoOpMessageConverter(), null, true); $fetchDataQuery = new FetchSomething(['filter' => 'foo']); $ex = new \Exception('ka boom'); $queryDeferred = new Deferred(); $producerInAsyncMode($fetchDataQuery, $queryDeferred); $this->responsePromise->reject($ex); //Perform next tick, required to resolve the promise when we are in async mode $queue = \GuzzleHttp\Promise\queue(); $queue->run(); $rejectionReason = null; $queryDeferred->promise()->otherwise(function ($reason) use(&$rejectionReason) { $rejectionReason = $reason; }); $this->assertSame($ex, $rejectionReason); }
public function __invoke(RequestInterface $request, array $options) { $artaxRequest = $this->convertRequest($request, $options); $artaxResponsePromise = $this->artaxClient->request($artaxRequest); $guzzleResponsePromise = new Promise(function () use($artaxResponsePromise) { \Amp\wait($artaxResponsePromise); }); $artaxResponsePromise->when(function ($error = null, Response $artaxResponse = null) use($request, $options, $guzzleResponsePromise) { if ($error) { $guzzleResponsePromise->reject($error); } else { $response = $this->convertResponse($artaxResponse, $request, $options); $guzzleResponsePromise->resolve($response); } }); return $guzzleResponsePromise; }
public function start() { $promise = new Promise([$this, 'wait']); $this->process->start(function ($type, $buffer) use($promise) { if (Process::ERR === $type) { $this->logger->error($buffer); } else { $this->logger->debug($buffer); } if ($promise->getState() === Promise::PENDING) { if (Process::ERR === $type) { $promise->reject(false); } else { $promise->resolve(true); } } }); return $promise; }
public function testCoroutineOtherwiseIntegrationTest() { $a = new P\Promise(); $b = new P\Promise(); $promise = P\coroutine(function () use($a, $b) { // Execute the pool of commands concurrently, and process errors. (yield $a); (yield $b); })->otherwise(function (\Exception $e) { // Throw errors from the operations as a specific Multipart error. throw new \OutOfBoundsException('a', 0, $e); }); $a->resolve('a'); $b->reject('b'); $reason = P\inspect($promise)['reason']; $this->assertInstanceOf('OutOfBoundsException', $reason); $this->assertInstanceOf('GuzzleHttp\\Promise\\RejectionException', $reason->getPrevious()); }
/** * @param string $method * @param mixed $params * * @return PromiseInterface */ public function callAsync($method, $params) { $requestId = uniqid(); $options = $this->prepareCall($method, $params, $requestId); $response = $this->getClient()->postAsync('', $options); $promise = new Promise(function ($unwrap) use($response) { return $response->wait($unwrap); }, function () use($response) { return $response->cancel(); }); $response->then(function (ResponseInterface $res) use($promise) { try { $promise->resolve($this->handleResponse($res->getBody())); return $res; } catch (\Exception $e) { $promise->reject($e); throw $e; } }, function (\Exception $reqException) use($promise, $requestId) { $this->logResponse($requestId, strval($reqException)); $promise->reject($reqException); return $reqException; }); $promise->then(function ($res) use($requestId) { $this->logResponse($requestId, json_encode($res)); return $res; }, function ($res) use($requestId) { $this->logResponse($requestId, strval($res)); return $res; }); return $promise; }
public function testOtherwiseIsSugarForRejections() { $p = new Promise(); $p->reject('foo'); $p->otherwise(function ($v) use(&$c) { $c = $v; }); P\queue()->run(); $this->assertEquals($c, 'foo'); }
/** * @param RequestInterface $request * @param array $options * @return Promise */ public function __invoke(RequestInterface $request, array $options) { $ready = false; $promise = new Promise(function () use(&$ready) { do { $this->loop->tick(); } while (!$ready); }); $this->requestFactory->create($request, $options, $this->httpClient, $this->loop)->then(function (ResponseInterface $response) use(&$ready, $promise) { $ready = true; $promise->resolve($response); $this->invokeQueue(); }, function ($error) use(&$ready, $promise) { $ready = true; $promise->reject($error); $this->invokeQueue(); }); return $promise; }
/** * Magic method which intercepts async calls, finds the sequential version, and wraps it in a * {@see Promise} object. In order for this to happen, the called methods need to be in the * following format: `createAsync`, where `create` is the sequential method being wrapped. * * @param $methodName The name of the method being invoked. * @param $args The arguments to be passed to the sequential method. * * @return Promise */ public function __call($methodName, $args) { if (substr($methodName, -5) === 'Async') { $realMethod = substr($methodName, 0, -5); if (!method_exists($this, $realMethod)) { throw new \InvalidArgumentException(sprintf('%s is not a defined method on %s', $realMethod, get_class($this))); } $promise = new Promise(function () use(&$promise, $realMethod, $args) { $value = call_user_func_array([$this, $realMethod], $args); $promise->resolve($value); }, function ($e) use(&$promise) { $promise->reject($e); }); return $promise; } }