From 32337fd036df907a21e9f5b1758abeb58649cb4e Mon Sep 17 00:00:00 2001 From: drevicko Date: Fri, 8 Nov 2013 13:03:23 +1100 Subject: [PATCH 1/4] changed cursor() from recursion to while loop modified: api.py --- twython/api.py | 52 ++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/twython/api.py b/twython/api.py index 39b8f28..2d5d065 100644 --- a/twython/api.py +++ b/twython/api.py @@ -408,38 +408,36 @@ class Twython(EndpointsMixin, object): if not hasattr(function, 'iter_mode'): raise TwythonError('Unable to create generator for Twython method "%s"' % function.__name__) - content = function(**params) + while True: + content = function(**params) - if not content: - raise StopIteration + if not content: + raise StopIteration - if hasattr(function, 'iter_key'): - results = content.get(function.iter_key) - else: - results = content + if hasattr(function, 'iter_key'): + results = content.get(function.iter_key) + else: + results = content - for result in results: - yield result + for result in results: + yield result - if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': - raise StopIteration + if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': + raise StopIteration - try: - if function.iter_mode == 'id': - if not 'max_id' in params: - # Add 1 to the id because since_id and max_id are inclusive - if hasattr(function, 'iter_metadata'): - since_id = content[function.iter_metadata].get('since_id_str') - else: - since_id = content[0]['id_str'] - params['since_id'] = (int(since_id) - 1) - elif function.iter_mode == 'cursor': - params['cursor'] = content['next_cursor_str'] - except (TypeError, ValueError): # pragma: no cover - raise TwythonError('Unable to generate next page of search results, `page` is not a number.') - - for result in self.cursor(function, **params): - yield result + try: + if function.iter_mode == 'id': + if not 'max_id' in params: + # Add 1 to the id because since_id and max_id are inclusive + if hasattr(function, 'iter_metadata'): + since_id = content[function.iter_metadata].get('since_id_str') + else: + since_id = content[0]['id_str'] + params['since_id'] = (int(since_id) - 1) + elif function.iter_mode == 'cursor': + params['cursor'] = content['next_cursor_str'] + except (TypeError, ValueError): # pragma: no cover + raise TwythonError('Unable to generate next page of search results, `page` is not a number.') @staticmethod def unicode2utf8(text): From 673336ff8f8a9a647fc8e44cb05f6c8a0ec39c98 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 23 Feb 2014 14:57:03 +0000 Subject: [PATCH 2/4] Failing test for Accept-Encoding header --- tests/test_core.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index df20660..4870688 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -277,6 +277,17 @@ class TwythonAPITestCase(unittest.TestCase): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') + @responses.activate + def test_sends_correct_accept_encoding_header(self): + """Test that Twython accepts compressed data.""" + endpoint = 'statuses/home_timeline' + url = self.get_url(endpoint) + self.register_response(responses.GET, url) + + self.api.get(endpoint) + + self.assertEqual('gzip, deflate, compress', responses.calls[0].request.headers['Accept-Encoding']) + # Static methods def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" From 31ff35349cc6a79bd4385cce26b76578ac41239f Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 23 Feb 2014 15:06:44 +0000 Subject: [PATCH 3/4] Merge headers, don't overwrite. --- twython/api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/twython/api.py b/twython/api.py index db79381..88b6735 100644 --- a/twython/api.py +++ b/twython/api.py @@ -109,10 +109,14 @@ class Twython(EndpointsMixin, object): # Never be used again. client_args_copy = self.client_args.copy() for k, v in client_args_copy.items(): - if k in ('cert', 'headers', 'hooks', 'max_redirects', 'proxies'): + if k in ('cert', 'hooks', 'max_redirects', 'proxies'): setattr(self.client, k, v) self.client_args.pop(k) # Pop, pop! + # Headers are always present, so we unconditionally pop them and merge + # them into the session headers. + self.client.headers.update(self.client_args.pop('headers')) + self._last_call = None def __repr__(self): From f514816c6f9905da9e76d598cdbeb8f32a8ca63b Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sun, 23 Feb 2014 15:12:44 +0000 Subject: [PATCH 4/4] Fixup python3 test failure. --- tests/test_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_core.py b/tests/test_core.py index 4870688..5b8a374 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -286,7 +286,7 @@ class TwythonAPITestCase(unittest.TestCase): self.api.get(endpoint) - self.assertEqual('gzip, deflate, compress', responses.calls[0].request.headers['Accept-Encoding']) + self.assertEqual(b'gzip, deflate, compress', responses.calls[0].request.headers['Accept-Encoding']) # Static methods def test_construct_api_url(self):