Opened 12 years ago

Closed 10 years ago

#18523 closed New feature (fixed)

Add getvalue to HttpResponse

Reported by: Claude Paroz Owned by: Michael Kelly
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The HttpResponse object can almost be used as another stream object. Unfortunately, it doesn't offer the getvalue() method to be able to read its content, hence requiring conditional code in some circonstances (see #15197). What about adding it (HttpResponse.getvalue() === HttpResponse.content)?

Change History (11)

comment:1 by Aymeric Augustin, 12 years ago

Triage Stage: UnreviewedAccepted

Good idea.

Do Python's docs provide a list of the methods that a "stream object" must support? While we're there, we should also check that HttpRequest objects support all the methods that make sense.

comment:2 by Claude Paroz, 12 years ago

Version 0, edited 12 years ago by Claude Paroz (next)

comment:3 by Unai Zalakain, 11 years ago

I would add the following attributes and methods:

  • HttpResponseBase.closed: set to False in __init__, set to True in close()
  • HttpResponseBase.writable(): returns False because write(content) raises an exception (should be changed to IOError BTW)
  • HttpResponseBase.writelines(lines): raises IOError
  • HttpResponseBase.tell(): change it to raise IOError
  • HttpResponse.getvalue(): returns self.content
  • HttpResponse.writelines(lines): writes each line with self.write(line)

comment:4 by Michael Kelly, 11 years ago

Has patch: set
Owner: changed from nobody to Michael Kelly
Status: newassigned

I submitted a PR with the changes mentioned in comment:3 here: https://github.com/django/django/pull/2545 . If we think that all of the stuff from io.IOBase should be included, I can update my PR with the rest.

Last edited 11 years ago by Michael Kelly (previous) (diff)

comment:5 by Tim Graham, 10 years ago

Patch needs improvement: set

There are some comments for improvement on the PR. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:6 by Michael Kelly, 10 years ago

Will do, just having trouble finding a good time to work on this lately. :D

comment:7 by Michael Kelly, 10 years ago

Patch needs improvement: unset

Updated branch with changes in response to feedback.

Last edited 10 years ago by Michael Kelly (previous) (diff)

comment:8 by Tim Graham, 10 years ago

Patch needs improvement: set

There are some problems with the tests on Python 3.

comment:9 by Tim Graham, 10 years ago

Patch needs improvement: unset

comment:10 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:11 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In ebc8e79cf3bdd42a99e91d6e679248d07097d3db:

Fixed #18523 -- Added stream-like API to HttpResponse.

Added getvalue() to HttpResponse to return the content of the response,
along with a few other methods to partially match io.IOBase.

Thanks Claude Paroz for the suggestion and Nick Sanford for review.

Note: See TracTickets for help on using tickets.
Back to Top