fix: 458 handle error format (#459)
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index 7992ea3..eb46449 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -89,7 +89,7 @@
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.
@@ -112,7 +112,10 @@
# 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
@@ -510,7 +513,6 @@
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(
diff --git a/tests/test_http.py b/tests/test_http.py
index 0df00ab..04e1142 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -128,7 +128,6 @@
ex = TimeoutError()
else:
ex = socket.error()
-
if self.num_errors == 2:
#first try a broken pipe error (#218)
ex.errno = socket.errno.EPIPE
@@ -739,6 +738,20 @@
}
}"""
+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 = {}
@@ -956,6 +969,29 @@
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):