Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Parser/AbstractRpcV2Parser.php

raw symbols SCIP
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 * Base implementation for Smithy RPC V2 protocol parsers.
12 *
13 * Implementers MUST define the following static property representing
14 * the `Smithy-Protocol` header value:
15 * self::HEADER_SMITHY_PROTOCOL => static::$smithyProtocol
16 *
17 * @internal
18 */
19 abstract class AbstractRpcV2Parser extends AbstractParser
20 {
21 private const HEADER_SMITHY_PROTOCOL = 'Smithy-Protocol';
22
23 /** @var string */
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 * Parses a response according to Smithy RPC V2 protocol standards.
37 *
38 * @param ResponseInterface $response the response to parse.
39 * @param Operation $operation the operation which holds information for
40 * parsing the response.
41 *
42 * @return Result
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 }