Skip to content

Commit 712b936

Browse files
committed
python: update samples
1 parent 359f203 commit 712b936

File tree

7 files changed

+177
-3
lines changed

7 files changed

+177
-3
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 = None
313+
"""bypass proxy for host in the no_proxy list.
314+
"""
312315
self.proxy_headers = None
313316
"""Proxy headers
314317
"""

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515

1616
import io
17+
import ipaddress
1718
import json
1819
import re
1920
import ssl
21+
from urllib.parse import urlparse
22+
from urllib.request import proxy_bypass_environment
2023

2124
import urllib3
2225

@@ -100,7 +103,7 @@ def __init__(self, configuration) -> None:
100103
# https pool manager
101104
self.pool_manager: urllib3.PoolManager
102105

103-
if configuration.proxy:
106+
if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''):
104107
if is_socks_proxy_url(configuration.proxy):
105108
from urllib3.contrib.socks import SOCKSProxyManager
106109
pool_args["proxy_url"] = configuration.proxy
@@ -257,3 +260,54 @@ def request(
257260
raise ApiException(status=0, reason=msg)
258261

259262
return RESTResponse(r)
263+
264+
def is_ipv4(target):
265+
""" Test if IPv4 address or not
266+
"""
267+
try:
268+
chk = ipaddress.IPv4Address(target)
269+
return True
270+
except ipaddress.AddressValueError:
271+
return False
272+
273+
def in_ipv4net(target, net):
274+
""" Test if target belongs to given IPv4 network
275+
"""
276+
try:
277+
nw = ipaddress.IPv4Network(net)
278+
ip = ipaddress.IPv4Address(target)
279+
if ip in nw:
280+
return True
281+
return False
282+
except ipaddress.AddressValueError:
283+
return False
284+
except ipaddress.NetmaskValueError:
285+
return False
286+
287+
def should_bypass_proxies(url, no_proxy=None):
288+
""" Yet another requests.should_bypass_proxies
289+
Test if proxies should not be used for a particular url.
290+
"""
291+
292+
parsed = urlparse(url)
293+
294+
# special cases
295+
if parsed.hostname in [None, '']:
296+
return True
297+
298+
# special cases
299+
if no_proxy in [None , '']:
300+
return False
301+
if no_proxy == '*':
302+
return True
303+
304+
no_proxy = no_proxy.lower().replace(' ','');
305+
entries = (
306+
host for host in no_proxy.split(',') if host
307+
)
308+
309+
if is_ipv4(parsed.hostname):
310+
for item in entries:
311+
if in_ipv4net(parsed.hostname, item):
312+
return True
313+
return proxy_bypass_environment(parsed.hostname, {'no': no_proxy} )

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 = None
313+
"""bypass proxy for host in the no_proxy list.
314+
"""
312315
self.proxy_headers = None
313316
"""Proxy headers
314317
"""

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515

1616
import io
17+
import ipaddress
1718
import json
1819
import re
1920
import ssl
21+
from urllib.parse import urlparse
22+
from urllib.request import proxy_bypass_environment
2023

2124
import urllib3
2225

@@ -100,7 +103,7 @@ def __init__(self, configuration) -> None:
100103
# https pool manager
101104
self.pool_manager: urllib3.PoolManager
102105

103-
if configuration.proxy:
106+
if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''):
104107
if is_socks_proxy_url(configuration.proxy):
105108
from urllib3.contrib.socks import SOCKSProxyManager
106109
pool_args["proxy_url"] = configuration.proxy
@@ -257,3 +260,54 @@ def request(
257260
raise ApiException(status=0, reason=msg)
258261

259262
return RESTResponse(r)
263+
264+
def is_ipv4(target):
265+
""" Test if IPv4 address or not
266+
"""
267+
try:
268+
chk = ipaddress.IPv4Address(target)
269+
return True
270+
except ipaddress.AddressValueError:
271+
return False
272+
273+
def in_ipv4net(target, net):
274+
""" Test if target belongs to given IPv4 network
275+
"""
276+
try:
277+
nw = ipaddress.IPv4Network(net)
278+
ip = ipaddress.IPv4Address(target)
279+
if ip in nw:
280+
return True
281+
return False
282+
except ipaddress.AddressValueError:
283+
return False
284+
except ipaddress.NetmaskValueError:
285+
return False
286+
287+
def should_bypass_proxies(url, no_proxy=None):
288+
""" Yet another requests.should_bypass_proxies
289+
Test if proxies should not be used for a particular url.
290+
"""
291+
292+
parsed = urlparse(url)
293+
294+
# special cases
295+
if parsed.hostname in [None, '']:
296+
return True
297+
298+
# special cases
299+
if no_proxy in [None , '']:
300+
return False
301+
if no_proxy == '*':
302+
return True
303+
304+
no_proxy = no_proxy.lower().replace(' ','');
305+
entries = (
306+
host for host in no_proxy.split(',') if host
307+
)
308+
309+
if is_ipv4(parsed.hostname):
310+
for item in entries:
311+
if in_ipv4net(parsed.hostname, item):
312+
return True
313+
return proxy_bypass_environment(parsed.hostname, {'no': no_proxy} )

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 = None
379+
"""bypass proxy for host in the no_proxy list.
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 = None
383+
"""bypass proxy for host in the no_proxy list.
384+
"""
382385
self.proxy_headers = None
383386
"""Proxy headers
384387
"""

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414

1515
import io
16+
import ipaddress
1617
import json
1718
import re
1819
import ssl
20+
from urllib.parse import urlparse
21+
from urllib.request import proxy_bypass_environment
1922

2023
import urllib3
2124

@@ -99,7 +102,7 @@ def __init__(self, configuration) -> None:
99102
# https pool manager
100103
self.pool_manager: urllib3.PoolManager
101104

102-
if configuration.proxy:
105+
if configuration.proxy and not should_bypass_proxies(configuration.host, no_proxy=configuration.no_proxy or ''):
103106
if is_socks_proxy_url(configuration.proxy):
104107
from urllib3.contrib.socks import SOCKSProxyManager
105108
pool_args["proxy_url"] = configuration.proxy
@@ -256,3 +259,54 @@ def request(
256259
raise ApiException(status=0, reason=msg)
257260

258261
return RESTResponse(r)
262+
263+
def is_ipv4(target):
264+
""" Test if IPv4 address or not
265+
"""
266+
try:
267+
chk = ipaddress.IPv4Address(target)
268+
return True
269+
except ipaddress.AddressValueError:
270+
return False
271+
272+
def in_ipv4net(target, net):
273+
""" Test if target belongs to given IPv4 network
274+
"""
275+
try:
276+
nw = ipaddress.IPv4Network(net)
277+
ip = ipaddress.IPv4Address(target)
278+
if ip in nw:
279+
return True
280+
return False
281+
except ipaddress.AddressValueError:
282+
return False
283+
except ipaddress.NetmaskValueError:
284+
return False
285+
286+
def should_bypass_proxies(url, no_proxy=None):
287+
""" Yet another requests.should_bypass_proxies
288+
Test if proxies should not be used for a particular url.
289+
"""
290+
291+
parsed = urlparse(url)
292+
293+
# special cases
294+
if parsed.hostname in [None, '']:
295+
return True
296+
297+
# special cases
298+
if no_proxy in [None , '']:
299+
return False
300+
if no_proxy == '*':
301+
return True
302+
303+
no_proxy = no_proxy.lower().replace(' ','');
304+
entries = (
305+
host for host in no_proxy.split(',') if host
306+
)
307+
308+
if is_ipv4(parsed.hostname):
309+
for item in entries:
310+
if in_ipv4net(parsed.hostname, item):
311+
return True
312+
return proxy_bypass_environment(parsed.hostname, {'no': no_proxy} )

0 commit comments

Comments
 (0)