Skip to content

Commit

Permalink
Making credential providers async.
Browse files Browse the repository at this point in the history
- Moving serialize from client to functions.php.
- Removed refreshable credentials. This is something that will be done
  in the memoized credential provider.
- Updated credential collaborators to use promise->wait()
  • Loading branch information
mtdowling committed May 10, 2015
1 parent 50e71d5 commit d25dea0
Show file tree
Hide file tree
Showing 26 changed files with 391 additions and 559 deletions.
31 changes: 7 additions & 24 deletions src/AwsClient.php
Expand Up @@ -4,20 +4,14 @@
use Aws\Api\ApiProvider;
use Aws\Api\DocModel;
use Aws\Api\Service;
use Aws\Credentials\CredentialsInterface;
use Aws\Signature\SignatureProvider;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Uri;

/**
* Default AWS client implementation
*/
class AwsClient implements AwsClientInterface
{
/** @var CredentialsInterface AWS credentials */
private $credentials;

/** @var string */
private $region;

Expand All @@ -30,6 +24,9 @@ class AwsClient implements AwsClientInterface
/** @var callable */
private $signatureProvider;

/** @var callable */
private $credentialProvider;

/** @var HandlerList */
private $handlerList;

Expand Down Expand Up @@ -140,7 +137,7 @@ public function __construct(array $args)
$this->api = $config['api'];
$this->signatureProvider = $config['signature_provider'];
$this->endpoint = new Uri($config['endpoint']);
$this->credentials = $config['credentials'];
$this->credentialProvider = $config['credentials'];
$this->region = isset($config['region']) ? $config['region'] : null;
$this->config = $config['config'];
$this->defaultRequestOptions = $config['http'];
Expand Down Expand Up @@ -180,7 +177,8 @@ public function getConfig($option = null)

public function getCredentials()
{
return $this->credentials;
$fn = $this->credentialProvider;
return $fn();
}

public function getEndpoint()
Expand Down Expand Up @@ -271,21 +269,6 @@ public function getWaiter($name, array $args = [])
return new Waiter($this, $name, $args, $config);
}

public function serialize(CommandInterface $command)
{
$request = null;
// Return a mock response.
$command->getHandlerList()->setHandler(
static function (CommandInterface $cmd, RequestInterface $req) use (&$request) {
$request = $req;
return new FulfilledPromise(new Result([]));
}
);
$this->execute($command);

return $request;
}

/**
* Get the signature_provider function of the client.
*
Expand Down Expand Up @@ -325,7 +308,7 @@ private function addSignatureMiddleware()
$this->handlerList->append(
'sign:signer',
Middleware::signer(
$this->credentials,
$this->credentialProvider,
constantly(SignatureProvider::resolve(
$this->signatureProvider,
$this->config['signature_version'],
Expand Down
17 changes: 6 additions & 11 deletions src/AwsClientInterface.php
Expand Up @@ -2,7 +2,7 @@
namespace Aws;

use Psr\Http\Message\UriInterface;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Promise\PromiseInterface;

/**
* Represents an AWS client.
Expand Down Expand Up @@ -62,18 +62,13 @@ public function execute(CommandInterface $command);
public function executeAsync(CommandInterface $command);

/**
* Serialize a request for a command but do not send it.
* Returns a promise that is fulfilled with an
* {@see \Aws\Credentials\CredentialsInterface} object.
*
* @param CommandInterface $command Command to serialize.
* If you need the credentials synchronously, then call the wait() method
* on the returned promise.
*
* @return RequestInterface
*/
public function serialize(CommandInterface $command);

/**
* Returns the AWS credentials associated with the client.
*
* @return \Aws\Credentials\CredentialsInterface
* @return PromiseInterface
*/
public function getCredentials();

