2015-03-08 15:01:09 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
"""Utility functions grab bag."""
|
|
|
|
|
2015-04-27 14:07:17 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import httplib2
|
|
|
|
|
|
|
|
|
2015-07-14 11:01:36 +01:00
|
|
|
try: # Python 3
|
2015-07-14 11:30:12 +02:00
|
|
|
ConnectionRefused = ConnectionRefusedError
|
2015-07-14 11:01:36 +01:00
|
|
|
except NameError: # Python 2
|
2015-07-14 11:30:12 +02:00
|
|
|
import socket
|
|
|
|
ConnectionRefused = socket.error
|
|
|
|
|
|
|
|
|
2015-04-27 14:07:17 +01:00
|
|
|
class VerboseHttp(httplib2.Http):
|
|
|
|
"""A subclass of Http that verbosely reports on activity."""
|
|
|
|
|
|
|
|
def _request(self, conn, host, absolute_uri, request_uri, method, body,
|
|
|
|
headers, redirections, cachekey):
|
|
|
|
"""Display request parameters before requesting."""
|
|
|
|
|
|
|
|
print('\n%s %s\nHost: %s' % (method, request_uri, host))
|
|
|
|
for key in headers:
|
|
|
|
print('%s: %s' % (key, headers[key]))
|
|
|
|
|
|
|
|
(response, content) = httplib2.Http._request(
|
|
|
|
self, conn, host, absolute_uri, request_uri, method, body,
|
|
|
|
headers, redirections, cachekey
|
|
|
|
)
|
|
|
|
|
|
|
|
print()
|
|
|
|
for key in response.dict:
|
|
|
|
print('%s: %s' % (key, response.dict[key]))
|
|
|
|
|
|
|
|
return (response, content)
|
|
|
|
|
2015-03-08 15:01:09 +00:00
|
|
|
|
|
|
|
def decode_content(response, content):
|
|
|
|
"""Decode content to a proper string."""
|
|
|
|
content_type = response.get('content-type',
|
2015-07-22 20:01:03 +01:00
|
|
|
'application/binary').strip().lower()
|
|
|
|
charset = 'utf-8'
|
2015-03-08 15:01:09 +00:00
|
|
|
if ';' in content_type:
|
2015-07-22 20:11:00 +01:00
|
|
|
content_type, parameter_strings = (attr.strip() for attr
|
|
|
|
in content_type.split(';', 1))
|
2015-07-22 20:01:03 +01:00
|
|
|
try:
|
|
|
|
parameter_pairs = [atom.strip().split('=')
|
|
|
|
for atom in parameter_strings.split(';')]
|
|
|
|
parameters = {name: value for name, value in parameter_pairs}
|
|
|
|
charset = parameters['charset']
|
|
|
|
except (ValueError, KeyError):
|
|
|
|
# KeyError when no charset found.
|
|
|
|
# ValueError when the parameter_strings are poorly
|
|
|
|
# formed (for example trailing ;)
|
|
|
|
pass
|
2015-03-08 15:01:09 +00:00
|
|
|
|
|
|
|
if not_binary(content_type):
|
|
|
|
return content.decode(charset)
|
|
|
|
else:
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2015-04-27 14:07:17 +01:00
|
|
|
def get_http(verbose=False):
|
|
|
|
"""Return an Http class for making requests."""
|
|
|
|
if verbose:
|
|
|
|
return VerboseHttp()
|
|
|
|
return httplib2.Http()
|
|
|
|
|
|
|
|
|
2015-03-08 15:01:09 +00:00
|
|
|
def not_binary(content_type):
|
|
|
|
"""Decide if something is content we'd like to treat as a string."""
|
|
|
|
return (content_type.startswith('text/') or
|
|
|
|
content_type.endswith('+xml') or
|
|
|
|
content_type.endswith('+json') or
|
|
|
|
content_type == 'application/javascript' or
|
2015-08-04 18:34:10 +01:00
|
|
|
content_type.startswith('application/json'))
|