@@ -83,9 +83,28 @@ async def ping(self) -> None:
8383 raise ServerError (f"Received unexpected response: { response_body } " )
8484
8585 async def verify_api_token (self ) -> None :
86- # POST Request. Ist wie ping, aber mit POST. Authorization header token
87- # mitschicken. Token gültig dann event, Token ungültig 401
88- raise NotImplementedError ("verify_api_token is not implemented yet." )
86+ request_body = json .dumps ({})
87+
88+ response : Response = await self .http_client .post (
89+ path = '/api/v1/verify-api-token' ,
90+ request_body = request_body ,
91+ )
92+ async with response :
93+ if response .status_code != HTTPStatus .OK :
94+ raise ServerError (
95+ f'Failed to verify API token: { response } '
96+ )
97+
98+ response_data = await response .body .read ()
99+ response_data = bytes .decode (response_data , encoding = 'utf-8' )
100+ response_json = json .loads (response_data )
101+
102+ if not isinstance (response_json , dict ) or 'type' not in response_json :
103+ raise ServerError ('Failed to parse response: {response}' )
104+
105+ expected_event_type = 'io.eventsourcingdb.api.api-token-verified'
106+ if response_json .get ('type' ) != expected_event_type :
107+ raise ServerError (f'Failed to verify API token: { response } ' )
89108
90109 async def write_events (
91110 self ,
@@ -111,7 +130,7 @@ async def write_events(
111130 if response .status_code != HTTPStatus .OK :
112131 raise ServerError (
113132 f'Unexpected response status: '
114- )
133+ )
115134
116135 response_data = await response .body .read ()
117136 response_data = bytes .decode (response_data , encoding = 'utf-8' )
@@ -144,7 +163,7 @@ async def read_events(
144163 if response .status_code != HTTPStatus .OK :
145164 raise ServerError (
146165 f'Unexpected response status: { response } '
147- )
166+ )
148167 async for raw_message in response .body :
149168 message = parse_raw_message (raw_message )
150169
@@ -209,7 +228,7 @@ async def run_eventql_query(self, query: str) -> AsyncGenerator[Any, None]:
209228
210229 if message .get ('type' ) == 'row' :
211230 payload = message ['payload' ]
212-
231+
213232 yield payload
214233 continue
215234
@@ -228,7 +247,7 @@ async def observe_events(
228247 'options' : options .to_json ()
229248 })
230249
231- response : Response = await self .http_client .post (
250+ response : Response = await self .http_client .post (
232251 path = '/api/v1/observe-events' ,
233252 request_body = request_body ,
234253 )
@@ -296,7 +315,7 @@ async def read_subjects(
296315 'baseSubject' : base_subject
297316 })
298317
299- response : Response = await self .http_client .post (
318+ response : Response = await self .http_client .post (
300319 path = '/api/v1/read-subjects' ,
301320 request_body = request_body ,
302321 )
0 commit comments