Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ from {{packageName}}.exceptions import ApiException, ApiValueError
RESTResponseType = aiohttp.ClientResponse

ALLOW_RETRY_METHODS = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'})
SUPPORTED_SOCKS_PROXIES = frozenset({'socks4', 'socks4a', 'socks5', 'socks5h'})


class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -65,8 +67,12 @@ class RESTClientObject:
self.ssl_context.check_hostname = False
self.ssl_context.verify_mode = ssl.CERT_NONE

self.proxy = configuration.proxy
proxy = configuration.proxy
proxy_scheme = (proxy or "").partition("://")[0].lower()

self.proxy = proxy
self.proxy_headers = configuration.proxy_headers
self.is_socks_proxy = proxy_scheme in SUPPORTED_SOCKS_PROXIES

retries = configuration.retries
if retries is None:
Expand Down Expand Up @@ -146,9 +152,9 @@ class RESTClientObject:
"headers": headers
}

if self.proxy:
if self.proxy and not self.is_socks_proxy:
args["proxy"] = self.proxy
if self.proxy_headers:
if self.proxy_headers and not self.is_socks_proxy:
args["proxy_headers"] = self.proxy_headers

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
Expand Down Expand Up @@ -200,8 +206,22 @@ class RESTClientObject:

# https pool manager
if self.pool_manager is None:
if self.is_socks_proxy:
# SOCKS proxies require ProxyConnector - aiohttp TCPConnector
# does not support SOCKS schemes
from aiohttp_socks import ProxyConnector
connector = ProxyConnector.from_url(
self.proxy,
limit=self.maxsize,
ssl=self.ssl_context,
)
else:
connector = aiohttp.TCPConnector(
limit=self.maxsize,
ssl=self.ssl_context,
)
self.pool_manager = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(limit=self.maxsize, ssl=self.ssl_context),
connector=connector,
trust_env=True,
)
pool_manager = self.pool_manager
Expand All @@ -216,4 +236,4 @@ class RESTClientObject:

r = await pool_manager.request(**args)

return RESTResponse(r)
return RESTResponse(r)
16 changes: 3 additions & 13 deletions modules/openapi-generator/src/main/resources/python/rest.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ import urllib3

from {{packageName}}.exceptions import ApiException, ApiValueError

SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
RESTResponseType = urllib3.HTTPResponse


def is_socks_proxy_url(url):
if url is None:
return False
split_section = url.split("://")
if len(split_section) < 2:
return False
else:
return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES

SUPPORTED_SOCKS_PROXIES = frozenset({"socks5", "socks5h", "socks4", "socks4a"})

class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -85,7 +75,6 @@ class RESTClientObject:
if configuration.tls_server_name:
pool_args['server_hostname'] = configuration.tls_server_name


if configuration.socket_options is not None:
pool_args['socket_options'] = configuration.socket_options

Expand All @@ -96,7 +85,8 @@ class RESTClientObject:
self.pool_manager: urllib3.PoolManager

if configuration.proxy:
if is_socks_proxy_url(configuration.proxy):
proxy_scheme = configuration.proxy.partition("://")[0].lower()
if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
from urllib3.contrib.socks import SOCKSProxyManager
pool_args["proxy_url"] = configuration.proxy
pool_args["headers"] = configuration.proxy_headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@

from openapi_client.exceptions import ApiException, ApiValueError

SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
RESTResponseType = urllib3.HTTPResponse


def is_socks_proxy_url(url):
if url is None:
return False
split_section = url.split("://")
if len(split_section) < 2:
return False
else:
return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES

SUPPORTED_SOCKS_PROXIES = frozenset({"socks5", "socks5h", "socks4", "socks4a"})

class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -95,7 +85,6 @@ def __init__(self, configuration) -> None:
if configuration.tls_server_name:
pool_args['server_hostname'] = configuration.tls_server_name


if configuration.socket_options is not None:
pool_args['socket_options'] = configuration.socket_options

Expand All @@ -106,7 +95,8 @@ def __init__(self, configuration) -> None:
self.pool_manager: urllib3.PoolManager

if configuration.proxy:
if is_socks_proxy_url(configuration.proxy):
proxy_scheme = configuration.proxy.partition("://")[0].lower()
if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
from urllib3.contrib.socks import SOCKSProxyManager
pool_args["proxy_url"] = configuration.proxy
pool_args["headers"] = configuration.proxy_headers
Expand Down
16 changes: 3 additions & 13 deletions samples/client/echo_api/python/openapi_client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@

from openapi_client.exceptions import ApiException, ApiValueError

SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
RESTResponseType = urllib3.HTTPResponse


def is_socks_proxy_url(url):
if url is None:
return False
split_section = url.split("://")
if len(split_section) < 2:
return False
else:
return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES

SUPPORTED_SOCKS_PROXIES = frozenset({"socks5", "socks5h", "socks4", "socks4a"})

class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -95,7 +85,6 @@ def __init__(self, configuration) -> None:
if configuration.tls_server_name:
pool_args['server_hostname'] = configuration.tls_server_name


