def _handlePlanStatusRequests(self, type, endpoint, isPostRequest=False, data=None): # Checks if it is a post request if isPostRequest: response = requests.post(endpoint, headers=self.headers, data=json.dumps(data)) else: response = requests.get(endpoint, headers=self.headers) # Checks if it can be parsed to json try: responseJson = response.json() except: raise ServerError({"error": True, "errMsg": response.text}) # Checks if it returns a 2xx code if response.ok: return {"error": False, "returnedData": responseJson} else: raise PlanStatusError(type, { "error": True, "returnedData": str(responseJson) })
def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise, name): # Check if we can obtain a json try: responseJson = response.json() except: raise ServerError({ "error": True, "name": name, "errMsg": response }) # Check if the response contains data parameter if not responseJson.get("data", None): raise TypeOfErrorToRaise({ "error": True, "name": name, "errMsg": responseJson.get("message", "Server is down") }) # Check if it is returning a 200 if not response.ok: errMsg = responseJson["data"].get("message", None) raise TypeOfErrorToRaise({"error": True, "errMsg": errMsg}) return responseJson
def _handleTransferStatusRequests(self, endpoint, isPostRequest=False, data=None): # Request headers headers = { 'content-type': 'application/json', } # Checks if it is a post request if isPostRequest: response = requests.post(endpoint, headers=headers, data=json.dumps(data)) else: response = requests.get(endpoint, headers=headers) # Checks if it can be parsed to json try: responseJson = response.json() except: raise ServerError({"error": True, "errMsg": response.text}) # Checks if it returns a 2xx code if response.ok: return {"error": False, "returnedData": responseJson} else: raise TransferFetchError({ "error": True, "returnedData": responseJson })
def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise, txRef=None, flwRef=None): preliminary_error_response = copy.deepcopy(response_object) preliminary_error_response = Payment.deleteUnnecessaryKeys( preliminary_error_response, "transactionComplete", "chargecode", "vbvmessage", "vbvcode", "acctmessage", "currency") # Check if we can obtain a json try: # print(response) responseJson = response.json() except: raise ServerError({ "error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": response }) # Check if the response contains data parameter if responseJson.get("data", None): if txRef: flwRef = responseJson["data"].get("flwRef", None) if flwRef: txRef = responseJson["data"].get("txRef", None) else: raise TypeOfErrorToRaise({ "error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": responseJson.get("message", "Server is down") }) # Check if it is returning a 200 if not response.ok: errMsg = responseJson["data"].get("message", None) raise TypeOfErrorToRaise({ "error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": errMsg }) return {"json": responseJson, "flwRef": flwRef, "txRef": txRef}
def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise, txRef=None, flwRef=None): # Check if we can obtain a json try: responseJson = response.json() except: raise ServerError({"error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": response}) # Check if the response contains data parameter if responseJson.get("data", None): if txRef: flwRef = responseJson["data"].get("flwRef", None) if flwRef: txRef = responseJson["data"].get("txRef", None) else: raise TypeOfErrorToRaise({"error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": responseJson.get("message", "Server is down")}) # Check if it is returning a 200 if not response.ok: errMsg = responseJson["data"].get("message", None) raise TypeOfErrorToRaise({"error": True, "txRef": txRef, "flwRef": flwRef, "errMsg": errMsg}) return {"json": responseJson, "flwRef": flwRef, "txRef": txRef}