Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of failed items in Base.put #76

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions deta/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ async def put(
data["key"] = key

insert_ttl(data, self.__ttl_attribute, expire_in=expire_in, expire_at=expire_at)
async with self._session.put(
f"{self._base_url}/items", json={"items": [data]}
) as resp:
async with self._session.put(f"{self._base_url}/items", json={"items": [data]}) as resp:
if resp.status == 207:
resp_json = await resp.json()
return resp_json["processed"]["items"][0]
else:
return None
if "processed" in resp_json:
return resp_json["processed"]["items"][0]
return None

async def put_many(
self,
Expand Down
6 changes: 5 additions & 1 deletion deta/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def put(
code, res = self._request(
"/items", "PUT", {"items": [data]}, content_type=JSON_MIME
)
return res["processed"]["items"][0] if res and code == 207 else None

if code == 207 and "processed" in res:
return res["processed"]["items"][0]
else:
return None

def put_many(
self,
Expand Down
3 changes: 2 additions & 1 deletion deta/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def _request(
## return None if not found
if status == 404:
return status, None
raise urllib.error.HTTPError(url, status, res.reason, res.headers, res.fp)
fp = res.fp if res.fp is not None else '' # FIXME: workaround to fix traceback printing for HTTPError
raise urllib.error.HTTPError(url, status, res.reason, res.headers, fp)

## if stream return the response and client without reading and closing the client
if stream:
Expand Down
2 changes: 1 addition & 1 deletion deta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def _get_project_key_id(project_key: str = None, project_id: str = None):
if project_id == project_key:
raise AssertionError("Bad project key provided")

return project_key, project_id
return project_key, project_id
2 changes: 1 addition & 1 deletion tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def get_expire_in(expire_in):
async def test_ttl(db, items):
item1 = items[0]
expire_in = 300
expire_at = datetime.datetime(2022, 3, 1, 12, 30, 30)
expire_at = datetime.datetime.now() + datetime.timedelta(seconds=300)
delta = 2 # allow time delta of 2 seconds
test_cases = [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def get_expire_in(self, expire_in):

def test_ttl(self):
expire_in = 300
expire_at = datetime.datetime(2022, 3, 1, 12, 30, 30)
expire_at = datetime.datetime.now() + datetime.timedelta(seconds=300)
delta = 2 # allow time delta of 2 seconds
test_cases = [
{
Expand Down