Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Serializer;
3
4 use Aws\Api\Service;
5
6 use Aws\Api\Shape;
7 use Aws\Api\StructureShape;
8 use Aws\CommandInterface;
9 use Aws\EndpointV2\EndpointV2SerializerTrait;
10 use Aws\EndpointV2\Ruleset\RulesetEndpoint;
11 use DateTimeInterface;
12 use GuzzleHttp\Psr7;
13 use GuzzleHttp\Psr7\Request;
14 use GuzzleHttp\Psr7\Uri;
15 use Psr\Http\Message\RequestInterface;
16
17 /**
18 * Base implementation for Smithy RPC V2 protocol serializers.
19 *
20 * Implementers MUST override the defaultHeader property to represent
21 * protocol-specific default header values:
22 * self::HEADER_SMITHY_PROTOCOL => static::SMITHY_PROTOCOL,
23 * self::HEADER_CONTENT_TYPE => static::DEFAULT_CONTENT_TYPE,
24 * self::HEADER_ACCEPT => static::DEFAULT_ACCEPT
25 *
26 * Implementers must also implement `serialize()`, `resolveBlob()`, and `resolveTimestamp()
27 * according to their respective protocol specifications.
28 *
29 * @internal
30 */
31 abstract class AbstractRpcV2Serializer
32 {
33 protected const HEADER_SMITHY_PROTOCOL = 'Smithy-Protocol';
34 protected const HEADER_CONTENT_TYPE = 'Content-Type';
35 protected const HEADER_ACCEPT = 'Accept';
36
37 /** @var array */
38 protected static array $defaultHeaders;
39
40 /** @var Service */
41 private Service $api;
42
43 /** @var string|Uri */
44 private string|Uri $endpoint;
45
46 /** @var bool */
47 private bool $isUseEndpointV2;
48
49 use EndpointV2SerializerTrait;
50
51 /**
52 * @param Service $api Service API description
53 * @param string $endpoint Endpoint to connect to
54 */
55 public function __construct(Service $api, string|Uri $endpoint)
56 {
57 $this->api = $api;
58 $this->endpoint = Psr7\Utils::uriFor($endpoint);
59 }
60
61 /**
62 * @param CommandInterface $command Command to serialize into a request.
63 * @param mixed|null $endpoint
64 *
65 * @return RequestInterface
66 */
67 public function __invoke(
68 CommandInterface $command,
69 mixed $endpoint = null
70 )
71 {
72 $commandArgs = $command->toArray();
73 $commandName = $command->getName();
74 $operation = $this->api->getOperation($commandName);
75 $headers = static::$defaultHeaders;
76
77 // Operations with no defined input type must not contain bodies
78 // Content-Type must not be set
79 if ($operation['input'] !== null) {
80 $body = $this->serialize($operation->getInput(), $commandArgs);
81 $headers['Content-Length'] = (string) strlen($body);
82 } else {
83 unset($headers['Content-Type']);
84 }
85
86 if ($endpoint instanceof RulesetEndpoint) {
87 $this->isUseEndpointV2 = true;
88 $this->setEndpointV2RequestOptions($endpoint, $headers);
89 $this->endpoint = $endpoint->getUrl();
90 }
91
92 $requestTarget = $this->buildRequestTarget(
93 $commandName,
94 $operation['http']['requestUri'] ?? ''
95 );
96 $uri = new Uri($this->endpoint . $requestTarget);
97
98 return new Request(
99 $operation['http']['method'],
100 $uri,
101 $headers,
102 $body ?? null
103 );
104 }
105
106 /**
107 * @param StructureShape $inputShape
108 * @param array $commandArgs
109 *
110 * @return string
111 */
112 abstract public function serialize(
113 StructureShape $inputShape,
114 array $commandArgs
115 ): string;
116
117 /**
118 * Resolves arguments for blob shapes present in the request arguments
119 * into a protocol-specific format.
120 *
121 * @param mixed $value
122 *
123 * @return array
124 */
125 abstract protected function resolveBlob(mixed $value): array;
126
127 /**
128 * Resolves arguments for timestamp shapes present in the request arguments
129 * into a protocol-specific format.
130 *
131 * @param mixed $value
132 *
133 * @return array
134 */
135 abstract protected function resolveTimestamp(
136 int|float|string|DateTimeInterface $value
137 ): array;
138
139 /**
140 * Resolves input shape fields that are present in the request arguments
141 *
142 * @param Shape $shape
143 * @param mixed $value
144 *
145 * @return mixed
146 */
147 protected function resolveInputShape(Shape $shape, mixed $value): mixed
148 {
149 switch ($shape->getType()) {
150 case 'structure':
151 $data = [];
152 foreach ($value as $k => $v) {
153 if ($v !== null && $shape->hasMember($k)) {
154 $valueShape = $shape->getMember($k);
155 $data[$valueShape['locationName'] ?: $k]
156 = $this->resolveInputShape($valueShape, $v);
157 }
158 }
159
160 return $data;
161
162 case 'list':
163 $items = $shape->getMember();
164 foreach ($value as $k => $v) {
165 $value[$k] = $this->resolveInputShape($items, $v);
166 }
167
168 return $value;
169
170 case 'map':
171 $values = $shape->getValue();
172 foreach ($value as $k => $v) {
173 $value[$k] = $this->resolveInputShape($values, $v);
174 }
175
176 return $value;
177
178 case 'timestamp':
179 return $this->resolveTimestamp($value);
180
181 case 'string':
182 return (string) $value;
183
184 case 'integer':
185 case 'long':
186 return (int) $value;
187
188 case 'double':
189 case 'float':
190 return (float) $value;
191
192 case 'blob':
193 return $this->resolveBlob($value);
194
195 default:
196 return $value;
197 }
198 }
199
200 /**
201 * Builds request URI absolute path
202 *
203 * @param string $commandName
204 * @param string $requestUri
205 *
206 * @return string
207 */
208 private function buildRequestTarget(
209 string $commandName,
210 string $requestUri
211 ): string
212 {
213 $requestUri = str_ends_with($requestUri, '/')
214 ? $requestUri
215 : $requestUri . '/';
216 $targetPrefix = $this->api->getMetadata('targetPrefix');
217
218 return "{$requestUri}service/{$targetPrefix}/operation/{$commandName}";
219 }
220 }