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

Some option to call raise_for_status() automatically on any request. #1724

Closed
socketpair opened this issue Mar 17, 2017 · 6 comments
Closed
Labels

Comments

@socketpair
Copy link
Contributor

socketpair commented Mar 17, 2017

I have many places where my application issues HTTP-request, for example:

async def get_network_interfaces():
    async with ClientSession() as session:
        async with session.get('http://localhost/network/interfaces') as response:
            response.raise_for_status()  # <- THIS LINE
            return await response.json()

The marked line annoys me, since our programmes forgot to add it too frequently. Unforunatelly, our web services report error with HTTP code 542 and JSON-response of error, so returning from such functions without exception is a bug.

fafhrd91 added a commit that referenced this issue Mar 17, 2017
@fafhrd91
Copy link
Member

added to master.

PS you should re-use session object

@socketpair
Copy link
Contributor Author

WOW!

  1. What did you mean when you say about ClientSession resuse ? As I understand, you suggest to make it global in my application. Where I can read about correct usage pattern ?
  2. You have added raise_for_status=False. This is good, but it does not solve my problem. Programmers will forget to set it anyway. Is any way to set it to True globally ?

@fafhrd91
Copy link
Member

just use one global ClientSession instance, then you can configure your session once.
http://aiohttp.readthedocs.io/en/latest/client_reference.html#client-session

@fafhrd91 fafhrd91 reopened this Mar 17, 2017
@fafhrd91
Copy link
Member

global session does connection pooling and cookie sharing if you need cookie

@socketpair
Copy link
Contributor Author

Keep-Alive is more important for me than cookies. So, I already know about such ClientSession usage. Thank you for the advice.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants