Skip to content

Conversation

@RatanShreshtha
Copy link

Added proxy parameters for ClientSession so that subsequent requests can use the session proxy.

Are there changes in behavior for the user?

New parameters of ClientSession():

  • proxy
  • proxy_auth
  • proxy_headers
proxy_auth = aiohttp.BasicAuth('user', 'pass')
async with ClientSession(proxy="http://proxy.com", proxy_auth=proxy_auth) as session:
    async with session.get('http://test.com') as resp:
        print(await resp.text())

Related issue number

#2580
#2582

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

Yusuke Tsutsumi
Марк Коренберг
Семён Марьясин
Семён Марьясин No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

This change is unrelated. Fix your editor to preserve original EOF.

Copy link
Member

Choose a reason for hiding this comment

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

version = self._version

# Merge with session proxy
proxy = self._proxy_info['proxy'] \
Copy link
Member

Choose a reason for hiding this comment

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

Please use brackets instead of escaping EOL

@asvetlov
Copy link
Member

Not sure if I like yet another params in ClientSession constructor.

Anyway, the biggest showstopper now is proxy tests.
I have a strong feeling that mocking proxy calls has close to zero value.
Our functional proxy tests are messy too.
What we need to unblock any proxy improvement is implementing a test proxy server, rewriting tests to use it and dropping the current mocking approach.

@RatanShreshtha
Copy link
Author

@asvetlov I am thinking about sending a separate PR with test proxy server and new tests.

@RatanShreshtha
Copy link
Author

@webknjaz I am unable to solve the issue with mypy I have not used mypy before, can you guide me through that I have done wrong ?

@webknjaz
Copy link
Member

@RatanShreshtha

So the errors are:

aiohttp/client.py:293: error: Incompatible types in assignment (expression has type "Union[str, URL, None, BasicAuth, Mapping[str, str]]", variable has type "Union[str, URL, None]")
aiohttp/client.py:296: error: Incompatible types in assignment (expression has type "Union[str, URL, None, BasicAuth, Mapping[str, str]]", variable has type "Optional[BasicAuth]")
aiohttp/client.py:299: error: Incompatible types in assignment (expression has type "Union[str, URL, None, BasicAuth, Mapping[str, str]]", variable has type "Union[Mapping[str, str], CIMultiDict[str], CIMultiDictProxy[str], None]")

Let's look at the first one. The statement (partially) is

proxy = self._proxy_info['proxy']

So it says expression has type "Union[str, URL, None, BasicAuth, Mapping[str, str]]", which means that the expression self._proxy_info['proxy'] ... returns smth of type Union[str, URL, None, BasicAuth, Mapping[str, str]] but earlier on line 258:

proxy: Optional[StrOrURL]=sentinel,

you declared that allowed types for data to assign to the proxy variable are Optional[StrOrURL] (which unpacks to Union[str, URL, None] as stated in the error).
You need to modify type definitions or code so that there were no code assigning "forbidden"/non-matching types to vars.

@RatanShreshtha

This comment has been minimized.

@codecov-io

This comment has been minimized.

@RatanShreshtha

This comment has been minimized.

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

  1. Please reserve merge conflicts
  2. Docs update is needed.
  3. @webknjaz asked for changes, not all comments are addressed as I see.

proxy_test_server implementation suggestion:
In my mind the server fixture proxies requests to the app.
Why not pass a app server to the fixture to check the proxy? It can get rid of mocks in tests.

connector_owner=True, \
auto_decompress=True, proxies=None)
auto_decompress=True, \
proxy=None, proxy_auth=None, proxy_headers=None))
Copy link
Member

Choose a reason for hiding this comment

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

Please document these params, not only enumerate in constructor signature.

Copy link
Author

Choose a reason for hiding this comment

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

I have documented these changes.

if proxy is None and self._session_proxy is not None:
try:
proxy = self._session_proxy
proxy_auth = self._session_proxy_auth
Copy link
Member

Choose a reason for hiding this comment

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

By this line you are overriding explicitly passed proxy_auth and proxy_headers arguments with session default ones.

I have no idea how to resolve the conflict properly but proposed behavior looks confusing.

Copy link
Author

Choose a reason for hiding this comment

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

I am trying to use session proxy if proxy is not mentioned explicitly for a particular request.

@webknjaz

This comment has been minimized.

@webknjaz

This comment has been minimized.

@RatanShreshtha

This comment has been minimized.

@CLAassistant

This comment has been minimized.

@webknjaz webknjaz closed this Jul 29, 2019
@webknjaz webknjaz reopened this Jul 29, 2019
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jul 29, 2019
@webknjaz webknjaz closed this Jul 29, 2019
@webknjaz webknjaz reopened this Jul 29, 2019
@webknjaz
Copy link
Member

Hey @RatanShreshtha, this PR needs updating

@RatanShreshtha
Copy link
Author

@webknjaz let me update the PR

Eugene Naydenov
Eugene Tolmachev
Evert Lammerts
Fan Yu
Copy link
Member

Choose a reason for hiding this comment

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

Plz clean-up all unrelated changes.

Copy link
Author

Choose a reason for hiding this comment

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

Should I squash all the changes ?

Copy link
Member

Choose a reason for hiding this comment

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

Up to you. We'll use "Merge and squash" strategy on this PR anyway :)

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Looks good, please address my comment

skip_headers.add(istr(i))

if proxy is not None:
if proxy is not None and self._session_proxy is None:
Copy link
Member

Choose a reason for hiding this comment

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

What is the behavior if proxy is not None and self._session_proxy is not None?
I think passed proxy parameter should take precedence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants