Skip to content

Commit

Permalink
fix: Handle raise_on_error correctly in async client (#17)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
charithe authored Aug 24, 2022
1 parent 955d2c8 commit ad01d17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
15 changes: 11 additions & 4 deletions cerbos/sdk/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ def __init__(

event_hooks = {"response": []}
if debug:
event_hooks["response"].append(
lambda response: self._log_response(response)
)
event_hooks["response"].append(self._log_response)

if raise_on_error:
event_hooks["response"].append(lambda response: response.raise_for_status())
event_hooks["response"].append(self._raise_on_status)

transport = None
base_url = host
Expand All @@ -91,7 +89,16 @@ def __init__(
transport=transport,
)

async def _raise_on_status(self, response: httpx.Response):
if response is None:
return

response.raise_for_status()

async def _log_response(self, response: httpx.Response):
if response is None:
return

req_prefix = "< "
res_prefix = "> "
request = response.request
Expand Down
15 changes: 11 additions & 4 deletions cerbos/sdk/_sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ def __init__(

event_hooks = {"response": []}
if debug:
event_hooks["response"].append(
lambda response: self._log_response(response)
)
event_hooks["response"].append(self._log_response)

if raise_on_error:
event_hooks["response"].append(lambda response: response.raise_for_status())
event_hooks["response"].append(self._raise_on_status)

transport = None
base_url = host
Expand All @@ -91,7 +89,16 @@ def __init__(
transport=transport,
)

def _raise_on_status(self, response: httpx.Response):
if response is None:
return

response.raise_for_status()

def _log_response(self, response: httpx.Response):
if response is None:
return

req_prefix = "< "
res_prefix = "> "
request = response.request
Expand Down
5 changes: 3 additions & 2 deletions tests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@

import anyio

from cerbos.sdk.client import CerbosAsyncClient
from cerbos.sdk.client import AsyncCerbosClient
from cerbos.sdk.model import *


async def main():
logging.basicConfig(level=logging.DEBUG)
logging.captureWarnings(True)

async with CerbosAsyncClient(
async with AsyncCerbosClient(
"https://localhost:3592",
playground_instance="XXY",
debug=True,
tls_verify=False,
raise_on_error=True,
) as c:
p = Principal(
"john",
Expand Down

0 comments on commit ad01d17

Please sign in to comment.