Expand Down
39 changes: 19 additions & 20 deletions src/ClientResolver.php
Expand Up @@ -6,10 +6,10 @@
use Aws\Api\Service;
use Aws\Credentials\Credentials;
use Aws\Credentials\CredentialsInterface;
use Aws\Credentials\NullCredentials;
use Aws\Signature\SignatureProvider;
use Aws\Endpoint\EndpointProvider;
use Aws\Credentials\CredentialProvider;
use GuzzleHttp\Promise;
use InvalidArgumentException as IAE;

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ class ClientResolver
'valid' => ['Aws\Credentials\CredentialsInterface', 'array', 'bool', 'callable'],
'doc' => 'Specifies the credentials used to sign requests. Provide an Aws\Credentials\CredentialsInterface object, an associative array of "key", "secret", and an optional "token" key, `false` to use null credentials, or a callable credentials provider used to create credentials or return null. See Aws\\Credentials\\CredentialProvider for a list of built-in credentials providers. If no credentials are provided, the SDK will attempt to load them from the environment.',
'fn' => [__CLASS__, '_apply_credentials'],
'default' => [__CLASS__, '_default_credentials'],
'default' => [CredentialProvider::class, 'defaultProvider'],
],
'retries' => [
'type' => 'value',
Expand Down Expand Up @@ -329,20 +329,26 @@ public static function _apply_retries($value, array &$args, HandlerList $list)

public static function _apply_credentials($value, array &$args)
{
if ($value instanceof CredentialsInterface) {
if (is_callable($value)) {
return;
} elseif (is_callable($value)) {
// Invoke the credentials provider and throw if it does not resolve.
$args['credentials'] = CredentialProvider::resolve($value);
} elseif (is_array($value) && isset($value['key']) && isset($value['secret'])) {
$args['credentials'] = new Credentials(
$value['key'],
$value['secret'],
isset($value['token']) ? $value['token'] : null,
isset($value['expires']) ? $value['expires'] : null
} elseif ($value instanceof CredentialsInterface) {
$args['credentials'] = CredentialProvider::fromCredentials($value);
} elseif (is_array($value)
&& isset($value['key'])
&& isset($value['secret'])
) {
$args['credentials'] = CredentialProvider::fromCredentials(
new Credentials(
$value['key'],
$value['secret'],
isset($value['token']) ? $value['token'] : null,
isset($value['expires']) ? $value['expires'] : null
)
);
} elseif ($value === false) {
$args['credentials'] = new Credentials('', '');
$args['credentials'] = CredentialProvider::fromCredentials(
new Credentials('', '')
);
$args['config']['signature_version'] = 'anonymous';
} else {
throw new IAE('Credentials must be an instance of '
Expand Down Expand Up @@ -434,13 +440,6 @@ public static function _apply_http_handler($value, array &$args, HandlerList $li
);
}

public static function _default_credentials()
{
return CredentialProvider::resolve(
CredentialProvider::defaultProvider()
);
}

public static function _default_endpoint_provider()
{
return EndpointProvider::defaultProvider();
Expand Down
31 changes: 1 addition & 30 deletions src/CloudSearch/CloudSearchClient.php
Expand Up @@ -2,37 +2,8 @@
namespace Aws\CloudSearch;

use Aws\AwsClient;
use Aws\CloudSearchDomain\CloudSearchDomainClient;

/**
* This client is used to interact with the **Amazon CloudSearch** service.
*/
class CloudSearchClient extends AwsClient
{
/**
* Create a CloudSearchDomainClient for a particular domain to do searching
* and document uploads.
*
* @param string $domainName Name of the CloudSearch domain.
* @param array $config Config options for the CloudSearchDomainClient
*
* @return CloudSearchDomainClient
*/
public function createDomainClient($domainName, array $config = [])
{
$config['endpoint'] = $this->describeDomains([
'DomainNames' => [$domainName]
])->search('DomainStatusList[0].SearchService.Endpoint');

$config += [
'credentials' => $this->getCredentials(),
'scheme' => 'https',
'version' => 'latest'
];

// Create an absolute URI for the endpoint.
$config['endpoint'] = $config['scheme'] . '://' . $config['endpoint'];

return new CloudSearchDomainClient($config);
}
}
class CloudSearchClient extends AwsClient {}

0 comments on commit d25dea0

Please sign in to comment.