From ea2cdd23c26e271e278bc49e50eae2691a3aa4a5 Mon Sep 17 00:00:00 2001 From: Steve B Date: Wed, 28 Oct 2020 02:22:28 +0100 Subject: [PATCH] [python] add socket_options to configuration for the rest client (#7795) * [python] add socket_options to configuration for the rest client * (python-experimental) add socket_options to configuration for the rest client This mirrors work done on the python generator * (python-experimental) add test to cover socket_options is passed on from configuration Co-authored-by: steve brazier --- .../resources/python/configuration.mustache | 4 ++ .../configuration.mustache | 3 + .../python/python-experimental/rest.mustache | 3 + .../src/main/resources/python/rest.mustache | 3 + .../petstore_api/configuration.py | 4 ++ .../petstore_api/configuration.py | 3 + .../python-experimental/petstore_api/rest.py | 3 + .../petstore_api/configuration.py | 4 ++ .../python/petstore_api/configuration.py | 4 ++ .../petstore/python/petstore_api/rest.py | 3 + .../x_auth_id_alias/configuration.py | 3 + .../x_auth_id_alias/rest.py | 3 + .../dynamic_servers/configuration.py | 3 + .../dynamic_servers/rest.py | 3 + .../petstore_api/configuration.py | 3 + .../python-experimental/petstore_api/rest.py | 3 + .../python-experimental/testfiles/rsa.pem | 15 ----- .../test_extra_pool_config_options.py | 61 +++++++++++++++++++ .../python/petstore_api/configuration.py | 4 ++ .../petstore/python/petstore_api/rest.py | 3 + 20 files changed, 120 insertions(+), 15 deletions(-) delete mode 100644 samples/openapi3/client/petstore/python-experimental/testfiles/rsa.pem create mode 100644 samples/openapi3/client/petstore/python-experimental/tests_manual/test_extra_pool_config_options.py diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 8025a1b655a..9f2542f5a91 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -302,6 +302,10 @@ conf = {{{packageName}}}.Configuration( # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/configuration.mustache index 8b911bdddd9..ca6fc37747f 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/configuration.mustache @@ -299,6 +299,9 @@ conf = {{{packageName}}}.Configuration( # Enable client side validation self.client_side_validation = True + # Options to pass down to the underlying urllib3 socket + self.socket_options = None + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/modules/openapi-generator/src/main/resources/python/python-experimental/rest.mustache b/modules/openapi-generator/src/main/resources/python/python-experimental/rest.mustache index 746f9844344..5fb2da0af28 100644 --- a/modules/openapi-generator/src/main/resources/python/python-experimental/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/python-experimental/rest.mustache @@ -64,6 +64,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index 05b46dadc16..39168786244 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -68,6 +68,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/client/petstore/python-asyncio/petstore_api/configuration.py b/samples/client/petstore/python-asyncio/petstore_api/configuration.py index ce419432358..42f9f939bec 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/configuration.py +++ b/samples/client/petstore/python-asyncio/petstore_api/configuration.py @@ -226,6 +226,10 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/client/petstore/python-experimental/petstore_api/configuration.py b/samples/client/petstore/python-experimental/petstore_api/configuration.py index b7e866546d0..516b0339732 100644 --- a/samples/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/client/petstore/python-experimental/petstore_api/configuration.py @@ -227,6 +227,9 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + # Options to pass down to the underlying urllib3 socket + self.socket_options = None + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/client/petstore/python-experimental/petstore_api/rest.py b/samples/client/petstore/python-experimental/petstore_api/rest.py index 8ca3604ebbc..9537cea6b1a 100644 --- a/samples/client/petstore/python-experimental/petstore_api/rest.py +++ b/samples/client/petstore/python-experimental/petstore_api/rest.py @@ -72,6 +72,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/client/petstore/python-tornado/petstore_api/configuration.py b/samples/client/petstore/python-tornado/petstore_api/configuration.py index 81b2cd7e9d0..71e6a7117f8 100644 --- a/samples/client/petstore/python-tornado/petstore_api/configuration.py +++ b/samples/client/petstore/python-tornado/petstore_api/configuration.py @@ -230,6 +230,10 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/client/petstore/python/petstore_api/configuration.py b/samples/client/petstore/python/petstore_api/configuration.py index 81b2cd7e9d0..71e6a7117f8 100644 --- a/samples/client/petstore/python/petstore_api/configuration.py +++ b/samples/client/petstore/python/petstore_api/configuration.py @@ -230,6 +230,10 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/client/petstore/python/petstore_api/rest.py b/samples/client/petstore/python/petstore_api/rest.py index 9cbcd62db56..17e85148bb3 100644 --- a/samples/client/petstore/python/petstore_api/rest.py +++ b/samples/client/petstore/python/petstore_api/rest.py @@ -76,6 +76,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/configuration.py b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/configuration.py index 5852bbc6b55..185497e8705 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/configuration.py +++ b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/configuration.py @@ -208,6 +208,9 @@ conf = x_auth_id_alias.Configuration( # Enable client side validation self.client_side_validation = True + # Options to pass down to the underlying urllib3 socket + self.socket_options = None + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/rest.py b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/rest.py index a7fc81ae0aa..dc890689357 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/rest.py +++ b/samples/openapi3/client/extensions/x-auth-id-alias/python-experimental/x_auth_id_alias/rest.py @@ -72,6 +72,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/configuration.py b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/configuration.py index 3bdb2bf6962..0e6b420ab0f 100644 --- a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/configuration.py +++ b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/configuration.py @@ -188,6 +188,9 @@ class Configuration(object): # Enable client side validation self.client_side_validation = True + # Options to pass down to the underlying urllib3 socket + self.socket_options = None + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/rest.py b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/rest.py index b277cbb9ff1..bf61d50d9c1 100644 --- a/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/rest.py +++ b/samples/openapi3/client/features/dynamic-servers/python-experimental/dynamic_servers/rest.py @@ -72,6 +72,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py index 14beaba9065..ca58bb0fc10 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/configuration.py @@ -274,6 +274,9 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + # Options to pass down to the underlying urllib3 socket + self.socket_options = None + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/openapi3/client/petstore/python-experimental/petstore_api/rest.py b/samples/openapi3/client/petstore/python-experimental/petstore_api/rest.py index 8ca3604ebbc..9537cea6b1a 100644 --- a/samples/openapi3/client/petstore/python-experimental/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-experimental/petstore_api/rest.py @@ -72,6 +72,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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 diff --git a/samples/openapi3/client/petstore/python-experimental/testfiles/rsa.pem b/samples/openapi3/client/petstore/python-experimental/testfiles/rsa.pem deleted file mode 100644 index ddc2c9767c4..00000000000 --- a/samples/openapi3/client/petstore/python-experimental/testfiles/rsa.pem +++ /dev/null @@ -1,15 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF -NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F -UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB -AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ+UuvMNUCdN2QPc4sBiA -QWvLw1cSKt5DsKZ8UETpYPy8pPYnnDEz2dDYiaew9+xEpubyeW2oH4Zx71wqBtOK -kqwrXa/pzdpiucRRjk6vE6YY7EBBs/g7uanVpGibOVAEsqH1AkEA7DkjVH28WDUg -f1nqvfn2Kj6CT7nIcE3jGJsZZ7zlZmBmHFDONMLUrXR/Zm3pR5m0tCmBqa5RK95u -412jt1dPIwJBANJT3v8pnkth48bQo/fKel6uEYyboRtA5/uHuHkZ6FQF7OUkGogc -mSJluOdc5t6hI1VsLn0QZEjQZMEOWr+wKSMCQQCC4kXJEsHAve77oP6HtG/IiEn7 -kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA -gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW -G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI -7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA== ------END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/samples/openapi3/client/petstore/python-experimental/tests_manual/test_extra_pool_config_options.py b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_extra_pool_config_options.py new file mode 100644 index 00000000000..5bf6b989ebe --- /dev/null +++ b/samples/openapi3/client/petstore/python-experimental/tests_manual/test_extra_pool_config_options.py @@ -0,0 +1,61 @@ +import unittest +from unittest.mock import patch + +import petstore_api + + +class StubPoolManager(object): + actual_kwargs = None + + def __init__(self, num_pools=10, headers=None, **kwargs): + # Matches the contract of urllib3.PoolManager + self.actual_kwargs = kwargs + + +class StubProxyManager: + actual_kwargs = None + + def __init__( + self, + proxy_url, + num_pools=10, + headers=None, + proxy_headers=None, + **kwargs + ): + # Matches the contract of urllib3.ProxyManager + self.actual_kwargs = kwargs + + +class TestExtraOptionsForPools(unittest.TestCase): + + def test_socket_options_get_passed_to_pool_manager(self): + + socket_options = ["extra", "socket", "options"] + + config = petstore_api.Configuration(host="HOST") + config.socket_options = socket_options + + with patch("petstore_api.rest.urllib3.PoolManager", StubPoolManager): + api_client = petstore_api.ApiClient(config) + + # urllib3.PoolManager promises to pass socket_options in kwargs + # to the underlying socket. So asserting that our manager + # gets it is a good start + assert api_client.rest_client.pool_manager.actual_kwargs["socket_options"] == socket_options + + def test_socket_options_get_passed_to_proxy_manager(self): + + socket_options = ["extra", "socket", "options"] + + config = petstore_api.Configuration(host="HOST") + config.socket_options = socket_options + config.proxy = True + + with patch("petstore_api.rest.urllib3.ProxyManager", StubProxyManager): + api_client = petstore_api.ApiClient(config) + + # urllib3.ProxyManager promises to pass socket_options in kwargs + # to the underlying socket. So asserting that our manager + # gets it is a good start + assert api_client.rest_client.pool_manager.actual_kwargs["socket_options"] == socket_options diff --git a/samples/openapi3/client/petstore/python/petstore_api/configuration.py b/samples/openapi3/client/petstore/python/petstore_api/configuration.py index e889b494727..f6d116438da 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python/petstore_api/configuration.py @@ -277,6 +277,10 @@ conf = petstore_api.Configuration( # Enable client side validation self.client_side_validation = True + self.socket_options = None + """Options to pass down to the underlying urllib3 socket + """ + def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) diff --git a/samples/openapi3/client/petstore/python/petstore_api/rest.py b/samples/openapi3/client/petstore/python/petstore_api/rest.py index 9cbcd62db56..17e85148bb3 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python/petstore_api/rest.py @@ -76,6 +76,9 @@ class RESTClientObject(object): if configuration.retries is not None: addition_pool_args['retries'] = configuration.retries + 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