if configuration.socket_options is not None:
pool_args['socket_options'] = configuration.socket_options

Expand All @@ -106,7 +95,8 @@ def __init__(self, configuration) -> None:
self.pool_manager: urllib3.PoolManager

if configuration.proxy:
if is_socks_proxy_url(configuration.proxy):
proxy_scheme = configuration.proxy.partition("://")[0].lower()
if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
from urllib3.contrib.socks import SOCKSProxyManager
pool_args["proxy_url"] = configuration.proxy
pool_args["headers"] = configuration.proxy_headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
RESTResponseType = aiohttp.ClientResponse

ALLOW_RETRY_METHODS = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'})
SUPPORTED_SOCKS_PROXIES = frozenset({'socks4', 'socks4a', 'socks5', 'socks5h'})


class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -74,8 +76,12 @@ def __init__(self, configuration) -> None:
self.ssl_context.check_hostname = False
self.ssl_context.verify_mode = ssl.CERT_NONE

self.proxy = configuration.proxy
proxy = configuration.proxy
proxy_scheme = (proxy or "").partition("://")[0].lower()

self.proxy = proxy
self.proxy_headers = configuration.proxy_headers
self.is_socks_proxy = proxy_scheme in SUPPORTED_SOCKS_PROXIES

retries = configuration.retries
if retries is None:
Expand Down Expand Up @@ -155,9 +161,9 @@ async def request(
"headers": headers
}

if self.proxy:
if self.proxy and not self.is_socks_proxy:
args["proxy"] = self.proxy
if self.proxy_headers:
if self.proxy_headers and not self.is_socks_proxy:
args["proxy_headers"] = self.proxy_headers

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
Expand Down Expand Up @@ -209,8 +215,22 @@ async def request(

# https pool manager
if self.pool_manager is None:
if self.is_socks_proxy:
# SOCKS proxies require ProxyConnector - aiohttp TCPConnector
# does not support SOCKS schemes
from aiohttp_socks import ProxyConnector
connector = ProxyConnector.from_url(
self.proxy,
limit=self.maxsize,
ssl=self.ssl_context,
)
else:
connector = aiohttp.TCPConnector(
limit=self.maxsize,
ssl=self.ssl_context,
)
self.pool_manager = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(limit=self.maxsize, ssl=self.ssl_context),
connector=connector,
trust_env=True,
)
pool_manager = self.pool_manager
Expand All @@ -225,4 +245,4 @@ async def request(

r = await pool_manager.request(**args)

return RESTResponse(r)
return RESTResponse(r)
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@

from petstore_api.exceptions import ApiException, ApiValueError

SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
RESTResponseType = urllib3.HTTPResponse


def is_socks_proxy_url(url):
if url is None:
return False
split_section = url.split("://")
if len(split_section) < 2:
return False
else:
return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES

SUPPORTED_SOCKS_PROXIES = frozenset({"socks5", "socks5h", "socks4", "socks4a"})

class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -94,7 +84,6 @@ def __init__(self, configuration) -> None:
if configuration.tls_server_name:
pool_args['server_hostname'] = configuration.tls_server_name


if configuration.socket_options is not None:
pool_args['socket_options'] = configuration.socket_options

Expand All @@ -105,7 +94,8 @@ def __init__(self, configuration) -> None:
self.pool_manager: urllib3.PoolManager

if configuration.proxy:
if is_socks_proxy_url(configuration.proxy):
proxy_scheme = configuration.proxy.partition("://")[0].lower()
if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
from urllib3.contrib.socks import SOCKSProxyManager
pool_args["proxy_url"] = configuration.proxy
pool_args["headers"] = configuration.proxy_headers
Expand Down
16 changes: 3 additions & 13 deletions samples/openapi3/client/petstore/python/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@

from petstore_api.exceptions import ApiException, ApiValueError

SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
RESTResponseType = urllib3.HTTPResponse


def is_socks_proxy_url(url):
if url is None:
return False
split_section = url.split("://")
if len(split_section) < 2:
return False
else:
return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES

SUPPORTED_SOCKS_PROXIES = frozenset({"socks5", "socks5h", "socks4", "socks4a"})

class RESTResponse(io.IOBase):

Expand Down Expand Up @@ -94,7 +84,6 @@ def __init__(self, configuration) -> None:
if configuration.tls_server_name:
pool_args['server_hostname'] = configuration.tls_server_name


if configuration.socket_options is not None:
pool_args['socket_options'] = configuration.socket_options

Expand All @@ -105,7 +94,8 @@ def __init__(self, configuration) -> None:
self.pool_manager: urllib3.PoolManager

if configuration.proxy:
if is_socks_proxy_url(configuration.proxy):
proxy_scheme = configuration.proxy.partition("://")[0].lower()
if proxy_scheme in SUPPORTED_SOCKS_PROXIES:
from urllib3.contrib.socks import SOCKSProxyManager
pool_args["proxy_url"] = configuration.proxy
pool_args["headers"] = configuration.proxy_headers
Expand Down