def check_errors(self, response=None, UserErr=None): """Raise specific errors depending on how the API call failed""" status = response.status_code code = None try: code = response.json().get("code") except: pass if status == 429 and code == "rate_err": raise error.RateLimitError(response) elif status == 402 and code == "payment_err": raise error.PaymentError(response) elif status / 100 == 4: if code == "token_expiration_err": raise error.TokenExpirationError(response) elif UserErr: raise UserErr(response) else: raise error.APIError(response) elif code == "media_err": raise error.MediaError(response) elif status / 100 == 5 and code == "server_err": raise error.ServerError(response) elif status / 100 != 2: raise error.APIError(response)
def save_token(self, response): """Extracts the access token from the API response""" res = response.json() if isinstance(res, str): print(response.text) raise error.APIError(message="Could not parse the response") access_token = res["access_token"] token_type = res["token_type"] expires_in = res["expires_in"] if not access_token or not token_type or not expires_in: raise error.APIError( message="Required parameters not found in the response") self.token = self.Token(token_type, access_token, expires_in)