7
7
8
8
# store the URL and the access/refresh tokens as state
9
9
import requests
10
+ import time
10
11
from .about import get_client_user_agent
11
12
12
13
class AuthError (Exception ):
@@ -118,6 +119,10 @@ def _do_call(self, uri, method, params=None, body=None, retry=True, files=None,
118
119
if response .status_code == requests .codes .NO_CONTENT :
119
120
return {}
120
121
122
+ if response .status_code == requests .codes .TOO_MANY :
123
+ time .sleep (1 )
124
+ return self ._do_call (uri , method , params , body , False )
125
+
121
126
# some endpoints have no content-type header
122
127
if 'content-type' not in response .headers :
123
128
response .headers ['content-type' ] = 'none'
@@ -130,14 +135,19 @@ def _do_call(self, uri, method, params=None, body=None, retry=True, files=None,
130
135
if response .headers .get ('content-type' ) == 'application/zip' :
131
136
return response .content
132
137
133
- json_body = response .json ()
134
- # if this is a background task, add the task id (or location) to the body
135
- if response .status_code == requests .codes .ACCEPTED :
136
- json_body ['task_id' ] = response .headers .get ('x-task-id' )
138
+ json_body = {}
139
+ try :
140
+ json_body = response .json ()
141
+
142
+ # if this is a background task, add the task id (or location) to the body
143
+ if response .status_code == requests .codes .ACCEPTED :
137
144
if 'x-task-id' in response .headers :
138
- json_body [ ' task_id' ] = response .headers ['x-task-id' ]
145
+ json_body . update ({ " task_id" : response .headers ['x-task-id' ]})
139
146
if 'location' in response .headers :
140
- json_body ['location' ] = response .headers ['location' ]
147
+ json_body .update ({"location" : response .headers ['location' ]})
148
+
149
+ except requests .exceptions .JSONDecodeError :
150
+ json_body = {}
141
151
142
152
if isinstance (json_body , dict ) and retry and json_body .get ('errorCode' ) == 60001 :
143
153
self ._refresh ()
0 commit comments