Skip to content

Commit ca2a488

Browse files
Improving ApiRequest and ApiClient support for custom serialization formats and additional headers.
1 parent 77b1435 commit ca2a488

File tree

3 files changed

+96
-11
lines changed

3 files changed

+96
-11
lines changed

Client/ApiClient.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,34 @@ protected function deserialize(ApiRequestInterface $apiRequest, ?string $normali
4646
return $this->serializer->deserialize(
4747
$normalizedData,
4848
$apiRequest->getClassName(),
49-
'json',
49+
$apiRequest->getDeserializationFormat(),
5050
$apiRequest->getDeserializationContext()
5151
);
5252
} catch (\Exception $e) {
5353
throw $this->handleError(
5454
$e,
55+
$apiRequest,
5556
$normalizedData,
56-
$apiRequest->getClassName(),
57-
$apiRequest->getDeserializationContext()
5857
);
5958
}
6059
}
6160

6261
protected function getRequest(ApiRequestInterface $apiRequest): RequestInterface
6362
{
6463
$request = $this->requestFactory->createRequest($apiRequest->getMethod(), $apiRequest->getPath())
65-
->withHeader('Content-Type', 'application/json');
64+
->withHeader('Content-Type', $apiRequest->getContentType());
65+
66+
foreach ($apiRequest->getHeaders() as $header => $value) {
67+
$request = $request->withHeader($header, $value);
68+
}
6669

6770
if (null === $apiRequest->getContent()) {
6871
return $request;
6972
}
7073

7174
$serializedContent = $this->serializer->serialize(
7275
$apiRequest->getContent(),
73-
'json',
76+
$apiRequest->getSerializationFormat(),
7477
$apiRequest->getSerializationContext()
7578
);
7679

@@ -113,18 +116,17 @@ protected function getResponseData(
113116

114117
protected function handleError(
115118
\Exception $exception,
119+
ApiRequestInterface $apiRequest,
116120
string $normalizedData,
117-
string $className,
118-
array $deserializationContext
119121
): ApiDeserializationException {
120122
$errorClass = $deserializationContext['error_class'] ?? null;
121123
if ($errorClass) {
122124
try {
123125
$errorObject = $this->serializer->deserialize(
124126
$normalizedData,
125127
$errorClass,
126-
'json',
127-
$deserializationContext
128+
$apiRequest->getDeserializationFormat(),
129+
$apiRequest->getDeserializationContext(),
128130
);
129131
} catch (\Exception) {
130132
$errorObject = \json_decode($normalizedData, true, 512, JSON_THROW_ON_ERROR);
@@ -133,6 +135,11 @@ protected function handleError(
133135
$errorObject = \json_decode($normalizedData, true, 512, JSON_THROW_ON_ERROR);
134136
}
135137

136-
return ApiDeserializationException::create($exception, $normalizedData, $className, $errorObject);
138+
return ApiDeserializationException::create(
139+
$exception,
140+
$normalizedData,
141+
$apiRequest->getClassName(),
142+
$errorObject
143+
);
137144
}
138145
}

Request/ApiRequest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ class ApiRequest implements ApiRequestInterface
1616

1717
protected mixed $content;
1818

19+
protected string $contentType = 'application/json';
20+
21+
protected array $headers = [];
22+
23+
protected string $serializationFormat = 'json';
24+
1925
protected array $serializationContext = [];
2026

27+
protected string $deserializationFormat = 'json';
28+
2129
protected array $deserializationContext = [];
2230

2331
public function __construct(
@@ -46,11 +54,31 @@ public function getContent(): mixed
4654
return $this->content;
4755
}
4856

57+
public function getContentType(): string
58+
{
59+
return $this->contentType;
60+
}
61+
62+
public function getHeaders(): array
63+
{
64+
return $this->headers;
65+
}
66+
67+
public function getSerializationFormat(): string
68+
{
69+
return $this->serializationFormat;
70+
}
71+
4972
public function getSerializationContext(): array
5073
{
5174
return $this->serializationContext;
5275
}
5376

77+
public function getDeserializationFormat(): string
78+
{
79+
return $this->deserializationFormat;
80+
}
81+
5482
public function getDeserializationContext(): array
5583
{
5684
return $this->deserializationContext;
@@ -70,13 +98,55 @@ public function setContent(mixed $content): self
7098
return $this;
7199
}
72100

101+
public function setContentType(string $contentType): self
102+
{
103+
$this->contentType = $contentType;
104+
105+
return $this;
106+
}
107+
108+
public function setHeaders(array $headers): self
109+
{
110+
$this->headers = $headers;
111+
112+
return $this;
113+
}
114+
115+
public function addHeader(string $key, string $value): self
116+
{
117+
$this->headers[$key] = $value;
118+
119+
return $this;
120+
}
121+
122+
public function removeHeader(string $key): self
123+
{
124+
unset($this->headers[$key]);
125+
126+
return $this;
127+
}
128+
129+
public function setSerializationFormat(string $serializationFormat): self
130+
{
131+
$this->serializationFormat = $serializationFormat;
132+
133+
return $this;
134+
}
135+
73136
public function setSerializationContext(array $serializationContext): self
74137
{
75138
$this->serializationContext = $serializationContext;
76139

77140
return $this;
78141
}
79142

143+
public function setDeserializationFormat(string $deserializationFormat): self
144+
{
145+
$this->deserializationFormat = $deserializationFormat;
146+
147+
return $this;
148+
}
149+
80150
public function setDeserializationContext(array $deserializationContext): self
81151
{
82152
$this->deserializationContext = $deserializationContext;

Request/ApiRequestInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ public function getPath(): string;
1818

1919
public function getClassName(): string;
2020

21-
public function getContent();
21+
public function getContent(): mixed;
22+
23+
public function getContentType(): string;
24+
25+
public function getHeaders(): array;
26+
27+
public function getSerializationFormat(): string;
2228

2329
public function getSerializationContext(): array;
2430

31+
public function getDeserializationFormat(): string;
32+
2533
public function getDeserializationContext(): array;
2634
}

0 commit comments

Comments
 (0)