Permalink
Browse files

Merge branch 'develop' of https://github.com/atdt/requests into develop

Conflicts:
	AUTHORS.rst
  • Loading branch information...
2 parents 75dc012 + f214b91 commit 9d88ac7b50f05b1196672ba7df88d2321c2d6ee0 @kennethreitz kennethreitz committed Dec 22, 2011
Showing with 45 additions and 1 deletion.
  1. +1 −0 AUTHORS.rst
  2. +21 −0 docs/user/advanced.rst
  3. +6 −1 requests/models.py
  4. +17 −0 test_requests.py
View
@@ -67,3 +67,4 @@ Patches and Suggestions
- Chris Van Horne
- Christopher Davis
- Ori Livneh
+- Jason Emerick
View
@@ -243,6 +243,27 @@ Then, we can make a request using our Pizza Auth::
<Response [200]>
+Streaming Requests
+------------------
+
+With ``requests.Response.iter_lines()`` you can easily iterate over streaming
+APIs such as the `Twitter Streaming API <https://dev.twitter.com/docs/streaming-api>`_.
+
+To use the Twitter Streaming API to track the keyword "requests":
+
+::
+
+ import requests
+ import json
+
+ r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
+ data={'track': 'requests'}, auth=('username', 'password'))
+
+ for line in r.iter_lines():
+ if line: # filter out keep-alive new lines
+ print json.loads(line)
+
+
Verbose Logging
---------------
View
@@ -597,7 +597,7 @@ def iter_lines(self, newlines=None, decode_unicode=None):
def generate():
chunk = []
- while 1:
+ while True:
c = self.raw.read(1)
if not c:
break
@@ -608,6 +608,11 @@ def generate():
else:
chunk.append(c)
+ # Yield the remainder, in case the response
+ # did not terminate with a newline
+ if chunk:
+ yield ''.join(chunk)
+
self._content_consumed = True
gen = generate()
View
@@ -3,6 +3,7 @@
from __future__ import with_statement
+import StringIO
import time
import os
import unittest
@@ -603,5 +604,21 @@ def test_iter_lines(self):
self.assertEqual(i, len_lines)
+ # Test 'dangling' fragment in responses that do not terminate in
+ # a newline.
+ quote = (
+ '''Why will he not upon our fair request\n'''
+ '''Untent his person and share the air with us?'''
+ )
+
+ # Make a request and monkey-patch its contents
+ r = requests.get(httpbin('get'))
+ r.raw = StringIO.StringIO(quote)
+
+ # Make sure iter_lines doesn't chop the trailing bit
+ lines = '\n'.join(r.iter_lines())
+ self.assertEqual(lines, quote)
+
+
if __name__ == '__main__':
unittest.main()

0 comments on commit 9d88ac7

Please sign in to comment.