Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Serializer/JsonRpcSerializer.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Serializer;
3
4 use Aws\Api\Service;
5 use Aws\CommandInterface;
6 use Aws\EndpointV2\EndpointV2SerializerTrait;
7 use Aws\EndpointV2\Ruleset\RulesetEndpoint;
8 use GuzzleHttp\Psr7\Request;
9 use Psr\Http\Message\RequestInterface;
10
11 /**
12 * Prepares a JSON-RPC request for transfer.
13 * @internal
14 */
15 class JsonRpcSerializer
16 {
17 use EndpointV2SerializerTrait;
18
19 /** @var JsonBody */
20 private $jsonFormatter;
21
22 /** @var string */
23 private $endpoint;
24
25 /** @var Service */
26 private $api;
27
28 /** @var string */
29 private $contentType;
30
31 /**
32 * @param Service $api Service description
33 * @param string $endpoint Endpoint to connect to
34 * @param JsonBody $jsonFormatter Optional JSON formatter to use
35 */
36 public function __construct(
37 Service $api,
38 $endpoint,
39 ?JsonBody $jsonFormatter = null
40 ) {
41 $this->endpoint = $endpoint;
42 $this->api = $api;
43 $this->jsonFormatter = $jsonFormatter ?: new JsonBody($this->api);
44 $this->contentType = JsonBody::getContentType($api);
45 }
46
47 /**
48 * When invoked with an AWS command, returns a serialization array
49 * containing "method", "uri", "headers", and "body" key value pairs.
50 *
51 * @param CommandInterface $command Command to serialize into a request.
52 * @param $endpointProvider Provider used for dynamic endpoint resolution.
53 * @param $clientArgs Client arguments used for dynamic endpoint resolution.
54 *
55 * @return RequestInterface
56 */
57 public function __invoke(
58 CommandInterface $command,
59 $endpoint = null
60 )
61 {
62 $operationName = $command->getName();
63 $operation = $this->api->getOperation($operationName);
64 $commandArgs = $command->toArray();
65 $body = $this->jsonFormatter->build($operation->getInput(), $commandArgs);
66 $headers = [
67 'X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $operationName,
68 'Content-Type' => $this->contentType,
69 'Content-Length' => (string) strlen($body)
70 ];
71
72 if ($endpoint instanceof RulesetEndpoint) {
73 $this->setEndpointV2RequestOptions($endpoint, $headers);
74 }
75
76 $requestUri = $operation['http']['requestUri'] ?? null;
77 $absoluteUri = str_ends_with($this->endpoint, '/')
78 ? $this->endpoint : $this->endpoint . $requestUri;
79
80 return new Request(
81 $operation['http']['method'],
82 $absoluteUri,
83 $headers,
84 $body
85 );
86 }
87 }