Skip to content

Commit ad01d17

Browse files
authored
fix: Handle raise_on_error correctly in async client (#17)
When the `raise_on_error` flag is used, the httpx hook would throw an exception because the lambda is not an awaitable. This PR addresses that issue. Fixes #16 Signed-off-by: Charith Ellawala <[email protected]> Signed-off-by: Charith Ellawala <[email protected]>
1 parent 955d2c8 commit ad01d17

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

cerbos/sdk/_async/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ def __init__(
6464

6565
event_hooks = {"response": []}
6666
if debug:
67-
event_hooks["response"].append(
68-
lambda response: self._log_response(response)
69-
)
67+
event_hooks["response"].append(self._log_response)
7068

7169
if raise_on_error:
72-
event_hooks["response"].append(lambda response: response.raise_for_status())
70+
event_hooks["response"].append(self._raise_on_status)
7371

7472
transport = None
7573
base_url = host
@@ -91,7 +89,16 @@ def __init__(
9189
transport=transport,
9290
)
9391

92+
async def _raise_on_status(self, response: httpx.Response):
93+
if response is None:
94+
return
95+
96+
response.raise_for_status()
97+
9498
async def _log_response(self, response: httpx.Response):
99+
if response is None:
100+
return
101+
95102
req_prefix = "< "
96103
res_prefix = "> "
97104
request = response.request

cerbos/sdk/_sync/client.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ def __init__(
6464

6565
event_hooks = {"response": []}
6666
if debug:
67-
event_hooks["response"].append(
68-
lambda response: self._log_response(response)
69-
)
67+
event_hooks["response"].append(self._log_response)
7068

7169
if raise_on_error:
72-
event_hooks["response"].append(lambda response: response.raise_for_status())
70+
event_hooks["response"].append(self._raise_on_status)
7371

7472
transport = None
7573
base_url = host
@@ -91,7 +89,16 @@ def __init__(
9189
transport=transport,
9290
)
9391

92+
def _raise_on_status(self, response: httpx.Response):
93+
if response is None:
94+
return
95+
96+
response.raise_for_status()
97+
9498
def _log_response(self, response: httpx.Response):
99+
if response is None:
100+
return
101+
95102
req_prefix = "< "
96103
res_prefix = "> "
97104
request = response.request

tests/check.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55

66
import anyio
77

8-
from cerbos.sdk.client import CerbosAsyncClient
8+
from cerbos.sdk.client import AsyncCerbosClient
99
from cerbos.sdk.model import *
1010

1111

1212
async def main():
1313
logging.basicConfig(level=logging.DEBUG)
1414
logging.captureWarnings(True)
1515

16-
async with CerbosAsyncClient(
16+
async with AsyncCerbosClient(
1717
"https://localhost:3592",
1818
playground_instance="XXY",
1919
debug=True,
2020
tls_verify=False,
21+
raise_on_error=True,
2122
) as c:
2223
p = Principal(
2324
"john",

0 commit comments

Comments
 (0)