Skip to content

Commit

Permalink
Enhancement: Endpointv2 Middleware (#2844)
Browse files Browse the repository at this point in the history
  • Loading branch information
stobrien89 committed Dec 7, 2023
1 parent f2002e5 commit 42949ea
Show file tree
Hide file tree
Showing 20 changed files with 674 additions and 295 deletions.
7 changes: 7 additions & 0 deletions .changes/nextrelease/endpointv2-middleware.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "EndpointV2",
"description": "Adds endpoint resolution middleware and refactors endpoint resolution logic"
}
]
16 changes: 4 additions & 12 deletions src/Api/Serializer/JsonRpcSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use Aws\Api\Service;
use Aws\CommandInterface;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\EndpointV2\EndpointV2SerializerTrait;
use Aws\EndpointV2\Ruleset\RulesetEndpoint;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -56,8 +56,7 @@ public function __construct(
*/
public function __invoke(
CommandInterface $command,
$endpointProvider = null,
$clientArgs = null
$endpoint = null
)
{
$operationName = $command->getName();
Expand All @@ -68,15 +67,8 @@ public function __invoke(
'Content-Type' => $this->contentType
];

if ($endpointProvider instanceof EndpointProviderV2) {
$this->setRequestOptions(
$endpointProvider,
$command,
$operation,
$commandArgs,
$clientArgs,
$headers
);
if ($endpoint instanceof RulesetEndpoint) {
$this->setEndpointV2RequestOptions($endpoint, $headers);
}

return new Request(
Expand Down
15 changes: 4 additions & 11 deletions src/Api/Serializer/QuerySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aws\CommandInterface;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\EndpointV2\EndpointV2SerializerTrait;
use Aws\EndpointV2\Ruleset\RulesetEndpoint;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -42,8 +43,7 @@ public function __construct(
*/
public function __invoke(
CommandInterface $command,
$endpointProvider = null,
$clientArgs = null
$endpoint = null
)
{
$operation = $this->api->getOperation($command->getName());
Expand All @@ -67,15 +67,8 @@ public function __invoke(
'Content-Type' => 'application/x-www-form-urlencoded'
];

if ($endpointProvider instanceof EndpointProviderV2) {
$this->setRequestOptions(
$endpointProvider,
$command,
$operation,
$commandArgs,
$clientArgs,
$headers
);
if ($endpoint instanceof RulesetEndpoint) {
$this->setEndpointV2RequestOptions($endpoint, $headers);
}

return new Request(
Expand Down
23 changes: 8 additions & 15 deletions src/Api/Serializer/RestSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Aws\CommandInterface;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\EndpointV2\EndpointV2SerializerTrait;
use Aws\EndpointV2\Ruleset\RulesetEndpoint;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
Expand Down Expand Up @@ -49,33 +50,25 @@ public function __construct(Service $api, $endpoint)
*/
public function __invoke(
CommandInterface $command,
$endpointProvider = null,
$clientArgs = null
$endpoint = null
)
{
$operation = $this->api->getOperation($command->getName());
$commandArgs = $command->toArray();
$opts = $this->serialize($operation, $commandArgs);
$headers = isset($opts['headers']) ? $opts['headers'] : [];

if ($endpointProvider instanceof EndpointProviderV2) {
$this->setRequestOptions(
$endpointProvider,
$command,
$operation,
$commandArgs,
$clientArgs,
$headers
);
$this->endpoint = new Uri($this->endpoint);
$headers = $opts['headers'] ?? [];

if ($endpoint instanceof RulesetEndpoint) {
$this->setEndpointV2RequestOptions($endpoint, $headers);
}

$uri = $this->buildEndpoint($operation, $commandArgs, $opts);

return new Request(
$operation['http']['method'],
$uri,
$headers,
isset($opts['body']) ? $opts['body'] : null
$opts['body'] ?? null
);
}

Expand Down
25 changes: 11 additions & 14 deletions src/AwsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Aws\Api\Service;
use Aws\EndpointDiscovery\EndpointDiscoveryMiddleware;
use Aws\EndpointV2\EndpointProviderV2;
use Aws\EndpointV2\EndpointV2Middleware;
use Aws\Exception\AwsException;
use Aws\Signature\SignatureProvider;
use GuzzleHttp\Psr7\Uri;
Expand Down Expand Up @@ -240,7 +241,9 @@ public function __construct(array $args)
$this->loadAliases();
$this->addStreamRequestPayload();
$this->addRecursionDetection();
$this->addRequestBuilder();
if ($this->isUseEndpointV2()) {
$this->addEndpointV2Middleware();
}

if (!is_null($this->api->getMetadata('awsQueryCompatible'))) {
$this->addQueryCompatibleInputMiddleware($this->api);
Expand Down Expand Up @@ -516,24 +519,18 @@ private function addRecursionDetection()
);
}

/**
* Adds the `builder` middleware such that a client's endpoint
* provider and endpoint resolution arguments can be passed.
*/
private function addRequestBuilder()
private function addEndpointV2Middleware()
{
$handlerList = $this->getHandlerList();
$serializer = $this->serializer;
$endpointProvider = $this->endpointProvider;
$list = $this->getHandlerList();
$endpointArgs = $this->getEndpointProviderArgs();

$handlerList->prependBuild(
Middleware::requestBuilder(
$serializer,
$endpointProvider,
$list->prependBuild(
EndpointV2Middleware::wrap(
$this->endpointProvider,
$this->getApi(),
$endpointArgs
),
'builderV2'
'endpoint-resolution'
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/ClientResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class ClientResolver
],
'serializer' => [
'default' => [__CLASS__, '_default_serializer'],
'fn' => [__CLASS__, '_apply_serializer'],
'internal' => true,
'type' => 'value',
'valid' => ['callable'],
Expand Down Expand Up @@ -834,6 +835,11 @@ public static function _default_use_dual_stack_endpoint(array &$args) {
return UseDualStackConfigProvider::defaultProvider($args);
}

public static function _apply_serializer($value, array &$args, HandlerList $list)
{
$list->prependBuild(Middleware::requestBuilder($value), 'builder');
}

public static function _apply_debug($value, array &$args, HandlerList $list)
{
if ($value !== false) {
Expand Down
Loading

0 comments on commit 42949ea

Please sign in to comment.