-
We are trying to implement a custom LogTransport which logs both successful (e.g. there's a response) and unsuccesful ( class AsyncLogTransport(httpx.AsyncBaseTransport):
def __init__(self, **kwargs) -> None:
self._wrapper = httpx.AsyncHTTPTransport(**kwargs)
async def handle_async_request(
self,
request: Request,
) -> Response:
try:
response = await self._wrapper.handle_async_request(request)
except HTTPError as exc:
logging.error(
"Request: %s %s -> %s",
request.method,
request.url,
str(exc.__class__.__name__),
extra={
"status": str(exc.__class__.__name__),
"method": request.method,
"path": str(request.url),
},
)
raise
await response.aread()
logging.info(
"Request: %s %s -> %s %s",
request.method,
request.url,
response.status_code,
response.reason_phrase,
extra={
"code": response.status_code,
"status": f"{response.status_code} {response.reason_phrase}",
"duration": response.elapsed.total_seconds(),
"method": request.method,
"path": str(request.url),
},
)
return response
async def aclose(self) -> None:
await self._wrapper.aclose() The code in the happy path - with minor adjustments - is taken from our response event hook and works perfectly there. However, when we try to access the
Can somebody explain why that is? We examined the code a bit but couldn't quite figure out why. When the byte stream is closed via |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok it seems I got a bit confused there, because it's quite obvious: I guess that leaves us with only one option: Using the event hook for logging the regular reponses and using our custom transport for logging the errors. |
Beta Was this translation helpful? Give feedback.
Ok it seems I got a bit confused there, because it's quite obvious:
elapsed
gets set on closingBoundSyncStream
orBoundAsyncStream
, which are both constructed in the respective clients after the transports are getting invoked. I think I got lost in the streams... ^^ and the error message set me on the wrong track.I guess that leaves us with only one option: Using the event hook for logging the regular reponses and using our custom transport for logging the errors.