Skip to content

Commit ea754bd

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 44b46ba commit ea754bd

8 files changed

+69
-71
lines changed

Input/AddLayerVersionPermissionRequest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,23 @@ public function getVersionNumber(): ?string
128128
return $this->VersionNumber;
129129
}
130130

131-
public function requestBody(): array
131+
public function requestBody(): string
132132
{
133133
$payload = ['Action' => 'AddLayerVersionPermission', 'Version' => '2015-03-31'];
134134

135135
$payload['StatementId'] = $this->StatementId;
136136
$payload['Action'] = $this->Action;
137137
$payload['Principal'] = $this->Principal;
138-
139138
if (null !== $v = $this->OrganizationId) {
140139
$payload['OrganizationId'] = $v;
141140
}
142141

143-
return $payload;
142+
return json_encode($payload);
144143
}
145144

146145
public function requestHeaders(): array
147146
{
148-
$headers = [];
147+
$headers = ['content-type' => 'application/json'];
149148

150149
return $headers;
151150
}

Input/InvocationRequest.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,12 @@ public function getQualifier(): ?string
107107

108108
public function requestBody(): string
109109
{
110-
$document = new \DOMDocument('1.0', 'UTF-8');
111-
$document->formatOutput = true;
112-
113-
if (null !== $input = $this->Payload) {
114-
$document->appendChild($document->createElement('Payload', $input));
115-
}
116-
117-
return $document->saveXML();
110+
return $this->Payload ?? '';
118111
}
119112

120113
public function requestHeaders(): array
121114
{
122-
$headers = [];
115+
$headers = ['content-type' => 'application/json'];
123116
if (null !== $this->InvocationType) {
124117
$headers['X-Amz-Invocation-Type'] = $this->InvocationType;
125118
}

Input/ListLayerVersionsRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ public function getMaxItems(): ?int
7777
return $this->MaxItems;
7878
}
7979

80-
public function requestBody(): array
80+
public function requestBody(): string
8181
{
8282
$payload = ['Action' => 'ListLayerVersions', 'Version' => '2015-03-31'];
8383

84-
return $payload;
84+
return json_encode($payload);
8585
}
8686

8787
public function requestHeaders(): array
8888
{
89-
$headers = [];
89+
$headers = ['content-type' => 'application/json'];
9090

9191
return $headers;
9292
}

Input/PublishLayerVersionRequest.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,47 +95,49 @@ public function getLicenseInfo(): ?string
9595
return $this->LicenseInfo;
9696
}
9797

