From 7a0242311fecf97ad8b83e10cc1457a54191073d Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 16 Apr 2020 03:16:08 +0200 Subject: [PATCH] [python/asyncio] fix passing proxy parameters to aiohttp (#5943) --- .../resources/python/asyncio/rest.mustache | 20 ++++++++++--------- .../python-asyncio/petstore_api/rest.py | 20 ++++++++++--------- .../python-asyncio/tests/test_pet_api.py | 13 ++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index e797d0ae7a4..f2099c75312 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -65,16 +65,13 @@ class RESTClientObject(object): ssl=ssl_context ) + self.proxy = configuration.proxy + self.proxy_headers = configuration.proxy_headers + # https pool manager - if configuration.proxy: - self.pool_manager = aiohttp.ClientSession( - connector=connector, - proxy=configuration.proxy - ) - else: - self.pool_manager = aiohttp.ClientSession( - connector=connector - ) + self.pool_manager = aiohttp.ClientSession( + connector=connector + ) async def close(self): await self.pool_manager.close() @@ -122,6 +119,11 @@ class RESTClientObject(object): "headers": headers } + if self.proxy: + args["proxy"] = self.proxy + if self.proxy_headers: + args["proxy_headers"] = self.proxy_headers + if query_params: args["url"] += '?' + urlencode(query_params) diff --git a/samples/client/petstore/python-asyncio/petstore_api/rest.py b/samples/client/petstore/python-asyncio/petstore_api/rest.py index 37616f18536..1394af356ba 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/rest.py +++ b/samples/client/petstore/python-asyncio/petstore_api/rest.py @@ -73,16 +73,13 @@ class RESTClientObject(object): ssl=ssl_context ) + self.proxy = configuration.proxy + self.proxy_headers = configuration.proxy_headers + # https pool manager - if configuration.proxy: - self.pool_manager = aiohttp.ClientSession( - connector=connector, - proxy=configuration.proxy - ) - else: - self.pool_manager = aiohttp.ClientSession( - connector=connector - ) + self.pool_manager = aiohttp.ClientSession( + connector=connector + ) async def close(self): await self.pool_manager.close() @@ -130,6 +127,11 @@ class RESTClientObject(object): "headers": headers } + if self.proxy: + args["proxy"] = self.proxy + if self.proxy_headers: + args["proxy_headers"] = self.proxy_headers + if query_params: args["url"] += '?' + urlencode(query_params) diff --git a/samples/client/petstore/python-asyncio/tests/test_pet_api.py b/samples/client/petstore/python-asyncio/tests/test_pet_api.py index d37b2e09130..09d48581a40 100644 --- a/samples/client/petstore/python-asyncio/tests/test_pet_api.py +++ b/samples/client/petstore/python-asyncio/tests/test_pet_api.py @@ -192,6 +192,19 @@ class TestPetApiTests(unittest.TestCase): except ApiException as e: self.assertEqual(404, e.status) + @async_test + async def test_proxy(self): + config = Configuration() + # set not-existent proxy and catch an error to verify that + # the client library (aiohttp) tried to use it. + config.proxy = 'http://localhost:8080/proxy' + async with petstore_api.ApiClient(config) as client: + pet_api = petstore_api.PetApi(client) + + with self.assertRaisesRegex(petstore_api.rest.aiohttp.client_exceptions.ClientProxyConnectionError, + 'Cannot connect to host localhost:8080'): + await pet_api.get_pet_by_id(self.pet.id) + if __name__ == '__main__': import logging