| 1 |
<?php |
| 2 |
namespace Aws\Api\Parser; |
| 3 |
|
| 4 |
use Aws\Api\Operation; |
| 5 |
use Aws\Api\Parser\Exception\ParserException; |
| 6 |
use Aws\Result; |
| 7 |
use Aws\CommandInterface; |
| 8 |
use Psr\Http\Message\ResponseInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
abstract class AbstractRpcV2Parser extends AbstractParser |
| 20 |
{ |
| 21 |
private const HEADER_SMITHY_PROTOCOL = 'Smithy-Protocol'; |
| 22 |
|
| 23 |
|
| 24 |
protected static string $smithyProtocol; |
| 25 |
|
| 26 |
public function __invoke( |
| 27 |
CommandInterface $command, |
| 28 |
ResponseInterface $response |
| 29 |
) { |
| 30 |
$operation = $this->api->getOperation($command->getName()); |
| 31 |
|
| 32 |
return $this->parseResponse($response, $operation); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private function parseResponse( |
| 45 |
ResponseInterface $response, |
| 46 |
Operation $operation |
| 47 |
): Result |
| 48 |
{ |
| 49 |
$smithyProtocolHeader = $response->getHeaderLine(self::HEADER_SMITHY_PROTOCOL); |
| 50 |
if ($smithyProtocolHeader !== static::$smithyProtocol) { |
| 51 |
$statusCode = $response->getStatusCode(); |
| 52 |
throw new ParserException( |
| 53 |
"Malformed response: Smithy-Protocol header mismatch (HTTP {$statusCode}). " |
| 54 |
. 'Expected ' . static::$smithyProtocol |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
if ($operation['output'] === null) { |
| 59 |
return new Result([]); |
| 60 |
} |
| 61 |
|
| 62 |
$outputShape = $operation->getOutput(); |
| 63 |
foreach ($outputShape->getMembers() as $memberName => $memberProps) { |
| 64 |
if (!empty($memberProps['eventstream'])) { |
| 65 |
return new Result([ |
| 66 |
$memberName => new EventParsingIterator( |
| 67 |
$response->getBody(), |
| 68 |
$outputShape->getMember($memberName), |
| 69 |
$this |
| 70 |
) |
| 71 |
]); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$result = $this->parseMemberFromStream( |
| 76 |
$response->getBody(), |
| 77 |
$outputShape, |
| 78 |
$response |
| 79 |
); |
| 80 |
|
| 81 |
return new Result(is_null($result) ? [] : $result); |
| 82 |
} |
| 83 |
} |