From 5fb8b75cd4a8ee907840965bfeb730e62af10518 Mon Sep 17 00:00:00 2001 From: Matthew Tran <0e4ef622@gmail.com> Date: Tue, 19 Nov 2024 17:52:05 -0600 Subject: [PATCH] Fix async http error handling --- ntropy_sdk/async_/http.py | 22 +++++++++---------- ntropy_sdk/paging.py | 2 +- tests/v3/test_async_sdk.py | 44 ++++++++++++++++++++++++++++++++++++++ tests/v3/test_sdk.py | 13 ----------- 4 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 tests/v3/test_async_sdk.py diff --git a/ntropy_sdk/async_/http.py b/ntropy_sdk/async_/http.py index 13d44e4..c336bb9 100644 --- a/ntropy_sdk/async_/http.py +++ b/ntropy_sdk/async_/http.py @@ -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") diff --git a/ntropy_sdk/paging.py b/ntropy_sdk/paging.py index 35e8b09..518d150 100644 --- a/ntropy_sdk/paging.py +++ b/ntropy_sdk/paging.py @@ -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, diff --git a/tests/v3/test_async_sdk.py b/tests/v3/test_async_sdk.py new file mode 100644 index 0000000..dac725a --- /dev/null +++ b/tests/v3/test_async_sdk.py @@ -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" diff --git a/tests/v3/test_sdk.py b/tests/v3/test_sdk.py index f5e9d94..7f03751 100644 --- a/tests/v3/test_sdk.py +++ b/tests/v3/test_sdk.py @@ -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")