Skip to content

Commit

Permalink
Using rules endpoint provider rather than list of endpoints in a desc…
Browse files Browse the repository at this point in the history
…ription
  • Loading branch information
mtdowling committed Jan 27, 2015
1 parent 2d120e4 commit a7aaf90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 60 deletions.
27 changes: 12 additions & 15 deletions src/Aws/Common/Client/AbstractClient.php
Expand Up @@ -18,10 +18,10 @@

use Aws\Common\Aws;
use Aws\Common\Credentials\CredentialsInterface;
use Aws\Common\Credentials\NullCredentials;
use Aws\Common\Enum\ClientOptions as Options;
use Aws\Common\Exception\InvalidArgumentException;
use Aws\Common\Exception\TransferException;
use Aws\Common\RulesEndpointProvider;
use Aws\Common\Signature\EndpointSignatureInterface;
use Aws\Common\Signature\SignatureInterface;
use Aws\Common\Signature\SignatureListener;
Expand Down Expand Up @@ -111,21 +111,18 @@ public function __call($method, $args)
*/
public static function getEndpoint(ServiceDescriptionInterface $description, $region, $scheme)
{
$service = $description->getData('serviceFullName');
// Lookup the region in the service description
if (!($regions = $description->getData('regions'))) {
throw new InvalidArgumentException("No regions found in the {$service} description");
}
// Ensure that the region exists for the service
if (!isset($regions[$region])) {
throw new InvalidArgumentException("{$region} is not a valid region for {$service}");
}
// Ensure that the scheme is valid
if ($regions[$region][$scheme] == false) {
throw new InvalidArgumentException("{$scheme} is not a valid URI scheme for {$service} in {$region}");
try {
$service = $description->getData('endpointPrefix');
$provider = RulesEndpointProvider::fromDefaults();
$result = $provider(array(
'service' => $service,
'region' => $region,
'scheme' => $scheme
));
return $result['endpoint'];
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), 0, $e);
}

return $scheme . '://' . $regions[$region]['hostname'];
}

public function getCredentials()
Expand Down
48 changes: 3 additions & 45 deletions tests/Aws/Tests/Common/Client/AbstractClientTest.php
Expand Up @@ -213,53 +213,11 @@ public function testAllowsMagicIterators()
$client->getFooIterator(array('baz' => 'bar'));
}

/**
* @expectedException \Aws\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage No regions
*/
public function testEnsuresRegionsAreSetWhenCreatingEndpoints()
{
AbstractClient::getEndpoint(ServiceDescription::factory(array()), 'foo', 'baz');
}

/**
* @expectedException \Aws\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage foo is not a valid region
*/
public function testEnsuresRegionIsValidWhenCreatingEndpoints()
{
AbstractClient::getEndpoint(ServiceDescription::factory(array(
'regions' => array(
'baz' => array()
)
)), 'foo', 'baz');
}

/**
* @expectedException \Aws\Common\Exception\InvalidArgumentException
* @expectedExceptionMessage http is not a valid URI scheme for
*/
public function testEnsuresSchemeIsValidWhenCreatingEndpoints()
{
AbstractClient::getEndpoint(ServiceDescription::factory(array(
'regions' => array(
'baz' => array(
'http' => false
)
)
)), 'baz', 'http');
}

public function testCreatesEndpoints()
{
$this->assertEquals('http://test.com', AbstractClient::getEndpoint(ServiceDescription::factory(array(
'regions' => array(
'baz' => array(
'http' => true,
'hostname' => 'test.com'
)
)
)), 'baz', 'http'));
$this->assertEquals('http://s3.amazonaws.com', AbstractClient::getEndpoint(ServiceDescription::factory(array(
'endpointPrefix' => 's3',
)), 'us-east-1', 'http'));
}

public function testChangeRegionAndCredentialsEvents()
Expand Down

0 comments on commit a7aaf90

Please sign in to comment.