Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function abortIn(float $seconds): void
public function ping(): void
{
$response = $this->httpClient->get('/api/v1/ping');
if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();

if ($status !== 200) {
Expand Down Expand Up @@ -60,6 +63,9 @@ public function verifyApiToken(): void
'/api/v1/verify-api-token',
$this->apiToken,
);
if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();

if ($status !== 200) {
Expand Down Expand Up @@ -98,6 +104,9 @@ public function writeEvents(array $events, array $preconditions = []): array
$this->apiToken,
$requestBody,
);
if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();

if ($status !== 200) {
Expand Down Expand Up @@ -150,6 +159,9 @@ public function readEvents(string $subject, ReadEventsOptions $readEventsOptions
'options' => $readEventsOptions,
],
);
if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();

if ($status !== 200) {
Expand Down Expand Up @@ -198,7 +210,9 @@ public function runEventQlQuery(string $query): iterable
'query' => $query,
],
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();

if ($status !== 200) {
Expand Down Expand Up @@ -235,7 +249,9 @@ public function observeEvents(string $subject, ObserveEventsOptions $observeEven
'options' => $observeEventsOptions,
],
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();
if ($status !== 200) {
throw new RuntimeException(sprintf(
Expand Down Expand Up @@ -286,7 +302,9 @@ public function registerEventSchema(string $eventType, array $schema): void
'schema' => $schema,
],
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();
if ($status !== 200) {
throw new RuntimeException(sprintf(
Expand All @@ -305,7 +323,9 @@ public function readSubjects(string $baseSubject): iterable
'baseSubject' => $baseSubject,
],
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();
if ($status !== 200) {
throw new RuntimeException(sprintf(
Expand Down Expand Up @@ -337,7 +357,9 @@ public function readEventTypes(): iterable
'/api/v1/read-event-types',
$this->apiToken,
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();
if ($status !== 200) {
throw new RuntimeException(sprintf(
Expand Down Expand Up @@ -376,7 +398,9 @@ public function readEventType(string $eventType): EventType
'eventType' => $eventType,
],
);

if (!$this->isValidServerHeader($response)) {
throw new RuntimeException('Server must be EventSourcingDB.');
}
$status = $response->getStatusCode();
if ($status !== 200) {
throw new RuntimeException(sprintf(
Expand All @@ -401,4 +425,14 @@ public function readEventType(string $eventType): EventType
$data['schema'] ?? [],
);
}

private function isValidServerHeader(\Thenativeweb\Eventsourcingdb\Stream\Response $response): bool
{
$serverHeader = $response->getHeader('Server');

if ($serverHeader === []) {
return false;
}
return str_starts_with($serverHeader[0], 'EventSourcingDB/');
}
}