Skip to content

Commit e16cd3d

Browse files
committed
python: Update samples
1 parent 9d77691 commit e16cd3d

7 files changed

Lines changed: 129 additions & 12 deletions

File tree

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: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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

@@ -100,14 +102,23 @@ def __init__(self, configuration) -> None:
100102
# https pool manager
101103
self.pool_manager: urllib3.PoolManager
102104

103-
if configuration.proxy:
104-
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):
105116
from urllib3.contrib.socks import SOCKSProxyManager
106-
pool_args["proxy_url"] = configuration.proxy
117+
pool_args["proxy_url"] = proxy
107118
pool_args["headers"] = configuration.proxy_headers
108119
self.pool_manager = SOCKSProxyManager(**pool_args)
109120
else:
110-
pool_args["proxy_url"] = configuration.proxy
121+
pool_args["proxy_url"] = proxy
111122
pool_args["proxy_headers"] = configuration.proxy_headers
112123
self.pool_manager = urllib3.ProxyManager(**pool_args)
113124
else:
@@ -257,3 +268,27 @@ def request(
257268
raise ApiException(status=0, reason=msg)
258269

259270
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

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: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
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

@@ -100,14 +102,23 @@ def __init__(self, configuration) -> None:
100102
# https pool manager
101103
self.pool_manager: urllib3.PoolManager
102104

103-
if configuration.proxy:
104-
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):
105116
from urllib3.contrib.socks import SOCKSProxyManager
106-
pool_args["proxy_url"] = configuration.proxy
117+
pool_args["proxy_url"] = proxy
107118
pool_args["headers"] = configuration.proxy_headers
108119
self.pool_manager = SOCKSProxyManager(**pool_args)
109120
else:
110-
pool_args["proxy_url"] = configuration.proxy
121+
pool_args["proxy_url"] = proxy
111122
pool_args["proxy_headers"] = configuration.proxy_headers
112123
self.pool_manager = urllib3.ProxyManager(**pool_args)
113124
else:
@@ -257,3 +268,27 @@ def request(
257268
raise ApiException(status=0, reason=msg)
258269

259270
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

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: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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

@@ -99,14 +101,23 @@ def __init__(self, configuration) -> None:
99101
# https pool manager
100102
self.pool_manager: urllib3.PoolManager
101103

102-
if configuration.proxy:
103-
if is_socks_proxy_url(configuration.proxy):
104+
parsed = urlparse(configuration.host)
105+
proxy = getproxies().get(parsed.scheme) if configuration.proxy is None else configuration.proxy
106+
if proxy:
107+
if configuration.no_proxy is not None:
108+
if _proxy_bypass(parsed.netloc, configuration.no_proxy):
109+
proxy = None
110+
elif proxy_bypass(parsed.netloc):
111+
proxy = None
112+
113+
if proxy:
114+
if is_socks_proxy_url(proxy):
104115
from urllib3.contrib.socks import SOCKSProxyManager
105-
pool_args["proxy_url"] = configuration.proxy
116+
pool_args["proxy_url"] = proxy
106117
pool_args["headers"] = configuration.proxy_headers
107118
self.pool_manager = SOCKSProxyManager(**pool_args)
108119
else:
109-
pool_args["proxy_url"] = configuration.proxy
120+
pool_args["proxy_url"] = proxy
110121
pool_args["proxy_headers"] = configuration.proxy_headers
111122
self.pool_manager = urllib3.ProxyManager(**pool_args)
112123
else:
@@ -256,3 +267,27 @@ def request(
256267
raise ApiException(status=0, reason=msg)
257268

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

0 commit comments

Comments
 (0)