Skip to content

Commit

Permalink
Fix async http error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
0e4ef622 committed Nov 20, 2024
1 parent f5b2992 commit 5fb8b75
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
22 changes: 10 additions & 12 deletions ntropy_sdk/async_/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,15 @@ async def retry_ratelimited_request(

continue

try:
resp.raise_for_status()
except aiohttp.ClientResponseError as e:
status_code = resp.status

try:
content = await resp.json()
except JSONDecodeError:
content = {}

err = error_from_http_status_code(request_id, status_code, content)
raise err
if not resp.ok:
async with resp:
status_code = resp.status
try:
content = await resp.json()
except JSONDecodeError:
content = {}

err = error_from_http_status_code(request_id, status_code, content)
raise err
return resp
raise NtropyError(f"Failed to {method} {url} after {retries} attempts")
2 changes: 1 addition & 1 deletion ntropy_sdk/paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PagedResponse(GenericModel, Generic[T]):
data: List[T]
request_id: Optional[str] = None
_resource: Optional[ListableResource[T]] = PrivateAttr(None)
_extra_kwargs: Optional[Mapping] = PrivateAttr(None)
_request_kwargs: Optional[Mapping] = PrivateAttr(None)

def __init__(
self,
Expand Down
44 changes: 44 additions & 0 deletions tests/v3/test_async_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from ntropy_sdk.async_.sdk import AsyncSDK
from ntropy_sdk.v2.errors import NtropyValueError


@pytest.mark.asyncio
async def test_async_pagination(async_sdk: AsyncSDK):
tx_ids = set()
it = (await async_sdk.transactions.list(limit=2)).auto_paginate(page_size=2)
i = 0
async for tx in it:
tx_ids.add(tx.id)
i += 1
if i == 10:
break
assert len(tx_ids) == 10


@pytest.mark.asyncio
async def test_recurrence_groups(async_sdk: AsyncSDK):
sdk = async_sdk
try:
await sdk.account_holders.create(
id="Xksd9SWd",
type="consumer",
)
except NtropyValueError:
pass

for i in range(1, 5):
await sdk.transactions.create(
id=f"netflix-{i}",
description=f"Recurring Debit Purchase Card 1350 #{i} netflix.com Netflix.com CA",
amount=17.99,
currency="USD",
entry_type="outgoing",
date=f"2021-0{i}-01",
account_holder_id="Xksd9SWd",
)

recurring_groups = await sdk.account_holders.recurring_groups("Xksd9SWd")

assert recurring_groups.groups[0].counterparty.website == "netflix.com"
assert recurring_groups.groups[0].periodicity == "monthly"
13 changes: 0 additions & 13 deletions tests/v3/test_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ def test_pagination(sdk: SDK):
assert len(tx_ids) == 10


@pytest.mark.asyncio
async def test_async_pagination(async_sdk: AsyncSDK):
tx_ids = set()
it = (await async_sdk.transactions.list(limit=2)).auto_paginate(page_size=2)
i = 0
async for tx in it:
tx_ids.add(tx.id)
i += 1
if i == 10:
break
assert len(tx_ids) == 10


def test_readme(api_key):
readme_file = open(
os.path.join(os.path.dirname(__file__), "..", "..", "README.md")
Expand Down

0 comments on commit 5fb8b75

Please sign in to comment.