Skip to content

Commit

Permalink
Defer asyncio/trio import until it is used
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Jul 14, 2020
1 parent 7d449b4 commit 978ff52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion httpx_socks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'httpx-socks'
__version__ = '0.2.2'
__version__ = '0.2.3'

from .core_socks import (
ProxyError,
Expand Down
20 changes: 10 additions & 10 deletions httpx_socks/_async_transport.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import asyncio
import trio
import sniffio

from httpcore import AsyncConnectionPool
from httpcore._async.connection import AsyncHTTPConnection # noqa
from httpcore._backends.asyncio import (SocketStream # noqa
as AsyncioSocketStream)
from httpcore._backends.trio import (SocketStream # noqa
as TrioSocketStream)
from httpcore._utils import url_to_origin # noqa
from httpx._config import SSLConfig # noqa

Expand Down Expand Up @@ -79,11 +73,14 @@ async def _open_stream(self, host, port, connect_timeout, ssl_context):
backend = sniffio.current_async_library()

if backend == 'asyncio':
if self._loop is None:
self._loop = asyncio.get_event_loop()

import asyncio
from httpcore._backends.asyncio import SocketStream # noqa
from .core_socks.async_.asyncio import Proxy

if self._loop is None:
self._loop = asyncio.get_event_loop()

proxy = Proxy.create(
loop=self._loop,
proxy_type=self._proxy_type,
Expand All @@ -104,11 +101,14 @@ async def _open_stream(self, host, port, connect_timeout, ssl_context):
ssl=ssl_context,
server_hostname=host if ssl_context else None,
)
return AsyncioSocketStream(
return SocketStream(
stream_reader=stream_reader, stream_writer=stream_writer
)

elif backend == 'trio':

import trio
from httpcore._backends.trio import SocketStream # noqa
from .core_socks.async_.trio import Proxy

proxy = Proxy.create(
Expand All @@ -131,7 +131,7 @@ async def _open_stream(self, host, port, connect_timeout, ssl_context):
)
await stream.do_handshake()

return TrioSocketStream(
return SocketStream(
stream=stream
)

Expand Down
2 changes: 1 addition & 1 deletion httpx_socks/core_socks/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = 'core-socks'
__version__ = '0.1.1'
__version__ = '0.1.2'

0 comments on commit 978ff52

Please sign in to comment.