forked from aws/aws-sdk-php
/
AbstractRestParser.php
142 lines (127 loc) · 4.05 KB
/
AbstractRestParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Aws\Api\Parser;
use Aws\Api\DateTimeResult;
use Aws\Api\Shape;
use Aws\Api\StructureShape;
use Aws\Result;
use Aws\CommandInterface;
use Psr\Http\Message\ResponseInterface;
/**
* @internal
*/
abstract class AbstractRestParser extends AbstractParser
{
/**
* Parses a payload from a response.
*
* @param ResponseInterface $response Response to parse.
* @param StructureShape $member Member to parse
* @param array $result Result value
*
* @return mixed
*/
abstract protected function payload(
ResponseInterface $response,
StructureShape $member,
array &$result
);
public function __invoke(
CommandInterface $command,
ResponseInterface $response
) {
$output = $this->api->getOperation($command->getName())->getOutput();
$result = [];
if ($payload = $output['payload']) {
$this->extractPayload($payload, $output, $response, $result);
}
foreach ($output->getMembers() as $name => $member) {
switch ($member['location']) {
case 'header':
$this->extractHeader($name, $member, $response, $result);
break;
case 'headers':
$this->extractHeaders($name, $member, $response, $result);
break;
case 'statusCode':
$this->extractStatus($name, $response, $result);
break;
}
}
if (!$payload && $response->getBody()->getSize() > 0) {
// if no payload was found, then parse the contents of the body
$this->payload($response, $output, $result);
}
return new Result($result);
}
private function extractPayload(
$payload,
StructureShape $output,
ResponseInterface $response,
array &$result
) {
$member = $output->getMember($payload);
if ($member instanceof StructureShape) {
// Structure members parse top-level data into a specific key.
$result[$payload] = [];
$this->payload($response, $member, $result[$payload]);
} else {
// Streaming data is just the stream from the response body.
$result[$payload] = $response->getBody();
}
}
/**
* Extract a single header from the response into the result.
*/
private function extractHeader(
$name,
Shape $shape,
ResponseInterface $response,
&$result
) {
$value = $response->getHeaderLine($shape['locationName'] ?: $name);
$type = $shape->getType();
if ($type === 'blob') {
$value = base64_decode($value);
} elseif ($type === 'timestamp') {
try {
$value = new DateTimeResult($value);
} catch (\Exception $e) {
// If the value cannot be parsed, then do not add it to the
// output structure.
return;
}
}
$result[$name] = $value;
}
/**
* Extract a map of headers with an optional prefix from the response.
*/
private function extractHeaders(
$name,
Shape $shape,
ResponseInterface $response,
&$result
) {
// Check if the headers are prefixed by a location name
$result[$name] = [];
$prefix = $shape['locationName'];
$prefixLen = strlen($prefix);
foreach ($response->getHeaders() as $k => $values) {
if (!$prefixLen) {
$result[$name][$k] = implode(', ', $values);
} elseif (stripos($k, $prefix) === 0) {
$result[$name][substr($k, $prefixLen)] = implode(', ', $values);
}
}
}
/**
* Places the status code of the response into the result array.
*/
private function extractStatus(
$name,
ResponseInterface $response,
array &$result
) {
$result[$name] = (int) $response->getStatusCode();
}
}