Skip to content

Commit a7b407b

Browse files
committed
python: Update samples
1 parent 183ee97 commit a7b407b

File tree

7 files changed

+135
-15
lines changed

7 files changed

+135
-15
lines changed

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def __init__(
309309
self.proxy: Optional[str] = None
310310
"""Proxy URL
311311
"""
312+
self.no_proxy: Optional[str] = None
313+
"""Hosts for which to bypass the proxy
314+
"""
312315
self.proxy_headers = None
313316
"""Proxy headers
314317
"""

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
import json
1818
import re
1919
import ssl
20+
from urllib.parse import urlparse
21+
from urllib.request import getproxies, proxy_bypass
2022

2123
import urllib3
2224

25+
from openapi_client.configuration import Configuration
2326
from openapi_client.exceptions import ApiException, ApiValueError
2427

2528
SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
@@ -60,7 +63,7 @@ def getheader(self, name, default=None):
6063

6164
class RESTClientObject:
6265

63-
def __init__(self, configuration) -> None:
66+
def __init__(self, configuration: Configuration) -> None:
6467
# urllib3.PoolManager will pass all kw parameters to connectionpool
6568
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501
6669
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501
@@ -100,14 +103,23 @@ def __init__(self, configuration) -> None:
100103
# https pool manager
101104
self.pool_manager: urllib3.PoolManager
102105

103-
if configuration.proxy:
104-
if is_socks_proxy_url(configuration.proxy):
106+
parsed = urlparse(configuration.host)
107+
proxy = getproxies().get(parsed.scheme) if configuration.proxy is None else configuration.proxy
108+
if proxy:
109+
if configuration.no_proxy is not None:
110+
if _proxy_bypass(parsed.netloc, configuration.no_proxy):
111+
proxy = None
112+
elif proxy_bypass(parsed.netloc):
113+
proxy = None
114+
115+
if proxy:
116+
if is_socks_proxy_url(proxy):
105117
from urllib3.contrib.socks import SOCKSProxyManager
106-
pool_args["proxy_url"] = configuration.proxy
118+
pool_args["proxy_url"] = proxy
107119
pool_args["headers"] = configuration.proxy_headers
108120
self.pool_manager = SOCKSProxyManager(**pool_args)
109121
else:
110-
pool_args["proxy_url"] = configuration.proxy
122+
pool_args["proxy_url"] = proxy
111123
pool_args["proxy_headers"] = configuration.proxy_headers
112124
self.pool_manager = urllib3.ProxyManager(**pool_args)
113125
else:
@@ -257,3 +269,27 @@ def request(
257269
raise ApiException(status=0, reason=msg)
258270

259271
return RESTResponse(r)
272+
273+
# Adapted from urllib.request.proxy_bypass_environment
274+
def _proxy_bypass(netloc: str, no_proxy: str):
275+
if no_proxy == '*':
276+
return True
277+
278+
host = netloc.lower()
279+
if match := re.match(r'(.*):([0-9]*)', host):
280+
hostonly, _port = match.groups()
281+
else:
282+
hostonly = host
283+
284+
for name in no_proxy.split(','):
285+
name = name.strip()
286+
if name:
287+
name = name.lstrip('.')
288+
name = name.lower()
289+
if hostonly == name or host == name:
290+
return True
291+
name = '.' + name
292+
if hostonly.endswith(name) or host.endswith(name):
293+
return True
294+
295+
return False

samples/client/echo_api/python/openapi_client/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def __init__(
309309
self.proxy: Optional[str] = None
310310
"""Proxy URL
311311
"""
312+
self.no_proxy: Optional[str] = None
313+
"""Hosts for which to bypass the proxy
314+
"""
312315
self.proxy_headers = None
313316
"""Proxy headers
314317
"""

samples/client/echo_api/python/openapi_client/rest.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
import json
1818
import re
1919
import ssl
20+
from urllib.parse import urlparse
21+
from urllib.request import getproxies, proxy_bypass
2022

2123
import urllib3
2224

25+
from openapi_client.configuration import Configuration
2326
from openapi_client.exceptions import ApiException, ApiValueError
2427

2528
SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
@@ -60,7 +63,7 @@ def getheader(self, name, default=None):
6063

6164
class RESTClientObject:
6265

63-
def __init__(self, configuration) -> None:
66+
def __init__(self, configuration: Configuration) -> None:
6467
# urllib3.PoolManager will pass all kw parameters to connectionpool
6568
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501
6669
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501
@@ -100,14 +103,23 @@ def __init__(self, configuration) -> None:
100103
# https pool manager
101104
self.pool_manager: urllib3.PoolManager
102105

103-
if configuration.proxy:
104-
if is_socks_proxy_url(configuration.proxy):
106+
parsed = urlparse(configuration.host)
107+
proxy = getproxies().get(parsed.scheme) if configuration.proxy is None else configuration.proxy
108+
if proxy:
109+
if configuration.no_proxy is not None:
110+
if _proxy_bypass(parsed.netloc, configuration.no_proxy):
111+
proxy = None
112+
elif proxy_bypass(parsed.netloc):
113+
proxy = None
114+
115+
if proxy:
116+
if is_socks_proxy_url(proxy):
105117
from urllib3.contrib.socks import SOCKSProxyManager
106-
pool_args["proxy_url"] = configuration.proxy
118+
pool_args["proxy_url"] = proxy
107119
pool_args["headers"] = configuration.proxy_headers
108120
self.pool_manager = SOCKSProxyManager(**pool_args)
109121
else:
110-
pool_args["proxy_url"] = configuration.proxy
122+
pool_args["proxy_url"] = proxy
111123
pool_args["proxy_headers"] = configuration.proxy_headers
112124
self.pool_manager = urllib3.ProxyManager(**pool_args)
113125
else:
@@ -257,3 +269,27 @@ def request(
257269
raise ApiException(status=0, reason=msg)
258270

259271
return RESTResponse(r)
272+
273+
# Adapted from urllib.request.proxy_bypass_environment
274+
def _proxy_bypass(netloc: str, no_proxy: str):
275+
if no_proxy == '*':
276+
return True
277+
278+
host = netloc.lower()
279+
if match := re.match(r'(.*):([0-9]*)', host):
280+
hostonly, _port = match.groups()
281+
else:
282+
hostonly = host
283+
284+
for name in no_proxy.split(','):
285+
name = name.strip()
286+
if name:
287+
name = name.lstrip('.')
288+
name = name.lower()
289+
if hostonly == name or host == name:
290+
return True
291+
name = '.' + name
292+
if hostonly.endswith(name) or host.endswith(name):
293+
return True
294+
295+
return False

samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ def __init__(
375375
self.proxy: Optional[str] = None
376376
"""Proxy URL
377377
"""
378+
self.no_proxy: Optional[str] = None
379+
"""Hosts for which to bypass the proxy
380+
"""
378381
self.proxy_headers = None
379382
"""Proxy headers
380383
"""

samples/openapi3/client/petstore/python/petstore_api/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ def __init__(
379379
self.proxy: Optional[str] = None
380380
"""Proxy URL
381381
"""
382+
self.no_proxy: Optional[str] = None
383+
"""Hosts for which to bypass the proxy
384+
"""
382385
self.proxy_headers = None
383386
"""Proxy headers
384387
"""

samples/openapi3/client/petstore/python/petstore_api/rest.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
import json
1717
import re
1818
import ssl
19+
from urllib.parse import urlparse
20+
from urllib.request import getproxies, proxy_bypass
1921

2022
import urllib3
2123

24+
from petstore_api.configuration import Configuration
2225
from petstore_api.exceptions import ApiException, ApiValueError
2326

2427
SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"}
@@ -59,7 +62,7 @@ def getheader(self, name, default=None):
5962

6063
class RESTClientObject:
6164

62-
def __init__(self, configuration) -> None:
65+
def __init__(self, configuration: Configuration) -> None:
6366
# urllib3.PoolManager will pass all kw parameters to connectionpool
6467
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501
6568
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501
@@ -99,14 +102,23 @@ def __init__(self, configuration) -> None:
99102
# https pool manager
100103
self.pool_manager: urllib3.PoolManager
101104

102-
if configuration.proxy:
103-
if is_socks_proxy_url(configuration.proxy):
105+
parsed = urlparse(configuration.host)
106+
proxy = getproxies().get(parsed.scheme) if configuration.proxy is None else configuration.proxy
107+
if proxy:
108+
if configuration.no_proxy is not None:
109+
if _proxy_bypass(parsed.netloc, configuration.no_proxy):
110+
proxy = None
111+
elif proxy_bypass(parsed.netloc):
112+
proxy = None
113+
114+
if proxy:
115+
if is_socks_proxy_url(proxy):
104116
from urllib3.contrib.socks import SOCKSProxyManager
105-
pool_args["proxy_url"] = configuration.proxy
117+
pool_args["proxy_url"] = proxy
106118
pool_args["headers"] = configuration.proxy_headers
107119
self.pool_manager = SOCKSProxyManager(**pool_args)
108120
else:
109-
pool_args["proxy_url"] = configuration.proxy
121+
pool_args["proxy_url"] = proxy
110122
pool_args["proxy_headers"] = configuration.proxy_headers
111123
self.pool_manager = urllib3.ProxyManager(**pool_args)
112124
else:
@@ -256,3 +268,27 @@ def request(
256268
raise ApiException(status=0, reason=msg)
257269

258270
return RESTResponse(r)
271+
272+
# Adapted from urllib.request.proxy_bypass_environment
273+
def _proxy_bypass(netloc: str, no_proxy: str):
274+
if no_proxy == '*':
275+
return True
276+
277+
host = netloc.lower()
278+
if match := re.match(r'(.*):([0-9]*)', host):
279+
hostonly, _port = match.groups()
280+
else:
281+
hostonly = host
282+
283+
for name in no_proxy.split(','):
284+
name = name.strip()
285+
if name:
286+
name = name.lstrip('.')
287+
name = name.lower()
288+
if hostonly == name or host == name:
289+
return True
290+
name = '.' + name
291+
if hostonly.endswith(name) or host.endswith(name):
292+
return True
293+
294+
return False

0 commit comments

Comments
 (0)