Skip to content

Commit 3d7a07b

Browse files
authored
Merge pull request #155 from equinor/async-fixes
Add raise_for_status_async decorator + follow_redirect
2 parents 17ed752 + 2d9bc82 commit 3d7a07b

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/sumo/wrapper/_blob_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import httpx
22

3-
from ._decorators import raise_for_status, http_retry
3+
from ._decorators import raise_for_status, http_retry, raise_for_status_async
44

55

66
class BlobClient:
@@ -25,7 +25,7 @@ def upload_blob(self, blob: bytes, url: str):
2525

2626
return response
2727

28-
@raise_for_status
28+
@raise_for_status_async
2929
@http_retry
3030
async def upload_blob_async(self, blob: bytes, url: str):
3131
"""Upload a blob async.

src/sumo/wrapper/_decorators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ def wrapper(*args, **kwargs):
1414
return wrapper
1515

1616

17+
def raise_for_status_async(func):
18+
async def wrapper(*args, **kwargs):
19+
# FIXME: in newer versions of httpx, raise_for_status() is chainable,
20+
# so we could simply write
21+
# return func(*args, **kwargs).raise_for_status()
22+
response = await func(*args, **kwargs)
23+
response.raise_for_status()
24+
return response
25+
26+
return wrapper
27+
28+
1729
def http_unpack(func):
1830
def wrapper(*args, **kwargs):
1931
response = func(*args, **kwargs)

src/sumo/wrapper/sumo_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ._auth_provider import get_auth_provider
1010
from .config import APP_REGISTRATION, TENANT_ID, AUTHORITY_HOST_URI
1111

12-
from ._decorators import raise_for_status, http_retry
12+
from ._decorators import raise_for_status, http_retry, raise_for_status_async
1313

1414
logger = logging.getLogger("sumo.wrapper")
1515

@@ -328,7 +328,7 @@ def getLogger(self, name):
328328
logger.addHandler(handler)
329329
return logger
330330

331-
@raise_for_status
331+
@raise_for_status_async
332332
@http_retry
333333
async def get_async(self, path: str, params: dict = None):
334334
"""Performs an async GET-request to the Sumo API.
@@ -364,7 +364,7 @@ async def get_async(self, path: str, params: dict = None):
364364
"authorization": f"Bearer {token}",
365365
}
366366

367-
async with httpx.AsyncClient() as client:
367+
async with httpx.AsyncClient(follow_redirects=True) as client:
368368
response = await client.get(
369369
f"{self.base_url}{path}",
370370
params=params,
@@ -374,7 +374,7 @@ async def get_async(self, path: str, params: dict = None):
374374

375375
return response
376376

377-
@raise_for_status
377+
@raise_for_status_async
378378
@http_retry
379379
async def post_async(
380380
self,
@@ -450,7 +450,7 @@ async def post_async(
450450

451451
return response
452452

453-
@raise_for_status
453+
@raise_for_status_async
454454
@http_retry
455455
async def put_async(
456456
self, path: str, blob: bytes = None, json: dict = None
@@ -496,7 +496,7 @@ async def put_async(
496496

497497
return response
498498

499-
@raise_for_status
499+
@raise_for_status_async
500500
@http_retry
501501
async def delete_async(self, path: str, params: dict = None) -> dict:
502502
"""Performs an async DELETE-request to the Sumo API.

0 commit comments

Comments
 (0)