From 467cd9418a7e876b8a783d3fa3afc06d8f45c031 Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Wed, 25 Oct 2023 10:05:45 -0700 Subject: [PATCH 1/6] add socks5 proxy support (requires additional import) --- .../python-pydantic-v1/rest.mustache | 36 +++++++++++++------ .../src/main/resources/python/rest.mustache | 36 +++++++++++++------ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache index f49dee2bed49..96b78e0a753c 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache @@ -16,6 +16,7 @@ from {{packageName}}.exceptions import ApiException, UnauthorizedException, Forb logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -71,17 +72,30 @@ class RESTClientObject: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index f49dee2bed49..96b78e0a753c 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -16,6 +16,7 @@ from {{packageName}}.exceptions import ApiException, UnauthorizedException, Forb logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -71,17 +72,30 @@ class RESTClientObject: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, From f75b0b083320be9936f9f13182101e36f53a23b7 Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Wed, 25 Oct 2023 13:37:50 -0700 Subject: [PATCH 2/6] updated examples --- .../openapi_client/rest.py | 36 +++++++++++++------ .../python-pydantic-v1/openapi_client/rest.py | 36 +++++++++++++------ .../echo_api/python/openapi_client/rest.py | 36 +++++++++++++------ .../python-pydantic-v1/petstore_api/rest.py | 36 +++++++++++++------ .../petstore/python/petstore_api/rest.py | 36 +++++++++++++------ 5 files changed, 125 insertions(+), 55 deletions(-) diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py index d05e077260de..2ed2af158a91 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py @@ -27,6 +27,7 @@ logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -82,17 +83,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py index d05e077260de..2ed2af158a91 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py @@ -27,6 +27,7 @@ logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -82,17 +83,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index d05e077260de..2ed2af158a91 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -27,6 +27,7 @@ logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -82,17 +83,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py index df9aca008048..071690c768ba 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py @@ -26,6 +26,7 @@ logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -81,17 +82,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index df9aca008048..071690c768ba 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -26,6 +26,7 @@ logger = logging.getLogger(__name__) +SOCKS5_PROXY_PREFIX = "sock5://" class RESTResponse(io.IOBase): @@ -81,17 +82,30 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, - cert_reqs=cert_reqs, - ca_certs=configuration.ssl_ca_cert, - cert_file=configuration.cert_file, - key_file=configuration.key_file, - proxy_url=configuration.proxy, - proxy_headers=configuration.proxy_headers, - **addition_pool_args - ) + if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + from urllib3.contrib.socks import SOCKSProxyManager + self.pool_manager = SOCKSProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=ca_certs, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + **addition_pool_args + ) + else: + self.pool_manager = urllib3.ProxyManager( + num_pools=pools_size, + maxsize=maxsize, + cert_reqs=cert_reqs, + ca_certs=configuration.ssl_ca_cert, + cert_file=configuration.cert_file, + key_file=configuration.key_file, + proxy_url=configuration.proxy, + proxy_headers=configuration.proxy_headers, + **addition_pool_args + ) else: self.pool_manager = urllib3.PoolManager( num_pools=pools_size, From d4b8f9b721318177281407644d5971493a134803 Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Fri, 10 Nov 2023 18:01:54 -0800 Subject: [PATCH 3/6] build samples. updated to support pydantic python option --- .../python-pydantic-v1/rest.mustache | 15 +- .../src/main/resources/python/rest.mustache | 2 + .../openapi_client/rest.py | 240 +++++++----------- .../python-pydantic-v1/openapi_client/rest.py | 15 +- .../echo_api/python/openapi_client/rest.py | 240 +++++++----------- .../python-pydantic-v1/petstore_api/rest.py | 15 +- .../petstore/python/petstore_api/rest.py | 240 +++++++----------- 7 files changed, 311 insertions(+), 456 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache index 96b78e0a753c..c27a3f915466 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache @@ -16,7 +16,18 @@ from {{packageName}}.exceptions import ApiException, UnauthorizedException, Forb logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} + + +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 + class RESTResponse(io.IOBase): @@ -72,7 +83,7 @@ class RESTClientObject: # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index c486f1fced01..2241e14babcc 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -14,6 +14,7 @@ from {{packageName}}.exceptions import ApiException, ApiValueError SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} RESTResponseType = urllib3.HTTPResponse + def is_socks_proxy_url(url): if url is None: return False @@ -23,6 +24,7 @@ def is_socks_proxy_url(url): else: return split_section[0].lower() in SUPPORTED_SOCKS_PROXIES + class RESTResponse(io.IOBase): def __init__(self, resp) -> None: diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py index 2ed2af158a91..1ff868d2128f 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py @@ -15,44 +15,55 @@ import io import json -import logging import re import ssl -from urllib.parse import urlencode, quote_plus import urllib3 -from openapi_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException +from openapi_client.exceptions import ApiException, ApiValueError +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +RESTResponseType = urllib3.HTTPResponse -logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +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 + class RESTResponse(io.IOBase): def __init__(self, resp) -> None: - self.urllib3_response = resp + self.response = resp self.status = resp.status self.reason = resp.reason - self.data = resp.data + self.data = None + + def read(self): + if self.data is None: + self.data = self.response.data + return self.data def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.headers + return self.response.headers def getheader(self, name, default=None): """Returns a given response header.""" - return self.urllib3_response.headers.get(name, default) + return self.response.headers.get(name, default) class RESTClientObject: - def __init__(self, configuration, pools_size=4, maxsize=None) -> None: + def __init__(self, configuration) -> None: # urllib3.PoolManager will pass all kw parameters to connectionpool # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 - # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 # cert_reqs @@ -63,7 +74,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: addition_pool_args = {} if configuration.assert_hostname is not None: - addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 + addition_pool_args['assert_hostname'] = ( + configuration.assert_hostname + ) if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries @@ -75,15 +88,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: if configuration.socket_options is not None: addition_pool_args['socket_options'] = configuration.socket_options - if maxsize is None: - if configuration.connection_pool_maxsize is not None: - maxsize = configuration.connection_pool_maxsize - else: - maxsize = 4 - # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, @@ -93,12 +100,11 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -109,8 +115,6 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: ) else: self.pool_manager = urllib3.PoolManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -118,30 +122,39 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: **addition_pool_args ) - def request(self, method, url, query_params=None, headers=None, - body=None, post_params=None, _preload_content=True, - _request_timeout=None): + def request( + self, + method, + url, + headers=None, + body=None, + post_params=None, + _request_timeout=None + ): """Perform requests. :param method: http request method :param url: http request url - :param query_params: query parameters in the url :param headers: http request headers :param body: request json body, for `application/json` :param post_params: request post parameters, `application/x-www-form-urlencoded` and `multipart/form-data` - :param _preload_content: if False, the urllib3.HTTPResponse object will - be returned without reading/decoding response - data. Default is True. :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts. """ method = method.upper() - assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', - 'PATCH', 'OPTIONS'] + assert method in [ + 'GET', + 'HEAD', + 'DELETE', + 'POST', + 'PUT', + 'PATCH', + 'OPTIONS' + ] if post_params and body: raise ApiValueError( @@ -150,65 +163,78 @@ def request(self, method, url, query_params=None, headers=None, post_params = post_params or {} headers = headers or {} - # url already contains the URL query string - # so reset query_params to empty dict - query_params = {} timeout = None if _request_timeout: - if isinstance(_request_timeout, (int,float)): # noqa: E501,F821 + if isinstance(_request_timeout, (int, float)): timeout = urllib3.Timeout(total=_request_timeout) - elif (isinstance(_request_timeout, tuple) and - len(_request_timeout) == 2): + elif ( + isinstance(_request_timeout, tuple) + and len(_request_timeout) == 2 + ): timeout = urllib3.Timeout( - connect=_request_timeout[0], read=_request_timeout[1]) + connect=_request_timeout[0], + read=_request_timeout[1] + ) try: # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: # no content type provided or payload is json - if not headers.get('Content-Type') or re.search('json', headers['Content-Type'], re.IGNORECASE): + content_type = headers.get('Content-Type') + if ( + not content_type + or re.search('json', content_type, re.IGNORECASE) + ): request_body = None if body is not None: request_body = json.dumps(body) r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 + headers=headers, + preload_content=False + ) + elif content_type == 'application/x-www-form-urlencoded': r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=False, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'multipart/form-data': + headers=headers, + preload_content=False + ) + elif content_type == 'multipart/form-data': # must del headers['Content-Type'], or the correct # Content-Type which generated by urllib3 will be # overwritten. del headers['Content-Type'] r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=True, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is # provided in serialized form elif isinstance(body, str) or isinstance(body, bytes): request_body = body r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) else: # Cannot generate the request from given parameters msg = """Cannot prepare a request message for provided @@ -217,102 +243,16 @@ def request(self, method, url, query_params=None, headers=None, raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: - r = self.pool_manager.request(method, url, - fields={}, - preload_content=_preload_content, - timeout=timeout, - headers=headers) + r = self.pool_manager.request( + method, + url, + fields={}, + timeout=timeout, + headers=headers, + preload_content=False + ) except urllib3.exceptions.SSLError as e: - msg = "{0}\n{1}".format(type(e).__name__, str(e)) + msg = "\n".join([type(e).__name__, str(e)]) raise ApiException(status=0, reason=msg) - if _preload_content: - r = RESTResponse(r) - - # log response body - logger.debug("response body: %s", r.data) - - if not 200 <= r.status <= 299: - if r.status == 400: - raise BadRequestException(http_resp=r) - - if r.status == 401: - raise UnauthorizedException(http_resp=r) - - if r.status == 403: - raise ForbiddenException(http_resp=r) - - if r.status == 404: - raise NotFoundException(http_resp=r) - - if 500 <= r.status <= 599: - raise ServiceException(http_resp=r) - - raise ApiException(http_resp=r) - - return r - - def get_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("GET", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def head_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("HEAD", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def options_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("OPTIONS", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def delete_request(self, url, headers=None, query_params=None, body=None, - _preload_content=True, _request_timeout=None): - return self.request("DELETE", url, - headers=headers, - query_params=query_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def post_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("POST", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def put_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PUT", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def patch_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PATCH", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) + return RESTResponse(r) diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py index 2ed2af158a91..4928097f6ba1 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py @@ -27,7 +27,18 @@ logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} + + +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 + class RESTResponse(io.IOBase): @@ -83,7 +94,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index 2ed2af158a91..1ff868d2128f 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -15,44 +15,55 @@ import io import json -import logging import re import ssl -from urllib.parse import urlencode, quote_plus import urllib3 -from openapi_client.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException +from openapi_client.exceptions import ApiException, ApiValueError +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +RESTResponseType = urllib3.HTTPResponse -logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +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 + class RESTResponse(io.IOBase): def __init__(self, resp) -> None: - self.urllib3_response = resp + self.response = resp self.status = resp.status self.reason = resp.reason - self.data = resp.data + self.data = None + + def read(self): + if self.data is None: + self.data = self.response.data + return self.data def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.headers + return self.response.headers def getheader(self, name, default=None): """Returns a given response header.""" - return self.urllib3_response.headers.get(name, default) + return self.response.headers.get(name, default) class RESTClientObject: - def __init__(self, configuration, pools_size=4, maxsize=None) -> None: + def __init__(self, configuration) -> None: # urllib3.PoolManager will pass all kw parameters to connectionpool # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 - # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 # cert_reqs @@ -63,7 +74,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: addition_pool_args = {} if configuration.assert_hostname is not None: - addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 + addition_pool_args['assert_hostname'] = ( + configuration.assert_hostname + ) if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries @@ -75,15 +88,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: if configuration.socket_options is not None: addition_pool_args['socket_options'] = configuration.socket_options - if maxsize is None: - if configuration.connection_pool_maxsize is not None: - maxsize = configuration.connection_pool_maxsize - else: - maxsize = 4 - # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, @@ -93,12 +100,11 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -109,8 +115,6 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: ) else: self.pool_manager = urllib3.PoolManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -118,30 +122,39 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: **addition_pool_args ) - def request(self, method, url, query_params=None, headers=None, - body=None, post_params=None, _preload_content=True, - _request_timeout=None): + def request( + self, + method, + url, + headers=None, + body=None, + post_params=None, + _request_timeout=None + ): """Perform requests. :param method: http request method :param url: http request url - :param query_params: query parameters in the url :param headers: http request headers :param body: request json body, for `application/json` :param post_params: request post parameters, `application/x-www-form-urlencoded` and `multipart/form-data` - :param _preload_content: if False, the urllib3.HTTPResponse object will - be returned without reading/decoding response - data. Default is True. :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts. """ method = method.upper() - assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', - 'PATCH', 'OPTIONS'] + assert method in [ + 'GET', + 'HEAD', + 'DELETE', + 'POST', + 'PUT', + 'PATCH', + 'OPTIONS' + ] if post_params and body: raise ApiValueError( @@ -150,65 +163,78 @@ def request(self, method, url, query_params=None, headers=None, post_params = post_params or {} headers = headers or {} - # url already contains the URL query string - # so reset query_params to empty dict - query_params = {} timeout = None if _request_timeout: - if isinstance(_request_timeout, (int,float)): # noqa: E501,F821 + if isinstance(_request_timeout, (int, float)): timeout = urllib3.Timeout(total=_request_timeout) - elif (isinstance(_request_timeout, tuple) and - len(_request_timeout) == 2): + elif ( + isinstance(_request_timeout, tuple) + and len(_request_timeout) == 2 + ): timeout = urllib3.Timeout( - connect=_request_timeout[0], read=_request_timeout[1]) + connect=_request_timeout[0], + read=_request_timeout[1] + ) try: # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: # no content type provided or payload is json - if not headers.get('Content-Type') or re.search('json', headers['Content-Type'], re.IGNORECASE): + content_type = headers.get('Content-Type') + if ( + not content_type + or re.search('json', content_type, re.IGNORECASE) + ): request_body = None if body is not None: request_body = json.dumps(body) r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 + headers=headers, + preload_content=False + ) + elif content_type == 'application/x-www-form-urlencoded': r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=False, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'multipart/form-data': + headers=headers, + preload_content=False + ) + elif content_type == 'multipart/form-data': # must del headers['Content-Type'], or the correct # Content-Type which generated by urllib3 will be # overwritten. del headers['Content-Type'] r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=True, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is # provided in serialized form elif isinstance(body, str) or isinstance(body, bytes): request_body = body r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) else: # Cannot generate the request from given parameters msg = """Cannot prepare a request message for provided @@ -217,102 +243,16 @@ def request(self, method, url, query_params=None, headers=None, raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: - r = self.pool_manager.request(method, url, - fields={}, - preload_content=_preload_content, - timeout=timeout, - headers=headers) + r = self.pool_manager.request( + method, + url, + fields={}, + timeout=timeout, + headers=headers, + preload_content=False + ) except urllib3.exceptions.SSLError as e: - msg = "{0}\n{1}".format(type(e).__name__, str(e)) + msg = "\n".join([type(e).__name__, str(e)]) raise ApiException(status=0, reason=msg) - if _preload_content: - r = RESTResponse(r) - - # log response body - logger.debug("response body: %s", r.data) - - if not 200 <= r.status <= 299: - if r.status == 400: - raise BadRequestException(http_resp=r) - - if r.status == 401: - raise UnauthorizedException(http_resp=r) - - if r.status == 403: - raise ForbiddenException(http_resp=r) - - if r.status == 404: - raise NotFoundException(http_resp=r) - - if 500 <= r.status <= 599: - raise ServiceException(http_resp=r) - - raise ApiException(http_resp=r) - - return r - - def get_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("GET", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def head_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("HEAD", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def options_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("OPTIONS", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def delete_request(self, url, headers=None, query_params=None, body=None, - _preload_content=True, _request_timeout=None): - return self.request("DELETE", url, - headers=headers, - query_params=query_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def post_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("POST", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def put_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PUT", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def patch_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PATCH", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) + return RESTResponse(r) diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py index 071690c768ba..5042fc7766bb 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py @@ -26,7 +26,18 @@ logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} + + +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 + class RESTResponse(io.IOBase): @@ -82,7 +93,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index 071690c768ba..026a641beda1 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -14,44 +14,55 @@ import io import json -import logging import re import ssl -from urllib.parse import urlencode, quote_plus import urllib3 -from petstore_api.exceptions import ApiException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException, ApiValueError, BadRequestException +from petstore_api.exceptions import ApiException, ApiValueError +SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +RESTResponseType = urllib3.HTTPResponse -logger = logging.getLogger(__name__) -SOCKS5_PROXY_PREFIX = "sock5://" +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 + class RESTResponse(io.IOBase): def __init__(self, resp) -> None: - self.urllib3_response = resp + self.response = resp self.status = resp.status self.reason = resp.reason - self.data = resp.data + self.data = None + + def read(self): + if self.data is None: + self.data = self.response.data + return self.data def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.headers + return self.response.headers def getheader(self, name, default=None): """Returns a given response header.""" - return self.urllib3_response.headers.get(name, default) + return self.response.headers.get(name, default) class RESTClientObject: - def __init__(self, configuration, pools_size=4, maxsize=None) -> None: + def __init__(self, configuration) -> None: # urllib3.PoolManager will pass all kw parameters to connectionpool # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75 # noqa: E501 # https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680 # noqa: E501 - # maxsize is the number of requests to host that are allowed in parallel # noqa: E501 # Custom SSL certificates and client certificates: http://urllib3.readthedocs.io/en/latest/advanced-usage.html # noqa: E501 # cert_reqs @@ -62,7 +73,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: addition_pool_args = {} if configuration.assert_hostname is not None: - addition_pool_args['assert_hostname'] = configuration.assert_hostname # noqa: E501 + addition_pool_args['assert_hostname'] = ( + configuration.assert_hostname + ) if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries @@ -74,15 +87,9 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: if configuration.socket_options is not None: addition_pool_args['socket_options'] = configuration.socket_options - if maxsize is None: - if configuration.connection_pool_maxsize is not None: - maxsize = configuration.connection_pool_maxsize - else: - maxsize = 4 - # https pool manager if configuration.proxy: - if configuration.proxy.startswith(SOCKS5_PROXY_PREFIX): + if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( num_pools=pools_size, @@ -92,12 +99,11 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: self.pool_manager = urllib3.ProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -108,8 +114,6 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: ) else: self.pool_manager = urllib3.PoolManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, @@ -117,30 +121,39 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: **addition_pool_args ) - def request(self, method, url, query_params=None, headers=None, - body=None, post_params=None, _preload_content=True, - _request_timeout=None): + def request( + self, + method, + url, + headers=None, + body=None, + post_params=None, + _request_timeout=None + ): """Perform requests. :param method: http request method :param url: http request url - :param query_params: query parameters in the url :param headers: http request headers :param body: request json body, for `application/json` :param post_params: request post parameters, `application/x-www-form-urlencoded` and `multipart/form-data` - :param _preload_content: if False, the urllib3.HTTPResponse object will - be returned without reading/decoding response - data. Default is True. :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts. """ method = method.upper() - assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', - 'PATCH', 'OPTIONS'] + assert method in [ + 'GET', + 'HEAD', + 'DELETE', + 'POST', + 'PUT', + 'PATCH', + 'OPTIONS' + ] if post_params and body: raise ApiValueError( @@ -149,65 +162,78 @@ def request(self, method, url, query_params=None, headers=None, post_params = post_params or {} headers = headers or {} - # url already contains the URL query string - # so reset query_params to empty dict - query_params = {} timeout = None if _request_timeout: - if isinstance(_request_timeout, (int,float)): # noqa: E501,F821 + if isinstance(_request_timeout, (int, float)): timeout = urllib3.Timeout(total=_request_timeout) - elif (isinstance(_request_timeout, tuple) and - len(_request_timeout) == 2): + elif ( + isinstance(_request_timeout, tuple) + and len(_request_timeout) == 2 + ): timeout = urllib3.Timeout( - connect=_request_timeout[0], read=_request_timeout[1]) + connect=_request_timeout[0], + read=_request_timeout[1] + ) try: # For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE` if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: # no content type provided or payload is json - if not headers.get('Content-Type') or re.search('json', headers['Content-Type'], re.IGNORECASE): + content_type = headers.get('Content-Type') + if ( + not content_type + or re.search('json', content_type, re.IGNORECASE) + ): request_body = None if body is not None: request_body = json.dumps(body) r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'application/x-www-form-urlencoded': # noqa: E501 + headers=headers, + preload_content=False + ) + elif content_type == 'application/x-www-form-urlencoded': r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=False, - preload_content=_preload_content, timeout=timeout, - headers=headers) - elif headers['Content-Type'] == 'multipart/form-data': + headers=headers, + preload_content=False + ) + elif content_type == 'multipart/form-data': # must del headers['Content-Type'], or the correct # Content-Type which generated by urllib3 will be # overwritten. del headers['Content-Type'] r = self.pool_manager.request( - method, url, + method, + url, fields=post_params, encode_multipart=True, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) # Pass a `string` parameter directly in the body to support # other content types than Json when `body` argument is # provided in serialized form elif isinstance(body, str) or isinstance(body, bytes): request_body = body r = self.pool_manager.request( - method, url, + method, + url, body=request_body, - preload_content=_preload_content, timeout=timeout, - headers=headers) + headers=headers, + preload_content=False + ) else: # Cannot generate the request from given parameters msg = """Cannot prepare a request message for provided @@ -216,102 +242,16 @@ def request(self, method, url, query_params=None, headers=None, raise ApiException(status=0, reason=msg) # For `GET`, `HEAD` else: - r = self.pool_manager.request(method, url, - fields={}, - preload_content=_preload_content, - timeout=timeout, - headers=headers) + r = self.pool_manager.request( + method, + url, + fields={}, + timeout=timeout, + headers=headers, + preload_content=False + ) except urllib3.exceptions.SSLError as e: - msg = "{0}\n{1}".format(type(e).__name__, str(e)) + msg = "\n".join([type(e).__name__, str(e)]) raise ApiException(status=0, reason=msg) - if _preload_content: - r = RESTResponse(r) - - # log response body - logger.debug("response body: %s", r.data) - - if not 200 <= r.status <= 299: - if r.status == 400: - raise BadRequestException(http_resp=r) - - if r.status == 401: - raise UnauthorizedException(http_resp=r) - - if r.status == 403: - raise ForbiddenException(http_resp=r) - - if r.status == 404: - raise NotFoundException(http_resp=r) - - if 500 <= r.status <= 599: - raise ServiceException(http_resp=r) - - raise ApiException(http_resp=r) - - return r - - def get_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("GET", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def head_request(self, url, headers=None, query_params=None, _preload_content=True, - _request_timeout=None): - return self.request("HEAD", url, - headers=headers, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - query_params=query_params) - - def options_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("OPTIONS", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def delete_request(self, url, headers=None, query_params=None, body=None, - _preload_content=True, _request_timeout=None): - return self.request("DELETE", url, - headers=headers, - query_params=query_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def post_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("POST", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def put_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PUT", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) - - def patch_request(self, url, headers=None, query_params=None, post_params=None, - body=None, _preload_content=True, _request_timeout=None): - return self.request("PATCH", url, - headers=headers, - query_params=query_params, - post_params=post_params, - _preload_content=_preload_content, - _request_timeout=_request_timeout, - body=body) + return RESTResponse(r) From a5139453c41f740169953b44c4c5d99f9740273e Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Fri, 10 Nov 2023 19:23:48 -0800 Subject: [PATCH 4/6] rename sock to socks for correct protocol name --- .../src/main/resources/python-pydantic-v1/rest.mustache | 2 +- .../openapi-generator/src/main/resources/python/rest.mustache | 2 +- .../openapi_client/rest.py | 2 +- .../client/echo_api/python-pydantic-v1/openapi_client/rest.py | 2 +- samples/client/echo_api/python/openapi_client/rest.py | 2 +- .../client/petstore/python-pydantic-v1/petstore_api/rest.py | 2 +- samples/openapi3/client/petstore/python/petstore_api/rest.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache index c27a3f915466..7119e8780da4 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache @@ -16,7 +16,7 @@ from {{packageName}}.exceptions import ApiException, UnauthorizedException, Forb logger = logging.getLogger(__name__) -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} def is_socks_proxy_url(url): diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index 2241e14babcc..a4432f2bd507 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -11,7 +11,7 @@ import urllib3 from {{packageName}}.exceptions import ApiException, ApiValueError -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} RESTResponseType = urllib3.HTTPResponse diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py index 1ff868d2128f..eb50d3c58d46 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py @@ -22,7 +22,7 @@ from openapi_client.exceptions import ApiException, ApiValueError -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} RESTResponseType = urllib3.HTTPResponse diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py index 4928097f6ba1..b2619facb9f6 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py @@ -27,7 +27,7 @@ logger = logging.getLogger(__name__) -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} def is_socks_proxy_url(url): diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index 1ff868d2128f..eb50d3c58d46 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -22,7 +22,7 @@ from openapi_client.exceptions import ApiException, ApiValueError -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} RESTResponseType = urllib3.HTTPResponse diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py index 5042fc7766bb..bdc98d7f9def 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py @@ -26,7 +26,7 @@ logger = logging.getLogger(__name__) -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} def is_socks_proxy_url(url): diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index 026a641beda1..f3510f04434a 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -21,7 +21,7 @@ from petstore_api.exceptions import ApiException, ApiValueError -SUPPORTED_SOCKS_PROXIES = {"sock5", "socks5h", "socks4", "socks4a"} +SUPPORTED_SOCKS_PROXIES = {"socks5", "socks5h", "socks4", "socks4a"} RESTResponseType = urllib3.HTTPResponse From 85b1fb3ea3ab0e512c52430b44774da0c62fc3a2 Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Sat, 11 Nov 2023 09:59:43 -0800 Subject: [PATCH 5/6] add proxy headers for pydantic --- .../src/main/resources/python-pydantic-v1/rest.mustache | 1 + .../client/echo_api/python-pydantic-v1/openapi_client/rest.py | 1 + .../client/petstore/python-pydantic-v1/petstore_api/rest.py | 1 + 3 files changed, 3 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache index 7119e8780da4..ee2841e6e09f 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache @@ -93,6 +93,7 @@ class RESTClientObject: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py index b2619facb9f6..8a0b044988e8 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py @@ -104,6 +104,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py index bdc98d7f9def..5b9291c3fc06 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py @@ -103,6 +103,7 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, + headers=configuration.proxy_headers, **addition_pool_args ) else: From eb9040fb09971aa0c536df4cb28e7de17030de17 Mon Sep 17 00:00:00 2001 From: Rolf Rando Date: Sat, 11 Nov 2023 11:22:20 -0800 Subject: [PATCH 6/6] fixed param changes from conflict resolution --- .../src/main/resources/python-pydantic-v1/rest.mustache | 4 +--- .../openapi-generator/src/main/resources/python/rest.mustache | 4 +--- .../openapi_client/rest.py | 4 +--- .../client/echo_api/python-pydantic-v1/openapi_client/rest.py | 4 +--- samples/client/echo_api/python/openapi_client/rest.py | 4 +--- .../client/petstore/python-pydantic-v1/petstore_api/rest.py | 4 +--- samples/openapi3/client/petstore/python/petstore_api/rest.py | 4 +--- 7 files changed, 7 insertions(+), 21 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache index ee2841e6e09f..1d6d255d2cd5 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/rest.mustache @@ -86,10 +86,8 @@ class RESTClientObject: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index a4432f2bd507..963dcbf5ad17 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -82,10 +82,8 @@ class RESTClientObject: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py index eb50d3c58d46..979a159d8ab2 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/rest.py @@ -93,10 +93,8 @@ def __init__(self, configuration) -> None: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py index 8a0b044988e8..f4a710085985 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/rest.py @@ -97,10 +97,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/samples/client/echo_api/python/openapi_client/rest.py b/samples/client/echo_api/python/openapi_client/rest.py index eb50d3c58d46..979a159d8ab2 100644 --- a/samples/client/echo_api/python/openapi_client/rest.py +++ b/samples/client/echo_api/python/openapi_client/rest.py @@ -93,10 +93,8 @@ def __init__(self, configuration) -> None: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py index 5b9291c3fc06..fcbbdab49ab9 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/rest.py @@ -96,10 +96,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None) -> None: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy, diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index f3510f04434a..23fab5cb54d6 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -92,10 +92,8 @@ def __init__(self, configuration) -> None: if is_socks_proxy_url(configuration.proxy): from urllib3.contrib.socks import SOCKSProxyManager self.pool_manager = SOCKSProxyManager( - num_pools=pools_size, - maxsize=maxsize, cert_reqs=cert_reqs, - ca_certs=ca_certs, + ca_certs=configuration.ssl_ca_cert, cert_file=configuration.cert_file, key_file=configuration.key_file, proxy_url=configuration.proxy,