Skip to content

Commit f763ae5

Browse files
authored
Add Input::request() (#303)
* Add Input::request() * Move Request away for Signer namespace * Fixed tests * cs * Format headers properly * cs * Rename private function * Update required core version * Updated date format * Better strigify date * cs
1 parent ddbc6f1 commit f763ae5

12 files changed

+190
-219
lines changed

src/AbstractApi.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
use AsyncAws\Core\Credentials\InstanceProvider;
1313
use AsyncAws\Core\Credentials\WebIdentityProvider;
1414
use AsyncAws\Core\Exception\InvalidArgument;
15-
use AsyncAws\Core\Signer\Request;
1615
use AsyncAws\Core\Signer\Signer;
1716
use AsyncAws\Core\Signer\SignerV4;
18-
use AsyncAws\Core\Stream\StreamFactory;
1917
use AsyncAws\Core\Stream\StringStream;
2018
use Psr\Log\LoggerInterface;
2119
use Psr\Log\NullLogger;
@@ -88,15 +86,10 @@ abstract protected function getSignatureVersion(): string;
8886

8987
abstract protected function getSignatureScopeName(): string;
9088

91-
/**
92-
* @param string[]|string[][] $headers headers names provided as keys or as part of values
93-
* @param string|resource|callable|iterable|null $body
94-
*/
95-
final protected function getResponse(string $method, $body, array $headers, string $endpoint): ResponseInterface
89+
final protected function getResponse(Request $request): ResponseInterface
9690
{
97-
$stream = StreamFactory::create($body);
91+
$request->setEndpoint($this->getEndpoint($request->getUri(), $request->getQuery()));
9892

99-
$request = new Request($method, $endpoint, $headers, $stream);
10093
$this->getSigner()->sign($request, $this->credentialProvider->getCredentials($this->configuration));
10194

10295
$length = $request->getBody()->length();
@@ -110,7 +103,19 @@ final protected function getResponse(string $method, $body, array $headers, stri
110103
$requestBody = $requestBody->stringify();
111104
}
112105

113-
return $this->httpClient->request($request->getMethod(), $request->getUrl(), ['headers' => $request->getHeaders(), 'body' => 0 === $length ? null : $requestBody]);
106+
return $this->httpClient->request($request->getMethod(), $request->getEndpoint(), ['headers' => $request->getHeaders(), 'body' => 0 === $length ? null : $requestBody]);
107+
}
108+
109+
/**
110+
* @return callable[]
111+
*/
112+
protected function getSignerFactories(): array
113+
{
114+
return [
115+
'v4' => static function (string $service, string $region) {
116+
return new SignerV4($service, $region);
117+
},
118+
];
114119
}
115120

116121
/**
@@ -119,7 +124,7 @@ final protected function getResponse(string $method, $body, array $headers, stri
119124
* @param string $uri or path
120125
* @param array $query parameters that should go in the query string
121126
*/
122-
protected function getEndpoint(string $uri, array $query): string
127+
private function getEndpoint(string $uri, array $query): string
123128
{
124129
/** @psalm-suppress PossiblyNullArgument */
125130
$endpoint = strtr($this->configuration->get('endpoint'), [
@@ -134,18 +139,6 @@ protected function getEndpoint(string $uri, array $query): string
134139
return $endpoint . (false === \strpos($endpoint, '?') ? '?' : '&') . http_build_query($query);
135140
}
136141

137-
/**
138-
* @return callable[]
139-
*/
140-
protected function getSignerFactories(): array
141-
{
142-
return [
143-
'v4' => static function (string $service, string $region) {
144-
return new SignerV4($service, $region);
145-
},
146-
];
147-
}
148-
149142
private function getSigner()
150143
{
151144
if (null === $this->signer) {

src/Signer/Request.php renamed to src/Request.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22

3-
namespace AsyncAws\Core\Signer;
3+
namespace AsyncAws\Core;
44

5+
use AsyncAws\Core\Exception\LogicException;
56
use AsyncAws\Core\Stream\Stream;
67

78
/**
8-
* Dummy object to store a Request.
9+
* Representation of a AWS Request.
910
*
1011
* @author Jérémy Derussé <[email protected]>
1112
*
@@ -15,33 +16,39 @@ final class Request
1516
{
1617
private $method;
1718

18-
private $url;
19+
private $uri;
1920

2021
private $headers;
2122

2223
private $body;
2324

25+
private $query;
26+
27+
private $endpoint;
28+
2429
/**
2530
* @param string[]|string[][] $headers
2631
*/
27-
public function __construct(string $method, string $url, array $headers, Stream $body)
32+
public function __construct(string $method, string $uri, array $query, array $headers, Stream $body)
2833
{
2934
$this->method = $method;
30-
$this->url = $url;
35+
$this->uri = $uri;
3136
foreach ($headers as $key => $value) {
3237
$this->headers[\strtolower($key)] = $value;
3338
}
3439
$this->body = $body;
40+
$this->query = $query;
41+
$this->endpoint = '';
3542
}
3643

3744
public function getMethod(): string
3845
{
3946
return $this->method;
4047
}
4148

42-
public function getUrl(): string
49+
public function getUri(): string
4350
{
44-
return $this->url;
51+
return $this->uri;
4552
}
4653

4754
public function hasHeader($name): bool
@@ -76,4 +83,22 @@ public function setBody(Stream $body)
7683
{
7784
$this->body = $body;
7885
}
86+
87+
public function getQuery(): array
88+
{
89+
return $this->query;
90+
}
91+
92+
public function getEndpoint(): string
93+
{
94+
return $this->endpoint;
95+
}
96+
97+
public function setEndpoint(string $endpoint): void
98+
{
99+
if (!empty($this->endpoint)) {
100+
throw new LogicException('Request::$endpoint cannot be changed after it has a value.');
101+
}
102+
$this->endpoint = $endpoint;
103+
}
79104
}

src/Signer/Signer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AsyncAws\Core\Signer;
44

55
use AsyncAws\Core\Credentials\Credentials;
6+
use AsyncAws\Core\Request;
67

78
/**
89
* Interface for signing a request.

src/Signer/SignerV4.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AsyncAws\Core\Credentials\Credentials;
66
use AsyncAws\Core\Exception\InvalidArgument;
7+
use AsyncAws\Core\Request;
78
use AsyncAws\Core\Stream\FixedSizeStream;
89
use AsyncAws\Core\Stream\IterableStream;
910
use AsyncAws\Core\Stream\Stream;
@@ -61,8 +62,8 @@ public function sign(Request $request, ?Credentials $credentials): void
6162
return;
6263
}
6364

64-
if (false === $parsedUrl = parse_url($request->getUrl())) {
65-
throw new InvalidArgument(sprintf('The endpoint "%s" is invalid.', $request->getUrl()));
65+
if (false === $parsedUrl = parse_url($request->getEndpoint())) {
66+
throw new InvalidArgument(sprintf('The endpoint "%s" is invalid.', $request->getEndpoint()));
6667
}
6768
if (null !== $sessionToken = $credentials->getSessionToken()) {
6869
$request->setHeader('x-amz-security-token', $sessionToken);

src/Stream/StreamFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
/**
88
* Create Streams.
99
*
10-
* @internal
11-
*
1210
* @author Jérémy Derussé <[email protected]>
1311
*/
1412
class StreamFactory

src/Sts/Input/AssumeRoleRequest.php

Lines changed: 71 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace AsyncAws\Core\Sts\Input;
44

55
use AsyncAws\Core\Exception\InvalidArgument;
6+
use AsyncAws\Core\Request;
7+
use AsyncAws\Core\Stream\StreamFactory;
68

79
class AssumeRoleRequest
810
{
@@ -202,94 +204,19 @@ public function getTransitiveTagKeys(): array
202204
/**
203205
* @internal
204206
*/
205-
public function requestBody(): string
206-
{
207-
$payload = ['Action' => 'AssumeRole', 'Version' => '2011-06-15'];
208-
$indices = new \stdClass();
209-
$payload['RoleArn'] = $this->RoleArn;
210-
$payload['RoleSessionName'] = $this->RoleSessionName;
211-
212-
(static function (array $input) use (&$payload, $indices) {
213-
$indices->kfc822c1 = 0;
214-
foreach ($input as $value) {
215-
++$indices->kfc822c1;
216-
217-
if (null !== $value) {
218-
(static function (PolicyDescriptorType $input) use (&$payload, $indices) {
219-
if (null !== $v = $input->getarn()) {
220-
$payload["PolicyArns.member.{$indices->kfc822c1}.arn"] = $v;
221-
}
222-
})($value);
223-
}
224-
}
225-
})($this->PolicyArns);
226-
if (null !== $v = $this->Policy) {
227-
$payload['Policy'] = $v;
228-
}
229-
if (null !== $v = $this->DurationSeconds) {
230-
$payload['DurationSeconds'] = $v;
231-
}
232-
233-
(static function (array $input) use (&$payload, $indices) {
234-
$indices->k26dfc14 = 0;
235-
foreach ($input as $value) {
236-
++$indices->k26dfc14;
237-
238-
if (null !== $value) {
239-
(static function (Tag $input) use (&$payload, $indices) {
240-
$payload["Tags.member.{$indices->k26dfc14}.Key"] = $input->getKey();
241-
$payload["Tags.member.{$indices->k26dfc14}.Value"] = $input->getValue();
242-
})($value);
243-
}
244-
}
245-
})($this->Tags);
246-
247-
(static function (array $input) use (&$payload, $indices) {
248-
$indices->k0ec5280 = 0;
249-
foreach ($input as $value) {
250-
++$indices->k0ec5280;
251-
$payload["TransitiveTagKeys.member.{$indices->k0ec5280}"] = $value;
252-
}
253-
})($this->TransitiveTagKeys);
254-
if (null !== $v = $this->ExternalId) {
255-
$payload['ExternalId'] = $v;
256-
}
257-
if (null !== $v = $this->SerialNumber) {
258-
$payload['SerialNumber'] = $v;
259-
}
260-
if (null !== $v = $this->TokenCode) {
261-
$payload['TokenCode'] = $v;
262-
}
263-
264-
return http_build_query($payload, '', '&', \PHP_QUERY_RFC1738);
265-
}
266-
267-
/**
268-
* @internal
269-
*/
270-
public function requestHeaders(): array
207+
public function request(): Request
271208
{
209+
// Prepare headers
272210
$headers = ['content-type' => 'application/x-www-form-urlencoded'];
273211

274-
return $headers;
275-
}
276-
277-
/**
278-
* @internal
279-
*/
280-
public function requestQuery(): array
281-
{
212+
// Prepare query
282213
$query = [];
283214

284-
return $query;
285-
}
215+
// Prepare URI
216+
$uriString = '/';
286217

287-
/**
288-
* @internal
289-
*/
290-
public function requestUri(): string
291-
{
292-
return '/';
218+
// Return the Request
219+
return new Request('POST', $uriString, $query, $headers, StreamFactory::create($this->requestBody()));
293220
}
294221

295222
public function setDurationSeconds(?int $value): self
@@ -389,4 +316,66 @@ public function validate(): void
389316
$item->validate();
390317
}
391318
}
319+
320+
private function requestBody(): string
321+
{
322+
$payload = ['Action' => 'AssumeRole', 'Version' => '2011-06-15'];
323+
$indices = new \stdClass();
324+
$payload['RoleArn'] = $this->RoleArn;
325+
$payload['RoleSessionName'] = $this->RoleSessionName;
326+
327+
(static function (array $input) use (&$payload, $indices) {
328+
$indices->kfc822c1 = 0;
329+
foreach ($input as $value) {
330+
++$indices->kfc822c1;
331+
332+
if (null !== $value) {
333+
(static function (PolicyDescriptorType $input) use (&$payload, $indices) {
334+
if (null !== $v = $input->getarn()) {
335+
$payload["PolicyArns.member.{$indices->kfc822c1}.arn"] = $v;
336+
}
337+
})($value);
338+
}
339+
}
340+
})($this->PolicyArns);
341+
if (null !== $v = $this->Policy) {
342+
$payload['Policy'] = $v;
343+
}
344+
if (null !== $v = $this->DurationSeconds) {
345+
$payload['DurationSeconds'] = $v;
346+
}
347+
348+
(static function (array $input) use (&$payload, $indices) {
349+
$indices->k26dfc14 = 0;
350+
foreach ($input as $value) {
351+
++$indices->k26dfc14;
352+
353+
if (null !== $value) {
354+
(static function (Tag $input) use (&$payload, $indices) {
355+
$payload["Tags.member.{$indices->k26dfc14}.Key"] = $input->getKey();
356+
$payload["Tags.member.{$indices->k26dfc14}.Value"] = $input->getValue();
357+
})($value);
358+
}
359+
}
360+
})($this->Tags);
361+
362+
(static function (array $input) use (&$payload, $indices) {
363+
$indices->k0ec5280 = 0;
364+
foreach ($input as $value) {
365+
++$indices->k0ec5280;
366+
$payload["TransitiveTagKeys.member.{$indices->k0ec5280}"] = $value;
367+
}
368+
})($this->TransitiveTagKeys);
369+
if (null !== $v = $this->ExternalId) {
370+
$payload['ExternalId'] = $v;
371+
}
372+
if (null !== $v = $this->SerialNumber) {
373+
$payload['SerialNumber'] = $v;
374+
}
375+
if (null !== $v = $this->TokenCode) {
376+
$payload['TokenCode'] = $v;
377+
}
378+
379+
return http_build_query($payload, '', '&', \PHP_QUERY_RFC1738);
380+
}
392381
}

0 commit comments

Comments
 (0)