fix(api): return list of events from insert endpoint#158
fix(api): return list of events from insert endpoint#158TimeToBuildBob wants to merge 2 commits intoActivityWatch:masterfrom
Conversation
…-rust)
The POST /api/0/buckets/{id}/events endpoint previously returned either a
single event dict or null, inconsistent with aw-server-rust which always
returns a list of events with server-assigned IDs.
Changes:
- api.py: create_events() now returns List[Event], using insert_one for
single events (to get ID) and insert_many for bulk
- rest.py: Always serialize response as a list of event dicts
- tests: Add test_insert_event_returns_list and test_insert_events_returns_list
This enables aw-client to use response.json()[0] uniformly against both
server implementations (see ActivityWatch/aw-client#103).
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to fbc2dee in 8 seconds. Click for details.
- Reviewed
95lines of code in3files - Skipped
0files when reviewing. - Skipped posting
0draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
Workflow ID: wflow_1jzMav81hfKfWbAq
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
|
@greptileai review |
Greptile SummaryThis PR successfully aligns the Python server's Confidence Score: 5/5
Important Files Changed
Last reviewed commit: 5273dc3 |
But are the returned events any different than the sent events then? Is this why Johan may have removed it? Performance optimization that made this infeasible/unjustifiable? |
|
Good question. The answer depends on the insert path: Single event: Yes, the returned event is different — it has the database-assigned Bulk insert: The returned events are effectively the same as sent, because peewee's
So Johan's removal was justified for the bulk case — returning the same events you sent without IDs isn't useful. My PR preserves this distinction: single events get the real ID, bulk returns the input list (which is arguably worse than returning nothing since it gives a false sense of having IDs). Suggestion: I can change the bulk path to also return What's your preference? |
This part makes sense, I guess the insert_events (many) should still not return the event ID. While the insert_event (single) could be a supported case without any performance impact. See discussion in ActivityWatch/aw-client#103 |
Summary
The
POST /api/0/buckets/{id}/eventsendpoint currently returns either a single event dict (for single inserts) ornull(for bulk inserts). This is inconsistent with aw-server-rust, which always returns a list of events with server-assigned IDs.This PR fixes the Python server to match aw-server-rust's response format:
[{"id": 1, "timestamp": "...", ...}](list of 1)[{"id": null, ...}, ...](list of events; IDs may not be set due to SQLite bulk insert limitations)Changes
api.py:create_events()now returnsList[Event], usinginsert_onefor single events (to get server-assigned ID) andinsert_manyfor bulkrest.py: Always serializes response as a list of event dictstests/test_server.py: Two new tests verifying list response format for both single and bulk insertsContext
This unblocks ActivityWatch/aw-client#103 which adds return values to
insert_event(s)— the client usesresponse.json()[0]which requires a list response from the server.Requested by @ErikBjare in ActivityWatch/aw-client#103 (comment)
Test plan
Important
The PR updates the
POST /api/0/buckets/{id}/eventsendpoint to consistently return a list of events, aligning with aw-server-rust, and adds tests to verify this behavior.POST /api/0/buckets/{id}/eventsnow always returns a list of events.create_events()inapi.pymodified to returnList[Event].post()inrest.pyupdated to serialize response as a list of event dicts.test_insert_event_returns_list()intest_server.pyfor single event list response.test_insert_events_returns_list()intest_server.pyfor multiple events list response.This description was created by
for fbc2dee. You can customize this summary. It will automatically update as commits are pushed.