98-
public function requestBody(): array
98+
public function requestBody(): string
9999
{
100100
$payload = ['Action' => 'PublishLayerVersion', 'Version' => '2015-03-31'];
101-
$indices = new \stdClass();
102101

103102
if (null !== $v = $this->Description) {
104103
$payload['Description'] = $v;
105104
}
106105

107-
(static function ($input) use (&$payload) {
108-
if (null !== $v = $input->getS3Bucket()) {
109-
$payload['Content.S3Bucket'] = $v;
110-
}
111-
if (null !== $v = $input->getS3Key()) {
112-
$payload['Content.S3Key'] = $v;
113-
}
114-
if (null !== $v = $input->getS3ObjectVersion()) {
115-
$payload['Content.S3ObjectVersion'] = $v;
116-
}
117-
if (null !== $v = $input->getZipFile()) {
118-
$payload['Content.ZipFile'] = base64_encode($v);
119-
}
120-
})($this->Content);
121-
(static function ($input) use (&$payload, $indices) {
122-
$indices->k45e4982 = 0;
106+
if (null !== $this->Content) {
107+
(static function (LayerVersionContentInput $input) use (&$payload) {
108+
if (null !== $v = $input->getS3Bucket()) {
109+
$payload['Content']['S3Bucket'] = $v;
110+
}
111+
112+
if (null !== $v = $input->getS3Key()) {
113+
$payload['Content']['S3Key'] = $v;
114+
}
115+
116+
if (null !== $v = $input->getS3ObjectVersion()) {
117+
$payload['Content']['S3ObjectVersion'] = $v;
118+
}
119+
120+
if (null !== $v = $input->getZipFile()) {
121+
$payload['Content']['ZipFile'] = base64_encode($v);
122+
}
123+
})($this->Content);
124+
}
125+
126+
(static function (array $input) use (&$payload) {
123127
foreach ($input as $value) {
124-
++$indices->k45e4982;
125-
$payload["CompatibleRuntimes.{$indices->k45e4982}"] = $value;
128+
$payload['CompatibleRuntimes'][] = $value;
126129
}
127130
})($this->CompatibleRuntimes);
128-
129131
if (null !== $v = $this->LicenseInfo) {
130132
$payload['LicenseInfo'] = $v;
131133
}
132134

133-
return $payload;
135+
return json_encode($payload);
134136
}
135137

136138
public function requestHeaders(): array
137139
{
138-
$headers = [];
140+
$headers = ['content-type' => 'application/json'];
139141

140142
return $headers;
141143
}

Result/AddLayerVersionPermissionResponse.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ public function getStatement(): ?string
3434

3535
protected function populateResult(ResponseInterface $response, HttpClientInterface $httpClient): void
3636
{
37-
$data = new \SimpleXMLElement($response->getContent(false));
38-
$this->Statement = ($v = $data->Statement) ? (string) $v : null;
39-
$this->RevisionId = ($v = $data->RevisionId) ? (string) $v : null;
37+
$data = json_decode($response->getContent(false), true);
38+
39+
$this->Statement = ($v = $data['Statement']) ? (string) $v : null;
40+
$this->RevisionId = ($v = $data['RevisionId']) ? (string) $v : null;
4041
}
4142
}

Result/InvocationResponse.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ protected function populateResult(ResponseInterface $response, HttpClientInterfa
8080
$this->LogResult = $headers['x-amz-log-result'][0] ?? null;
8181
$this->ExecutedVersion = $headers['x-amz-executed-version'][0] ?? null;
8282

83-
$data = new \SimpleXMLElement($response->getContent(false));
84-
$this->StatusCode = ($v = $data->StatusCode) ? (int) (string) $v : null;
85-
$this->Payload = ($v = $data->Payload) ? base64_decode((string) $v) : null;
83+
$data = json_decode($response->getContent(false), true);
84+
85+
$this->StatusCode = ($v = $data['StatusCode']) ? (int) (string) $v : null;
86+
$this->Payload = ($v = $data['Payload']) ? base64_decode((string) $v) : null;
8687
}
8788
}

Result/ListLayerVersionsResponse.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,33 @@ public function getNextMarker(): ?string
122122

