Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Api\Parser;
3
 
1441 ariadna 4
use Aws\Api\Operation;
1 efrain 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
 * @internal Implements JSON-RPC parsing (e.g., DynamoDB)
14
 */
15
class JsonRpcParser extends AbstractParser
16
{
17
    use PayloadParserTrait;
18
 
19
    /**
20
     * @param Service    $api    Service description
21
     * @param JsonParser $parser JSON body builder
22
     */
1441 ariadna 23
    public function __construct(Service $api, ?JsonParser $parser = null)
1 efrain 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());
1441 ariadna 34
 
35
        return $this->parseResponse($response, $operation);
36
    }
37
 
38
    /**
39
     * This method parses a response based on JSON RPC protocol.
40
     *
41
     * @param ResponseInterface $response the response to parse.
42
     * @param Operation $operation the operation which holds information for
43
     *        parsing the response.
44
     *
45
     * @return Result
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
        $result = $this->parseMemberFromStream(
1 efrain 67
                $response->getBody(),
68
                $operation->getOutput(),
69
                $response
70
            );
71
 
1441 ariadna 72
        return new Result(is_null($result) ? [] : $result);
1 efrain 73
    }
74
 
75
    public function parseMemberFromStream(
76
        StreamInterface $stream,
77
        StructureShape $member,
78
        $response
79
    ) {
80
        return $this->parser->parse($member, $this->parseJson($stream, $response));
81
    }
82
}