11from collections .abc import AsyncGenerator
22import contextlib
33from types import TracebackType
4- from typing import TypeVar
4+ from typing import Any , TypeVar
55
66from http import HTTPStatus
77import json
@@ -110,9 +110,8 @@ async def write_events(
110110
111111 if response .status_code != HTTPStatus .OK :
112112 raise ServerError (
113- f'Unexpected response status: '
114- f'{ response .status_code } { HTTPStatus (response .status_code ).phrase } .'
115- )
113+ f'Unexpected response status: '
114+ )
116115
117116 response_data = await response .body .read ()
118117 response_data = bytes .decode (response_data , encoding = 'utf-8' )
@@ -144,9 +143,8 @@ async def read_events(
144143 async with response :
145144 if response .status_code != HTTPStatus .OK :
146145 raise ServerError (
147- f'Unexpected response status: '
148- f'{ response .status_code } { HTTPStatus (response .status_code ).phrase } '
149- )
146+ f'Unexpected response status: { response } '
147+ )
150148 async for raw_message in response .body :
151149 message = parse_raw_message (raw_message )
152150
@@ -189,11 +187,36 @@ async def read_events(
189187 f'{ message } .'
190188 )
191189
192- async def run_eventql_query (self , query : str ) -> AsyncGenerator [Event ]:
193- # TODO: read events nehmen. Das Responsehandling ist gleich wie
194- # read_events. ein object was eine query property hat, Return ist ein any.
195- # da kann alles mögliche sein.
196- raise NotImplementedError ("run_eventql_query is not implemented yet." )
190+ async def run_eventql_query (self , query : str ) -> AsyncGenerator [Any , None ]:
191+ request_body = json .dumps ({
192+ 'query' : query ,
193+ })
194+ response : Response = await self .__http_client .post (
195+ path = '/api/v1/run-eventql-query' ,
196+ request_body = request_body ,
197+ )
198+
199+ async with response :
200+ if response .status_code != HTTPStatus .OK :
201+ raise ServerError (
202+ f'Unexpected response status: { response } '
203+ )
204+ async for raw_message in response .body :
205+ message = parse_raw_message (raw_message )
206+
207+ if is_stream_error (message ):
208+ raise ServerError (f'{ message ["payload" ]["error" ]} .' )
209+
210+ if message .get ('type' ) == 'row' :
211+ payload = message ['payload' ]
212+
213+ yield payload
214+ continue
215+
216+ raise ServerError (
217+ f'Failed to execute EventQL query, an unexpected stream item was received: '
218+ f'{ message } .'
219+ )
197220
198221 async def observe_events (
199222 self ,
@@ -205,17 +228,15 @@ async def observe_events(
205228 'options' : options .to_json ()
206229 })
207230
208- response : Response
209- response = await self .http_client .post (
231+ response : Response = await self .http_client .post (
210232 path = '/api/v1/observe-events' ,
211233 request_body = request_body ,
212234 )
213235
214- with response :
236+ async with response :
215237 if response .status_code != HTTPStatus .OK :
216238 raise ServerError (
217- f'Unexpected response status: '
218- f'{ response .status_code } { HTTPStatus (response .status_code ).phrase } '
239+ f'Unexpected response status: { response } '
219240 )
220241 async for raw_message in response .body :
221242 message = parse_raw_message (raw_message )
@@ -256,23 +277,17 @@ async def register_event_schema(self, event_type: str, json_schema: dict) -> Non
256277 'schema' : json_schema ,
257278 })
258279
259- response : Response
260- response = await self .http_client .post (
280+ response : Response = await self .http_client .post (
261281 path = '/api/v1/register-event-schema' ,
262282 request_body = request_body ,
263283 )
264284
265- with response :
285+ async with response :
266286 if response .status_code != HTTPStatus .OK :
267287 raise ServerError (
268- 'Unexpected response status: '
269- f'{ response .status_code } '
270- f'{ HTTPStatus (response .status_code ).phrase } '
271- f'{ str (response )} '
288+ f'Unexpected response status: { response } '
272289 )
273290
274- return
275-
276291 async def read_subjects (
277292 self ,
278293 base_subject : str
@@ -281,19 +296,15 @@ async def read_subjects(
281296 'baseSubject' : base_subject
282297 })
283298
284- response : Response
285- response = await self .http_client .post (
299+ response : Response = await self .http_client .post (
286300 path = '/api/v1/read-subjects' ,
287301 request_body = request_body ,
288302 )
289303
290- with response :
304+ async with response :
291305 if response .status_code != HTTPStatus .OK :
292306 raise ServerError (
293- 'Unexpected response status: '
294- f'{ response .status_code } '
295- f'{ HTTPStatus (response .status_code ).phrase } '
296- f'{ str (response )} '
307+ f'Unexpected response status: { response } '
297308 )
298309 async for raw_message in response .body :
299310 message = parse_raw_message (raw_message )
@@ -322,11 +333,10 @@ async def read_event_types(self) -> AsyncGenerator[EventType]:
322333 except Exception as other_error :
323334 raise InternalError (str (other_error )) from other_error
324335
325- with response :
336+ async with response :
326337 if response .status_code != HTTPStatus .OK :
327338 raise ServerError (
328- 'Unexpected response status: '
329- f'{ response .status_code } { HTTPStatus (response .status_code ).phrase } '
339+ f'Unexpected response status: { response } '
330340 )
331341 async for raw_message in response .body :
332342 message = parse_raw_message (raw_message )
0 commit comments