Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _should_retry_response(resp_status, content):

Args:
resp_status: The response status received.
content: The response content body.
content: The response content body.

Returns:
True if the response should be retried, otherwise False.
Expand All @@ -112,7 +112,10 @@ def _should_retry_response(resp_status, content):
# Content is in JSON format.
try:
data = json.loads(content.decode('utf-8'))
reason = data['error']['errors'][0]['reason']
if isinstance(data, dict):
reason = data['error']['errors'][0]['reason']
else:
reason = data[0]['error']['errors']['reason']
except (UnicodeDecodeError, ValueError, KeyError):
LOGGER.warning('Invalid JSON content from response: %s', content)
return False
Expand Down Expand Up @@ -510,7 +513,6 @@ class MediaFileUpload(MediaIoBaseUpload):
Construct a MediaFileUpload and pass as the media_body parameter of the
method. For example, if we had a service that allowed uploading images:


media = MediaFileUpload('cow.png', mimetype='image/png',
chunksize=1024*1024, resumable=True)
farm.animals().insert(
Expand Down Expand Up @@ -654,7 +656,7 @@ def next_chunk(self, num_retries=0):
request only once.

Returns:
(status, done): (MediaDownloadStatus, boolean)
(status, done): (MediaDownloadProgress, boolean)
The value of 'done' will be True when the media has been fully
downloaded.

Expand Down
38 changes: 37 additions & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def request(self, *args, **kwargs):
ex = TimeoutError()
else:
ex = socket.error()

if self.num_errors == 2:
#first try a broken pipe error (#218)
ex.errno = socket.errno.EPIPE
Expand Down Expand Up @@ -739,6 +738,20 @@ def test_media_io_base_download_empty_file(self):
}
}"""

LIST_NOT_CONFIGURED_RESPONSE = """[
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "accessNotConfigured",
"message": "Access Not Configured"
}
],
"code": 403,
"message": "Access Not Configured"
}
]"""

class Callbacks(object):
def __init__(self):
self.responses = {}
Expand Down Expand Up @@ -956,6 +969,29 @@ def test_no_retry_401_fails_fast(self):
request.execute()
request._sleep.assert_not_called()

def test_no_retry_403_list_fails(self):
http = HttpMockSequence([
({'status': '403'}, LIST_NOT_CONFIGURED_RESPONSE),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})

request._rand = lambda: 1.0
request._sleep = mock.MagicMock()

with self.assertRaises(HttpError):
request.execute()
request._sleep.assert_not_called()

class TestBatch(unittest.TestCase):

def setUp(self):
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ deps =
mox
pyopenssl
pycrypto==2.6
django
django<2.0.0; python_version < '3.0.0'
django>=2.0.0; python_version > '3.0.0'
webtest
nose
coverage>=3.6,<3.99
Expand Down