123123
protected function populateResult(ResponseInterface $response, HttpClientInterface $httpClient): void
124124
{
125-
$data = new \SimpleXMLElement($response->getContent(false));
126-
$this->NextMarker = ($v = $data->NextMarker) ? (string) $v : null;
127-
$this->LayerVersions = (function (\SimpleXMLElement $xml): array {
125+
$data = json_decode($response->getContent(false), true);
126+
127+
$this->NextMarker = ($v = $data['NextMarker']) ? (string) $v : null;
128+
$this->LayerVersions = (function (array $json): array {
128129
$items = [];
129-
foreach ($xml as $item) {
130+
foreach ($json as $item) {
130131
$items[] = new LayerVersionsListItem([
131-
'LayerVersionArn' => ($v = $item->LayerVersionArn) ? (string) $v : null,
132-
'Version' => ($v = $item->Version) ? (string) $v : null,
133-
'Description' => ($v = $item->Description) ? (string) $v : null,
134-
'CreatedDate' => ($v = $item->CreatedDate) ? (string) $v : null,
135-
'CompatibleRuntimes' => (function (\SimpleXMLElement $xml): array {
132+
'LayerVersionArn' => ($v = $item['LayerVersionArn']) ? (string) $v : null,
133+
'Version' => ($v = $item['Version']) ? (string) $v : null,
134+
'Description' => ($v = $item['Description']) ? (string) $v : null,
135+
'CreatedDate' => ($v = $item['CreatedDate']) ? (string) $v : null,
136+
'CompatibleRuntimes' => (function (array $json): array {
136137
$items = [];
137-
foreach ($xml as $item) {
138+
foreach ($json as $item) {
138139
$a = ($v = $item) ? (string) $v : null;
139140
if (null !== $a) {
140141
$items[] = $a;
141142
}
142143
}
143144

144145
return $items;
145-
})($item->CompatibleRuntimes),
146-
'LicenseInfo' => ($v = $item->LicenseInfo) ? (string) $v : null,
146+
})($item['CompatibleRuntimes']),
147+
'LicenseInfo' => ($v = $item['LicenseInfo']) ? (string) $v : null,
147148
]);
148149
}
149150

150151
return $items;
151-
})($data->LayerVersions);
152+
})($data['LayerVersions']);
152153
}
153154
}

Result/PublishLayerVersionResponse.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,29 @@ public function getVersion(): ?string
111111

112112
protected function populateResult(ResponseInterface $response, HttpClientInterface $httpClient): void
113113
{
114-
$data = new \SimpleXMLElement($response->getContent(false));
114+
$data = json_decode($response->getContent(false), true);
115+
115116
$this->Content = new LayerVersionContentOutput([
116-
'Location' => ($v = $data->Content->Location) ? (string) $v : null,
117-
'CodeSha256' => ($v = $data->Content->CodeSha256) ? (string) $v : null,
118-
'CodeSize' => ($v = $data->Content->CodeSize) ? (string) $v : null,
117+
'Location' => ($v = $data['Content']['Location']) ? (string) $v : null,
118+
'CodeSha256' => ($v = $data['Content']['CodeSha256']) ? (string) $v : null,
119+
'CodeSize' => ($v = $data['Content']['CodeSize']) ? (string) $v : null,
119120
]);
120-
$this->LayerArn = ($v = $data->LayerArn) ? (string) $v : null;
121-
$this->LayerVersionArn = ($v = $data->LayerVersionArn) ? (string) $v : null;
122-
$this->Description = ($v = $data->Description) ? (string) $v : null;
123-
$this->CreatedDate = ($v = $data->CreatedDate) ? (string) $v : null;
124-
$this->Version = ($v = $data->Version) ? (string) $v : null;
125-
$this->CompatibleRuntimes = (function (\SimpleXMLElement $xml): array {
121+
$this->LayerArn = ($v = $data['LayerArn']) ? (string) $v : null;
122+
$this->LayerVersionArn = ($v = $data['LayerVersionArn']) ? (string) $v : null;
123+
$this->Description = ($v = $data['Description']) ? (string) $v : null;
124+
$this->CreatedDate = ($v = $data['CreatedDate']) ? (string) $v : null;
125+
$this->Version = ($v = $data['Version']) ? (string) $v : null;
126+
$this->CompatibleRuntimes = (function (array $json): array {
126127
$items = [];
127-
foreach ($xml as $item) {
128+
foreach ($json as $item) {
128129
$a = ($v = $item) ? (string) $v : null;
129130
if (null !== $a) {
130131
$items[] = $a;
131132
}
132133
}
133134

134135
return $items;
135-
})($data->CompatibleRuntimes);
136-
$this->LicenseInfo = ($v = $data->LicenseInfo) ? (string) $v : null;
136+
})($data['CompatibleRuntimes']);
137+
$this->LicenseInfo = ($v = $data['LicenseInfo']) ? (string) $v : null;
137138
}
138139
}

0 commit comments

Comments
 (0)