Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop backend=... argument. #782

Closed
tomchristie opened this issue Jan 20, 2020 · 2 comments · Fixed by #791
Closed

Drop backend=... argument. #782

tomchristie opened this issue Jan 20, 2020 · 2 comments · Fixed by #791
Labels
concurrency Issues related to concurrency and usage of async libraries good first issue Good for newcomers refactor Issues and PRs related to code refactoring
Milestone

Comments

@tomchristie
Copy link
Member

Now that we have async autodetection, I think we ought to consider dropping the backend argument from AsyncClient.__init__(...), since it's not really required.

If anything it'd be better to find out if/when there are any cases that sniffio isn't able to correctly detect, and fix them, rather than having users be able to flick an override switch.

(It'd also potentially make it easier to have a clear split if we wanted to factor the dispatch out into an entirely independant package at some point, since we'd want the backend implementations to reside in the core networking package, rather than in httpx.)

@tomchristie tomchristie added the refactor Issues and PRs related to code refactoring label Jan 20, 2020
@florimondmanca florimondmanca added concurrency Issues related to concurrency and usage of async libraries good first issue Good for newcomers labels Jan 21, 2020
@dalf
Copy link

dalf commented Jan 23, 2020

Note: I use a custom Backend to record the certificates of the HTTPS requests.
This backend behaves as a proxy to a real backend, but record the ssl.SSLObject on the way.

Code:

[EDIT] As I understand, the code will be something like this (using the private API):

from httpx._backend.auto import AutoBackend
from httpx._dispatch.connection_pool import ConnectionPool

class CustomBackend(AutoBackend):
    async def open_tcp_stream(
        self,
        hostname: str,
        port: int,
        ssl_context: typing.Optional[ssl.SSLContext],
        timeout: Timeout,
    ) -> BaseSocketStream:
        value = await super().open_tcp_stream(hostname, port, ssl_context, timeout)
        # use value.stream_reader._transport.get_extra_info('ssl_object')
        return value

async def f(*args, **kwargs):
    backend = CustomBackend()
    dispatch = ConnectionPool(backend=backend, http2=True)
    async with AsyncClient(dispatch=dispatch) as client:
       ....

@tomchristie tomchristie added this to the v0.12 milestone Jan 24, 2020
@tomchristie
Copy link
Member Author

@dalf - That's a really useful (surprising) bit of feedback, thanks!

(And yes, you've got the right approach there, but make sure you're pinning your dependencies tightly if you're relying on private API!)

not-my-profile added a commit to not-my-profile/searx-space that referenced this issue Jan 28, 2022
In order to grade TLS certificates with CryptCheck
searxstats has to retrieve the certificates.

Previously this was done by monkey patching
httpx.backends.asyncio.AsyncioBackend.

The backend parameter for AsyncClient has
however been removed in httpx 0.12.0[1].

Since monkey patching private APIs isn't a good idea anyway
this commit instead monkey patches the public API of
the ssl.SSLContext class of the standard library.

[1]: encode/httpx#782

Fixes searxng#89.
not-my-profile added a commit to not-my-profile/searx-space that referenced this issue Jan 28, 2022
In order to grade TLS certificates with CryptCheck
searxstats has to retrieve the certificates.

Previously this was done by monkey patching
httpx.backends.asyncio.AsyncioBackend.

The backend parameter for AsyncClient has
however been removed in httpx 0.12.0[1].

Since monkey patching private APIs isn't a good idea anyway
this commit instead monkey patches the public API of
the ssl.SSLContext class of the standard library.

[1]: encode/httpx#782

Fixes searxng#89.
not-my-profile added a commit to not-my-profile/searx-space that referenced this issue Jan 28, 2022
In order to grade TLS certificates with CryptCheck
searxstats has to retrieve the certificates.

Previously this was done by monkey patching
httpx.backends.asyncio.AsyncioBackend.

The backend parameter for AsyncClient has
however been removed in httpx 0.12.0[1].

Since monkey patching private APIs isn't a good idea anyway
this commit instead monkey patches the public API of
the ssl.SSLContext class of the standard library.

[1]: encode/httpx#782

Fixes searxng#89.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency Issues related to concurrency and usage of async libraries good first issue Good for newcomers refactor Issues and PRs related to code refactoring
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants