diff --git a/src/Client.php b/src/Client.php index 22ea01e..abb5345 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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( @@ -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( @@ -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( @@ -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( @@ -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( @@ -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/'); + } }