Skip to content

Commit

Permalink
Make it compatible with httpx.v0.18.x
Browse files Browse the repository at this point in the history
  • Loading branch information
romis2012 committed Apr 30, 2021
1 parent 0ffa48c commit e15ed37
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 32 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.3.1'
__version__ = '0.4.0'

from python_socks import (
ProxyError,
Expand Down
17 changes: 9 additions & 8 deletions httpx_socks/_async_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ def __init__(self, *, proxy_type: ProxyType,

super().__init__(http2=http2, ssl_context=ssl_context, **kwargs)

async def arequest(self, method, url, headers=None, stream=None,
ext=None):
async def handle_async_request(
self, method, url, headers=None,
stream=None, extensions=None):

origin = url_to_origin(url)
connection = await self._get_connection_from_pool(origin)

ext = {} if ext is None else ext
timeout = ext.get('timeout', {})
extensions = {} if extensions is None else extensions
timeout = extensions.get('timeout', {})
connect_timeout = timeout.get('connect')

if connection is None:
socket = await self._connect_to_proxy(
socket = await self._connect_via_proxy(
origin=origin,
connect_timeout=connect_timeout
)
Expand All @@ -55,16 +56,16 @@ async def arequest(self, method, url, headers=None, stream=None,
)
await self._add_to_pool(connection=connection, timeout=timeout)

response = await connection.arequest(
response = await connection.handle_async_request(
method=method,
url=url,
headers=headers,
stream=stream,
ext=ext
extensions=extensions
)
return response

async def _connect_to_proxy(self, origin, connect_timeout):
async def _connect_via_proxy(self, origin, connect_timeout):
scheme, hostname, port = origin

ssl_context = self._ssl_context if scheme == b'https' else None
Expand Down
15 changes: 8 additions & 7 deletions httpx_socks/_sync_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ def __init__(self, *, proxy_type: ProxyType,

super().__init__(http2=http2, ssl_context=ssl_context, **kwargs)

def request(self, method, url, headers=None, stream=None, ext=None):
def handle_request(self, method, url, headers=None, stream=None,
extensions=None):
origin = url_to_origin(url)
connection = self._get_connection_from_pool(origin)

ext = {} if ext is None else ext
timeout = ext.get('timeout', {})
extensions = {} if extensions is None else extensions
timeout = extensions.get('timeout', {})
connect_timeout = timeout.get('connect')

if connection is None:
socket = self._connect_to_proxy(
socket = self._connect_via_proxy(
origin=origin,
connect_timeout=connect_timeout
)
Expand All @@ -51,17 +52,17 @@ def request(self, method, url, headers=None, stream=None, ext=None):
)
self._add_to_pool(connection=connection, timeout=timeout)

response = connection.request(
response = connection.handle_request(
method=method,
url=url,
headers=headers,
stream=stream,
ext=ext
extensions=extensions
)

return response

def _connect_to_proxy(self, origin, connect_timeout):
def _connect_via_proxy(self, origin, connect_timeout):
scheme, hostname, port = origin

ssl_context = self._ssl_context if scheme == b'https' else None
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
python-socks>=1.1.0
httpx>=0.16.0
httpcore>=0.12.0
httpx>=0.18.1
httpcore>=0.13.2
async-timeout>=3.0.1
trio>=0.16.0
sniffio>=1.1.0
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
packages=['httpx_socks'],
keywords='httpx asyncio socks socks5 socks4 http proxy',
install_requires=[
'httpx>=0.15.4',
'python-socks>=1.1.0',
'httpx>=0.18.1',
'python-socks>=1.2.4',
],
extras_require={
'asyncio': ['async-timeout>=3.0.1'],
Expand Down
3 changes: 2 additions & 1 deletion tests/test_transport_asyncio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ssl

import httpcore
import httpx
import pytest # noqa
from yarl import URL # noqa
Expand Down Expand Up @@ -75,7 +76,7 @@ async def test_socks5_proxy_with_read_timeout(url=TEST_URL_IPV4_DELAY):
verify=create_ssl_context(url)
)
timeout = httpx.Timeout(2, connect=32)
with pytest.raises(httpx.ReadTimeout):
with pytest.raises(httpcore.ReadTimeout):
await fetch(transport=transport, url=url, timeout=timeout)


Expand Down
11 changes: 7 additions & 4 deletions tests/test_transport_curio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ssl
import curio

import httpcore
import httpx
import pytest # noqa
from yarl import URL # noqa
Expand Down Expand Up @@ -31,8 +32,11 @@ def create_ssl_context(url):
return None


async def fetch(transport: AsyncProxyTransport,
url: str, timeout: httpx.Timeout = None):
async def fetch(
transport: AsyncProxyTransport,
url: str,
timeout: httpx.Timeout = None,
):
async with httpx.AsyncClient(transport=transport) as client:
res = await client.get(url=url, timeout=timeout)
return res
Expand Down Expand Up @@ -80,7 +84,7 @@ async def main():
verify=create_ssl_context(url)
)
timeout = httpx.Timeout(2, connect=32)
with pytest.raises(httpx.ReadTimeout):
with pytest.raises(httpcore.ReadTimeout):
await fetch(transport=transport, url=url, timeout=timeout)

curio.run(main)
Expand All @@ -106,7 +110,6 @@ async def main():
def test_socks5_proxy_with_invalid_proxy_port(
unused_tcp_port,
url=TEST_URL_IPV4):

async def main():
transport = AsyncProxyTransport(
proxy_type=ProxyType.SOCKS5,
Expand Down
12 changes: 8 additions & 4 deletions tests/test_transport_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ssl

import httpcore
import httpx
import pytest # noqa
from yarl import URL # noqa
Expand Down Expand Up @@ -29,9 +30,12 @@ def create_ssl_context(url):
return None


def fetch(transport: SyncProxyTransport,
url: str, timeout: httpx.Timeout = None):
with httpx.Client(transport=transport) as client:
def fetch(
transport: SyncProxyTransport,
url: str,
timeout: httpx.Timeout = None,
):
with httpx.Client(transport=transport) as client: # type: ignore
res = client.get(url=url, timeout=timeout)
return res

Expand Down Expand Up @@ -71,7 +75,7 @@ def test_socks5_proxy_with_read_timeout(url=TEST_URL_IPV4_DELAY):
verify=create_ssl_context(url)
)
timeout = httpx.Timeout(2, connect=32)
with pytest.raises(httpx.ReadTimeout):
with pytest.raises(httpcore.ReadTimeout):
fetch(transport=transport, url=url, timeout=timeout)


Expand Down
10 changes: 7 additions & 3 deletions tests/test_transport_trio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ssl

import httpcore
import httpx
import pytest # noqa
from yarl import URL # noqa
Expand Down Expand Up @@ -30,8 +31,11 @@ def create_ssl_context(url):
return None


async def fetch(transport: AsyncProxyTransport,
url: str, timeout: httpx.Timeout = None):
async def fetch(
transport: AsyncProxyTransport,
url: str,
timeout: httpx.Timeout = None,
):
async with httpx.AsyncClient(transport=transport) as client:
res = await client.get(url=url, timeout=timeout)
return res
Expand Down Expand Up @@ -75,7 +79,7 @@ async def test_socks5_proxy_with_read_timeout(url=TEST_URL_IPV4_DELAY):
verify=create_ssl_context(url)
)
timeout = httpx.Timeout(2, connect=32)
with pytest.raises(httpx.ReadTimeout):
with pytest.raises(httpcore.ReadTimeout):
await fetch(transport=transport, url=url, timeout=timeout)


Expand Down

0 comments on commit e15ed37

Please sign in to comment.