Skip to content

Disabling event hooks per request #2991

Answered by karpetrosyan
mijolabs asked this question in General
Discussion options

You must be logged in to vote

Hi!
I believe you can simply replace event hooks with custom transports as follows:

import httpx

async def request_hook(self, request):
    if self.token is None or self.token_has_expired():
        self.token = await self.client.get("/token_endpoint")

class CustomTransport(httpx.AsyncHTTPTransport):
    async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
        if not request.extensions.get("disable_hooks", False):
            await request_hook(request)

        return await super().handle_async_request(request)


client = httpx.AsyncClient(transport=CustomTransport())

await client.get("/token_endpoint")  # hook_works
await client.get("/token_endpoint", e…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mijolabs
Comment options

Answer selected by mijolabs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants