Skip to content

Drop backend=... argument. #782

Closed

Description

@lovelydinosaur

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.)

Activity

  1. dalf commented on Jan 23, 2020

    @dalf

    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:
           ....
  2. added this to the v0.12 milestone on Jan 24, 2020
  3. lovelydinosaur commented on Jan 24, 2020

    @lovelydinosaur
    MemberAuthor

    @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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    concurrencyIssues related to concurrency and usage of async librariesgood first issueGood for newcomersrefactorIssues and PRs related to code refactoring

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions