| 1 |
<?php |
| 2 |
namespace Aws\Api\Parser; |
| 3 |
|
| 4 |
use Aws\Api\Operation; |
| 5 |
use Aws\Api\StructureShape; |
| 6 |
use Aws\Api\Service; |
| 7 |
use Aws\Result; |
| 8 |
use Aws\CommandInterface; |
| 9 |
use Psr\Http\Message\ResponseInterface; |
| 10 |
use Psr\Http\Message\StreamInterface; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class JsonRpcParser extends AbstractParser |
| 16 |
{ |
| 17 |
use PayloadParserTrait; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function __construct(Service $api, ?JsonParser $parser = null) |
| 24 |
{ |
| 25 |
parent::__construct($api); |
| 26 |
$this->parser = $parser ?: new JsonParser(); |
| 27 |
} |
| 28 |
|
| 29 |
public function __invoke( |
| 30 |
CommandInterface $command, |
| 31 |
ResponseInterface $response |
| 32 |
) { |
| 33 |
$operation = $this->api->getOperation($command->getName()); |
| 34 |
|
| 35 |
return $this->parseResponse($response, $operation); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private function parseResponse(ResponseInterface $response, Operation $operation) |
| 48 |
{ |
| 49 |
if (null === $operation['output']) { |
| 50 |
return new Result([]); |
| 51 |
} |
| 52 |
|
| 53 |
$outputShape = $operation->getOutput(); |
| 54 |
foreach ($outputShape->getMembers() as $memberName => $memberProps) { |
| 55 |
if (!empty($memberProps['eventstream'])) { |
| 56 |
return new Result([ |
| 57 |
$memberName => new EventParsingIterator( |
| 58 |
$response->getBody(), |
| 59 |
$outputShape->getMember($memberName), |
| 60 |
$this |
| 61 |
) |
| 62 |
]); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$body = $response->getBody(); |
| 67 |
if ($body->isSeekable()) { |
| 68 |
$body->rewind(); |
| 69 |
} |
| 70 |
|
| 71 |
$result = $this->parseMemberFromStream( |
| 72 |
$body, |
| 73 |
$operation->getOutput(), |
| 74 |
$response |
| 75 |
); |
| 76 |
|
| 77 |
return new Result(is_null($result) ? [] : $result); |
| 78 |
} |
| 79 |
|
| 80 |
public function parseMemberFromStream( |
| 81 |
StreamInterface $stream, |
| 82 |
StructureShape $member, |
| 83 |
$response |
| 84 |
) { |
| 85 |
return $this->parser->parse($member, $this->parseJson($stream, $response)); |
| 86 |
} |
| 87 |
} |