~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/apiclient/maas_client.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20130304114944-azcvu9anlf8mizpa
Tags: upstream-1.3+bzr1452+dfsg
ImportĀ upstreamĀ versionĀ 1.3+bzr1452+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    'MAASOAuth',
17
17
    ]
18
18
 
 
19
import gzip
 
20
from io import BytesIO
19
21
import urllib2
20
22
 
21
23
from apiclient.encode_json import encode_json_data
82
84
 
83
85
        :return: A open file-like object that contains the response.
84
86
        """
 
87
        headers = dict(headers)
 
88
        # header keys are case insensitive, so we have to pass over them
 
89
        set_accept_encoding = False
 
90
        for key in headers:
 
91
            if key.lower() == 'accept-encoding':
 
92
                # The user already supplied a requested encoding, so just pass
 
93
                # it along.
 
94
                break
 
95
        else:
 
96
            set_accept_encoding = True
 
97
            headers['Accept-encoding'] = 'gzip'
85
98
        req = urllib2.Request(request_url, data, headers)
86
 
        return urllib2.urlopen(req)
 
99
        res = urllib2.urlopen(req)
 
100
        # If we set the Accept-encoding header, then we decode the header for
 
101
        # the caller.
 
102
        is_gzip = (
 
103
            set_accept_encoding
 
104
            and res.info().get('Content-Encoding') == 'gzip')
 
105
        if is_gzip:
 
106
            # Workaround python's gzip failure, gzip.GzipFile wants to be able
 
107
            # to seek the file object.
 
108
            res_content_io = BytesIO(res.read())
 
109
            ungz = gzip.GzipFile(mode='rb', fileobj=res_content_io)
 
110
            res = urllib2.addinfourl(ungz, res.headers, res.url, res.code)
 
111
        return res
87
112
 
88
113
 
89
114
class MAASClient: