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

Refactor client response decoding #98

Merged
merged 5 commits into from
Jul 3, 2014
Merged

Refactor client response decoding #98

merged 5 commits into from
Jul 3, 2014

Conversation

kxepal
Copy link
Member

@kxepal kxepal commented Jul 3, 2014

On the rights of the idea. I'd used these changes for some time and found them very handy, would like to hear what you think about. While I expect that they could be a bit radical, current JSON handling inside .read() is really creates more problems than tends to solve.

Replacing read_and_close() with read(close=True) is a necessary evil - otherwise we'll have json()/json_and_close() etc. - that is awkward.

@asvetlov
Copy link
Member

asvetlov commented Jul 3, 2014

I like the pull request.


return payload
return json.loads(self._content.decode('utf-8'))
Copy link
Member

Choose a reason for hiding this comment

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

  1. Please try to use encoding from Content-Type charset if specified.
  2. Please support encoding keyword-only method argument for overriding json encoding.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good points!

Copy link
Member Author

Choose a reason for hiding this comment

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

Please support encoding keyword-only method argument for overriding json encoding.

I'm afraid, this isn't actual any more:
https://docs.python.org/3/library/json.html#json.loads

The other arguments have the same meaning as in load(), except encoding which is ignored and deprecated.

Copy link
Member

Choose a reason for hiding this comment

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

I mean

 return json.loads(self._content.decode(encoding)) 

not

return json.loads(self._content.decode('utf-8'), encoding)

Copy link
Member Author

Choose a reason for hiding this comment

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

encoding passes as response.json(encoding='utf-8') and overrides defined in Content-Type header, correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, exactly!

@fafhrd91
Copy link
Member

fafhrd91 commented Jul 3, 2014

I don't like close parameter. Both read() and json() methods are high level and both fully consume response payload and I think it is safe just release connection. On the other hand in close() if payload is not consumed we have to force close connection.

@kxepal
Copy link
Member Author

kxepal commented Jul 3, 2014

@fafhrd91 I'd like to agree about close parameter - it's ugly and missing it may lead to connections leak. So remove it and always release connection after payload read?

@fafhrd91
Copy link
Member

fafhrd91 commented Jul 3, 2014

Yes, let's release connection implicitly. In any case it is still possible to manually read response from content object.

@asvetlov
Copy link
Member

asvetlov commented Jul 3, 2014

Agree with implicit releasing connection.

@fafhrd91
Copy link
Member

fafhrd91 commented Jul 3, 2014

I think we also need release() coroutine In case if response doesn't matter. It can just read payload and release connection.

@kxepal
Copy link
Member Author

kxepal commented Jul 3, 2014

Hm...good idea about release() - have few cases when I have to read the response, but I really don't care about the body since all the required information is in status and headers. Will add it.

One more question: could you suggest a better place for parse_mimetype function? I put it to client.py for now, but I feel it's not the best one.

@fafhrd91
Copy link
Member

fafhrd91 commented Jul 3, 2014

helpers.py? We can move all helper functions to this module.—
Sent from Mailbox

On Thu, Jul 3, 2014 at 7:29 AM, Alexander Shorin notifications@github.com
wrote:

Hm...good idea about release() - have few cases when I have to read the response, but I really don't care about the body since all the required information is in status and headers. Will add it.

One more question: could you suggest a better place for parse_mimetype function? I put it to client.py for now, but I feel it's not the best one.

Reply to this email directly or view it on GitHub:
#98 (comment)

@kxepal
Copy link
Member Author

kxepal commented Jul 3, 2014

helpers.py? We can move all helper functions to this module.—

oops, sorry, I push changes before read your response. Anyway, PR needs in rebase and squash since I made a mistake with logging - it's not available any more.

UPDATE: helpers or utils?

Deprecate ClientResponse.read_and_close method
Useful, when response body isn't available for read (HEAD requests) or
if you're not interested in it, but wants release connection correctly.
There are several issues with current decoding implementation inside
read method:
- issue #18
- no possibility to use different json lib
- doesn't scale for different mime types
- it crushes for empty body
- on call read(True) with decoding support for multiple types you
  won't be sure which kind of response you'll get back

TL;DR there are too many reasons to patch .read() method because of
decoding logic inside. Moving it to standalone method simplifies life
a lot.
@kxepal
Copy link
Member Author

kxepal commented Jul 3, 2014

Rebased PR and squashed commits. parse_mimetype moved to helpers module, but it's alone there and needs in some good company (:

@fafhrd91
Copy link
Member

fafhrd91 commented Jul 3, 2014

very good! thanks.

fafhrd91 added a commit that referenced this pull request Jul 3, 2014
@fafhrd91 fafhrd91 merged commit 24553b1 into aio-libs:master Jul 3, 2014
@kxepal
Copy link
Member Author

kxepal commented Jul 3, 2014

Awesome! Thanks @fafhrd91 , @asvetlov for help (:

@asvetlov
Copy link
Member

asvetlov commented Jul 4, 2014

@kxepal Sure!

@lock
Copy link

lock bot commented Oct 30, 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.

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

Successfully merging this pull request may close these issues.

3 participants