Fix handling of failed items in `Base.put`
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
Base.putwill no longer throw a KeyError when a PUT request fails and the API returns a response with thefailedfield- Added workaround when raising
urllib.error.HTTPErrorso the traceback prints properly - Fixed TTL tests
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?