Skip to content

Commit

Permalink
Use gzip when talking to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Sep 29, 2014
1 parent f796d67 commit 972b5c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Deprecated the `setFormat` method in favor of directly setting the `format` attribute.
* Deprecated the `setVersion` method in favor of directly setting the `version` attribute.
* Deprecated the `setTimeout` method in favor of directly setting the `timeout` attribute.
* Request gzipped responses from API.

0.4.1

Expand Down
8 changes: 7 additions & 1 deletion disqusapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__version__ = 'unknown'

import re
import zlib
import os.path
import warnings
import socket
Expand Down Expand Up @@ -174,7 +175,8 @@ def _request(self, endpoint=None, **kwargs):
params.append((k, v))

headers = {
'User-Agent': 'disqus-python/%s' % __version__
'User-Agent': 'disqus-python/%s' % __version__,
'Accept-Encoding': 'gzip',
}

if method == 'GET':
Expand All @@ -194,6 +196,10 @@ def _request(self, endpoint=None, **kwargs):
# Close connection
conn.close()

if response.getheader('Content-Encoding') == 'gzip':
# See: http://stackoverflow.com/a/2424549
body = zlib.decompress(body, 16 + zlib.MAX_WBITS)

# Determine the encoding of the response and respect
# the Content-Type header, but default back to utf-8
content_type = response.getheader('Content-Type')
Expand Down

0 comments on commit 972b5c9

Please sign in to comment.