Skip to content

Commit dd9d51a

Browse files
authored
Better generate requestBody (#174)
* Better generate request body and parse responses * Delete xml.php * Added some type hints * CS * CS * cs * CS
1 parent e9b1363 commit dd9d51a

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

AbstractApi.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,16 @@ abstract protected function getSignatureVersion(): string;
9999
abstract protected function getSignatureScopeName(): string;
100100

101101
/**
102-
* @param string[]|string[][] $headers headers names provided as keys or as part of values
103-
* @param array|string|resource|callable $body
102+
* @param string[]|string[][] $headers headers names provided as keys or as part of values
103+
* @param string|resource|callable $body
104104
*/
105105
final protected function getResponse(string $method, $body, $headers = [], ?string $endpoint = null): ResponseInterface
106106
{
107-
[$contentType, $body] = $this->stringifyBody($body);
107+
$body = $this->stringifyBody($body);
108108
if (!isset($headers['content-type'])) {
109-
$headers['content-type'] = $contentType;
110-
}
111-
if (empty($body)) {
112-
$headers['content-length'] = '0';
109+
$headers['content-type'] = 'text/plain';
113110
}
111+
$headers['content-length'] = (string) \strlen($body);
114112

115113
$request = new Request($method, $this->fillEndpoint($endpoint), $headers, $body);
116114
$this->getSigner()->Sign($request, $this->credentialProvider->getCredentials($this->configuration));
@@ -148,14 +146,12 @@ protected function getSignerFactories(): array
148146
];
149147
}
150148

151-
private function stringifyBody($body): array
149+
private function stringifyBody($body): string
152150
{
153151
if (\is_callable($body)) {
154152
return $this->stringifyBody($body());
155153
}
156-
if (\is_array($body)) {
157-
return ['application/x-www-form-urlencoded', http_build_query($body, '', '&', \PHP_QUERY_RFC1738)];
158-
}
154+
159155
if (\is_resource($body)) {
160156
if (!stream_get_meta_data($body)['seekable']) {
161157
throw new InvalidArgument(sprintf('The give body is not seekable. Therefor request can not be signed.'));
@@ -168,7 +164,7 @@ private function stringifyBody($body): array
168164
}
169165
}
170166

171-
return ['text/plain', (string) $body];
167+
return (string) $body;
172168
}
173169

174170
private function getSigner()

Sts/Input/AssumeRoleRequest.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,25 @@ public function getTransitiveTagKeys(): array
190190
return $this->TransitiveTagKeys;
191191
}
192192

193-
public function requestBody(): array
193+
public function requestBody(): string
194194
{
195195
$payload = ['Action' => 'AssumeRole', 'Version' => '2011-06-15'];
196196
$indices = new \stdClass();
197197
$payload['RoleArn'] = $this->RoleArn;
198198
$payload['RoleSessionName'] = $this->RoleSessionName;
199-
(static function ($input) use (&$payload, $indices) {
199+
200+
(static function (array $input) use (&$payload, $indices) {
200201
$indices->kd8fbed2 = 0;
201202
foreach ($input as $value) {
202203
++$indices->kd8fbed2;
203-
(static function ($input) use (&$payload, $indices) {
204-
if (null !== $v = $input->getarn()) {
205-
$payload["PolicyArns.{$indices->kd8fbed2}.arn"] = $v;
206-
}
207-
})($value);
204+
205+
if (null !== $value) {
206+
(static function (PolicyDescriptorType $input) use (&$payload, $indices) {
207+
if (null !== $v = $input->getarn()) {
208+
$payload["PolicyArns.{$indices->kd8fbed2}.arn"] = $v;
209+
}
210+
})($value);
211+
}
208212
}
209213
})($this->PolicyArns);
210214

@@ -216,17 +220,21 @@ public function requestBody(): array
216220
$payload['DurationSeconds'] = $v;
217221
}
218222

219-
(static function ($input) use (&$payload, $indices) {
223+
(static function (array $input) use (&$payload, $indices) {
220224
$indices->k848eed0 = 0;
221225
foreach ($input as $value) {
222226
++$indices->k848eed0;
223-
(static function ($input) use (&$payload, $indices) {
224-
$payload["Tags.{$indices->k848eed0}.Key"] = $input->getKey();
225-
$payload["Tags.{$indices->k848eed0}.Value"] = $input->getValue();
226-
})($value);
227+
228+
if (null !== $value) {
229+
(static function (Tag $input) use (&$payload, $indices) {
230+
$payload["Tags.{$indices->k848eed0}.Key"] = $input->getKey();
231+
$payload["Tags.{$indices->k848eed0}.Value"] = $input->getValue();
232+
})($value);
233+
}
227234
}
228235
})($this->Tags);
229-
(static function ($input) use (&$payload, $indices) {
236+
237+
(static function (array $input) use (&$payload, $indices) {
230238
$indices->k296eb4e = 0;
231239
foreach ($input as $value) {
232240
++$indices->k296eb4e;
@@ -246,12 +254,12 @@ public function requestBody(): array
246254
$payload['TokenCode'] = $v;
247255
}
248256

249-
return $payload;
257+
return http_build_query($payload, '', '&', \PHP_QUERY_RFC1738);
250258
}
251259

252260
public function requestHeaders(): array
253261
{
254-
$headers = [];
262+
$headers = ['content-type' => 'application/x-www-form-urlencoded'];
255263

256264
return $headers;
257265
}

Sts/Input/GetCallerIdentityRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ public static function create($input): self
99
return $input instanceof self ? $input : new self();
1010
}
1111

12-
public function requestBody(): array
12+
public function requestBody(): string
1313
{
1414
$payload = ['Action' => 'GetCallerIdentity', 'Version' => '2011-06-15'];
1515

16-
return $payload;
16+
return http_build_query($payload, '', '&', \PHP_QUERY_RFC1738);
1717
}
1818

1919
public function requestHeaders(): array
2020
{
21-
$headers = [];
21+
$headers = ['content-type' => 'application/x-www-form-urlencoded'];
2222

2323
return $headers;
2424
}

0 commit comments

Comments
 (0)