adding translations and transifex third party tool
[pub/Android/ownCloud.git] / third_party / transifex-client / txclib / http_utils.py
1 # -*- coding: utf-8 -*-
2
3 """
4 HTTP-related utility functions.
5 """
6
7 from __future__ import with_statement
8 import gzip
9 try:
10 import cStringIO as StringIO
11 except ImportError:
12 import StringIO
13
14
15 def _gzip_decode(gzip_data):
16 """
17 Unzip gzipped data and return them.
18
19 :param gzip_data: Gzipped data.
20 :returns: The actual data.
21 """
22 try:
23 gzip_data = StringIO.StringIO(gzip_data)
24 gzip_file = gzip.GzipFile(fileobj=gzip_data)
25 data = gzip_file.read()
26 return data
27 finally:
28 gzip_data.close()
29
30
31 def http_response(response):
32 """
33 Return the response of a HTTP request.
34
35 If the response has been gzipped, gunzip it first.
36
37 :param response: The raw response of a HTTP request.
38 :returns: A response suitable to be used by clients.
39 """
40 metadata = response.info()
41 data = response.read()
42 response.close()
43 if metadata.get('content-encoding') == 'gzip':
44 return _gzip_decode(data)
45 else:
46 return data