/** * Loads instance profile credentials. * * @return PromiseInterface */ public function __invoke() { return Promise\coroutine(function () { if (!$this->profile) { $this->profile = (yield $this->request(self::CRED_PATH)); } $json = (yield $this->request(self::CRED_PATH . $this->profile)); $result = $this->decodeResult($json); (yield new Credentials($result['AccessKeyId'], $result['SecretAccessKey'], $result['Token'], strtotime($result['Expiration']))); }); }
public function __invoke() { return Promise\coroutine(function () { $params = $this->logins ? ['Logins' => $this->logins] : []; $getIdParams = $params + ['IdentityPoolId' => $this->identityPoolId]; if ($this->accountId) { $getIdParams['AccountId'] = $this->accountId; } $id = (yield $this->client->getId($getIdParams)); $result = (yield $this->client->getCredentialsForIdentity(['IdentityId' => $id['IdentityId']] + $params)); (yield new Credentials($result['Credentials']['AccessKeyId'], $result['Credentials']['SecretKey'], $result['Credentials']['SessionToken'], (int) $result['Credentials']['Expiration']->format('U'))); }); }
/** * Runs a paginator asynchronously and uses a callback to handle results. * * The callback should have the signature: function (Aws\Result $result). * A non-null return value from the callback will be yielded by the * promise. This means that you can return promises from the callback that * will need to be resolved before continuing iteration over the remaining * items, essentially merging in other promises to the iteration. The last * non-null value returned by the callback will be the result that fulfills * the promise to any downstream promises. * * @param callable $handleResult Callback for handling each page of results. * The callback accepts the result that was * yielded as a single argument. If the * callback returns a promise, the promise * will be merged into the coroutine. * * @return Promise\Promise */ public function each(callable $handleResult) { return Promise\coroutine(function () use($handleResult) { $nextToken = null; do { $command = $this->createNextCommand($this->args, $nextToken); $result = (yield $this->client->executeAsync($command)); $nextToken = $this->determineNextToken($result); $retVal = $handleResult($result); if ($retVal !== null) { (yield Promise\promise_for($retVal)); } } while ($nextToken); }); }
public function executeAsync(CommandInterface $c) { return Promise\coroutine(function () use($c) { if ($region = $this->cache->get($this->getCacheKey($c['Bucket']))) { $c = $this->getRegionalizedCommand($c, $region); } try { (yield parent::executeAsync($c)); } catch (PermanentRedirectException $e) { if (empty($c['Bucket'])) { throw $e; } $region = (yield $this->lookupBucketRegion($c['Bucket'])); $this->cache->set($this->getCacheKey($c['Bucket']), $region); $c = $this->getRegionalizedCommand($c, $region); (yield parent::executeAsync($c)); } }); }
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()); }
public function promise() { return Promise\coroutine(function () { $name = $this->config['operation']; for ($state = 'retry', $attempt = 1; $state === 'retry'; $attempt++) { // Execute the operation. $args = $this->getArgsForAttempt($attempt); $command = $this->client->getCommand($name, $args); try { if ($this->config['before']) { $this->config['before']($command, $attempt); } $result = (yield $this->client->executeAsync($command)); } catch (AwsException $e) { $result = $e; } // Determine the waiter's state and what to do next. $state = $this->determineState($result); if ($state === 'success') { (yield $command); } elseif ($state === 'failed') { $msg = "The {$this->name} waiter entered a failure state."; if ($result instanceof \Exception) { $msg .= ' Reason: ' . $result->getMessage(); } (yield new RejectedPromise(new \RuntimeException($msg))); } elseif ($state === 'retry' && $attempt >= $this->config['maxAttempts']) { $state = 'failed'; (yield new RejectedPromise(new \RuntimeException("The {$this->name} waiter failed after attempt #{$attempt}."))); } } }); }
/** * Upload the source asynchronously using multipart upload operations. * * @return PromiseInterface */ public function promise() { if ($this->promise) { return $this->promise; } return $this->promise = Promise\coroutine(function () { // Initiate the upload. if ($this->state->isCompleted()) { throw new \LogicException('This multipart upload has already ' . 'been completed or aborted.'); } elseif (!$this->state->isInitiated()) { $result = (yield $this->execCommand('initiate', $this->getInitiateParams())); $this->state->setUploadId($this->info['id']['upload_id'], $result[$this->info['id']['upload_id']]); $this->state->setStatus(UploadState::INITIATED); } // Create a command pool from a generator that yields UploadPart // commands for each upload part. $resultHandler = $this->getResultHandler($errors); $commands = new CommandPool($this->client, $this->getUploadCommands($resultHandler), ['concurrency' => $this->config['concurrency'], 'before' => $this->config['before_upload']]); // Execute the pool of commands concurrently, and process errors. (yield $commands->promise()); if ($errors) { throw new MultipartUploadException($this->state, $errors); } // Complete the multipart upload. (yield $this->execCommand('complete', $this->getCompleteParams())); $this->state->setStatus(UploadState::COMPLETED); })->otherwise(function (\Exception $e) { // Throw errors from the operations as a specific Multipart error. if ($e instanceof AwsException) { $e = new MultipartUploadException($this->state, $e); } throw $e; }); }
/** * Copy an object of any size to a different location asynchronously. * * @param string $fromBucket Bucket where the copy source resides. * @param string $fromKey Key of the copy source. * @param string $destBucket Bucket to which to copy the object. * @param string $destKey Key to which to copy the object. * @param string $acl ACL to apply to the copy (default: private). * @param array $options Options used to configure the upload process. * * @see self::copy for more info about the parameters above. * @return PromiseInterface Returns a promise that will be fulfilled * with the result of the copy. */ public function copyAsync($fromBucket, $fromKey, $destBucket, $destKey, $acl = 'private', array $options = []) { // Prepare the options. static $defaults = ['before_upload' => null, 'concurrency' => 5, 'mup_threshold' => MultipartUploader::PART_MAX_SIZE, 'params' => [], 'part_size' => null, 'version_id' => null]; return Promise\coroutine($this->doCopyAsync(['Bucket' => $fromBucket, 'Key' => $fromKey], ['Bucket' => $destBucket, 'Key' => $destKey], $acl, $options + $defaults)); }
public function __invoke() { return Promise\coroutine(function () { (yield new Credentials('mocked', 'mocked', 'mocked', 'mocked')); }); }