Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, response_body, original_exception):
super(ParseResponseError, self).__init__(
"Slack API response body could not be parsed: {0}. Original exception: {1}".format(
response_body, original_exception
)
)
self.response_body = response_body
self.original_exception = original_exception
# Check for missing or expired access token before submitting the request
if method != "oauth.access" and self.refresh_token:
current_ts = int(time.time())
token_is_expired = current_ts > self.access_token_expires_at
if token_is_expired or self.token is None:
self.refresh_access_token()
response_body = self.server.api_call(
self.token, request=method, timeout=timeout, **kwargs
)
# Attempt to parse the response as JSON
try:
result = json.loads(response_body)
except ValueError as json_decode_error:
raise ParseResponseError(response_body, json_decode_error)
response_json = json.loads(response_body)
if result.get("ok", False):
if method == "im.open":
self.server.attach_channel(kwargs["user"], result["channel"]["id"])
elif method in ("mpim.open", "groups.create", "groups.createchild"):
self.server.parse_channel_data([result["group"]])
elif method in ("channels.create", "channels.join"):
self.server.parse_channel_data([result["channel"]])
else:
# if the API request returns an invalid_auth error, refresh the token and try again
if (
self.refresh_token
and "error" in response_json
and response_json["error"] == "invalid_auth"
):