Skip to content

Commit

Permalink
Do not send gzip header for streaming calls
Browse files Browse the repository at this point in the history
Twitter is moving all of its APIs towards HTTP1.1.
https://dev.twitter.com/blog/deprecating-http-1.0-streaming-api
Userstream passed already mid-november and has been borken for this lib since
On Jan 6, Twitter will apply it as weel for all the other stream methods,
meaning they probably would be all broken.

The problem was in two pieces.
First with this commit, we stop asking for gzip encoded data for streaming
calls: for the HTPP1.0 calls, this was not taken into account anyway, but with
HTTP1.1 the answer is now chunked and should therefore not caught zipped.

(First part of fix for userstream and soon all streams cf python-twitter-tools#190 python-twitter-tools#116)
  • Loading branch information
RouxRC authored and Dennis Kanygin committed Apr 18, 2014
1 parent f5ed8dd commit c732d49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
27 changes: 8 additions & 19 deletions twitter/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# encoding: utf-8
from __future__ import unicode_literals

try:
import urllib.request as urllib_request
import urllib.error as urllib_error
Expand Down Expand Up @@ -132,8 +129,8 @@ def wrap_response(response, headers):
class TwitterCall(object):

def __init__(
self, auth, format, domain, callable_cls, uri="",
uriparts=None, secure=True, timeout=None, gzip=False):
self, auth, format, domain, callable_cls, uri="",
uriparts=None, secure=True, timeout=None, gzip=False):
self.auth = auth
self.format = format
self.domain = domain
Expand All @@ -151,9 +148,9 @@ def __getattr__(self, k):
def extend_call(arg):
return self.callable_cls(
auth=self.auth, format=self.format, domain=self.domain,
callable_cls=self.callable_cls, timeout=self.timeout,
secure=self.secure, gzip=self.gzip,
uriparts=self.uriparts + (arg,))
callable_cls=self.callable_cls, timeout=self.timeout, uriparts=self.uriparts \
+ (arg,),
secure=self.secure, gzip=self.gzip)
if k == "_":
return extend_call
else:
Expand Down Expand Up @@ -200,17 +197,9 @@ def __call__(self, **kwargs):
uriBase = "http%s://%s/%s%s%s" % (
secure_str, self.domain, uri, dot, self.format)

# Catch media arguments to handle oauth query differently for multipart
media = None
for arg in ['media[]', 'banner', 'image']:
if arg in kwargs:
media = kwargs.pop(arg)
mediafield = arg
break

headers = {'Accept-Encoding': 'gzip'} if self.gzip else dict()
body = None
arg_data = None
headers = {}
if self.gzip:
headers['Accept-Encoding'] = 'gzip'
if self.auth:
headers.update(self.auth.generate_headers())
# Use urlencoded oauth args with no params when sending media
Expand Down
2 changes: 1 addition & 1 deletion twitter/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,5 @@ def _handle_response(self, req, uri, arg_data, _timeout=None):

TwitterCall.__init__(
self, auth=auth, format="json", domain=domain,
callable_cls=TwitterStreamCall,
callable_cls=call_cls,
secure=secure, uriparts=uriparts, timeout=timeout, gzip=False)

0 comments on commit c732d49

Please sign in to comment.