@@ -435,6 +435,7 @@ async def get_events(request: Request):
435435 - kind: Event kind to filter events
436436 - state: Event state to filter events
437437 - result: Event result to filter events
438+ - id / ids: Event document id(s) to filter events
438439 - recursive: Retrieve node together with event
439440 This API endpoint is under development and may change in future.
440441 """
@@ -446,6 +447,39 @@ async def get_events(request: Request):
446447 state = query_params .pop ('state' , None )
447448 result = query_params .pop ('result' , None )
448449 from_ts = query_params .pop ('from' , None )
450+ # Support filtering by MongoDB _id.
451+ # Accept `id=<hex>` for a single id or `ids=a,b,c` for multiple ids.
452+ # Using `id` as query param is safe here because we remove it from the
453+ # filter before passing to Mongo.
454+ event_id = query_params .pop ('id' , None )
455+ event_ids = query_params .pop ('ids' , None )
456+ if event_id and event_ids :
457+ raise HTTPException (
458+ status_code = status .HTTP_400_BAD_REQUEST ,
459+ detail = "Provide either id or ids, not both"
460+ )
461+ if event_id :
462+ try :
463+ query_params ['_id' ] = ObjectId (event_id )
464+ except (errors .InvalidId , TypeError ) as exc :
465+ raise HTTPException (
466+ status_code = status .HTTP_400_BAD_REQUEST ,
467+ detail = "Invalid id format"
468+ ) from exc
469+ elif event_ids :
470+ try :
471+ ids_list = [ObjectId (x .strip ()) for x in event_ids .split (',' ) if x .strip ()]
472+ except (errors .InvalidId , TypeError ) as exc :
473+ raise HTTPException (
474+ status_code = status .HTTP_400_BAD_REQUEST ,
475+ detail = "Invalid ids format"
476+ ) from exc
477+ if not ids_list :
478+ raise HTTPException (
479+ status_code = status .HTTP_400_BAD_REQUEST ,
480+ detail = "ids must contain at least one id"
481+ )
482+ query_params ['_id' ] = {'$in' : ids_list }
449483 if from_ts :
450484 if isinstance (from_ts , str ):
451485 from_ts = datetime .fromisoformat (from_ts )
0 commit comments