Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
m = str(r.content)
error = None
try:
error = r.json()['error']
msg = error['message']
except json.JSONDecodeError:
msg = str(r.content)
if r.status_code == 404:
raise FileNotFoundError(path)
elif r.status_code == 403:
raise IOError("Forbidden: %s\n%s" % (path, msg))
elif "invalid" in m:
raise ValueError("Bad Request: %s\n%s" % (path, msg))
elif error:
raise HtmlError(error)
else:
raise RuntimeError(m)
def _call(self, method, path, *args, **kwargs):
for k, v in list(kwargs.items()):
# only pass parameters that have values
if v is None:
del kwargs[k]
json = kwargs.pop('json', None)
meth = getattr(self.session, method)
if args:
path = path.format(*[quote_plus(p) for p in args])
for retry in range(self.retries):
try:
time.sleep(2**retry - 1)
r = meth(self.base + path, params=kwargs, json=json)
validate_response(r, path)
break
except (HtmlError, RequestException, GoogleAuthError) as e:
if retry == self.retries - 1:
logger.exception("_call out of retries on exception: %s", e)
raise e
if is_retriable(e):
logger.debug("_call retrying after exception: %s", e)
continue
logger.exception("_call non-retriable exception: %s", e)
raise e
try:
out = r.json()
except ValueError:
out = r.content
return out