deta / deta-python

deta's official sdk
https://deta.space/docs/en/build/reference/sdk/base
MIT License
153 stars 25 forks source link

Fix handling of failed items in `Base.put` #76

Closed lemonyte closed 1 year ago

lemonyte commented 2 years ago

The Problem

The put method would raise a KeyError when trying to submit a dictionary. Issue originally brought up in https://github.com/deta/docs/discussions/431.

The Cause

The data dictionary that was submitted had empty keys (i.e. {"": "foo"}), which the API rejected and returned in a response like the following:

{
    "failed": {
        "items": [
            {"": "foo", "detabase_id": "<id>", "key": "<key>"}
        ]
    }
}

The put method was expecting a "processed" key to be present, and consequently failing when no such key existed. https://github.com/deta/deta-python/blob/49d53b8aa193f59a1085928cecf428710c1c69fa/deta/base.py#L161

The Solution

The put method now checks if a "processed" exists before accessing it. If it does not exist, it handles the response as any other unsuccessful response, and returns None.


Changes

Additional

Perhaps it would be a good idea to make sure the API always returns a "processed" key, which would be empty when all items failed to add?

hozan23 commented 1 year ago

Thanks @lemonyte