Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python/asyncio] fix passing proxy parameters to aiohttp #5943

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was added 3 years ago so it looks like the signature has changed in that time.

else:
self.pool_manager = aiohttp.ClientSession(
connector=connector
)
self.pool_manager = aiohttp.ClientSession(
connector=connector
)

async def close(self):
await self.pool_manager.close()
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 11 additions & 9 deletions samples/client/petstore/python-asyncio/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
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()
Expand Down Expand Up @@ -130,6 +127,11 @@ async def request(self, method, url, query_params=None, headers=None,
"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)

Expand Down
13 changes: 13 additions & 0 deletions samples/client/petstore/python-asyncio/tests/test_pet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ async def test_delete_pet(self):
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
Expand Down