def _generate_st_error(self, e, request_reference): response = securetrading.Response() response["requestreference"] = request_reference response_data = {} response_data.update({"errorcode": "{0}".format(e.code), "errordata": e.data, "requesttypedescription": "ERROR", "requestreference": request_reference, }) response.setdefault("responses", [response_data]) return response
def _decode(self, response, response_headers, request_reference): try: result = securetrading.util.json.loads(response) except Exception as e: raise securetrading.SendReceiveError("5", data=e) debug = "{0} Begin decoding".format(request_reference) securetrading.util.logger.debug(debug) response_object = securetrading.Response() for k in ["requestreference", "version"]: response_object.update({k: result[k]}) response_object["responses"] = result["response"] if self.config.http_response_headers and response_headers: response_object["headers"] = response_headers response_data = None debug = "{0} Finished decoding".format(request_reference) securetrading.util.logger.debug(debug) return response_object
def test__generateError(self): tests = [ (KeyError("A Key Error Occured"), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Ahc6uwqq6", "errorcode": "9", "errordata": ["A Key Error Occured"] }] }), (AssertionError("An Assertion Error Occured"), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "9", "errordata": ["An Assertion Error Occured"] }] }), (ValueError("A Value Error Occured"), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "9", "errordata": ["A Value Error Occured"] }] }), ] for exception, request_reference, exp_update in tests: config = self.get_config() api = securetrading.Api(config) actual = api._generate_error(exception, request_reference) exp_response = securetrading.Response() exp_response.update(exp_update) self.assertEqual(actual, exp_response)
def test__generateStError(self): sterror = securetrading.SecureTradingError apierror = securetrading.ApiError connecterror = securetrading.ConnectionError sendrecverror = securetrading.SendReceiveError tests = [ ({}, sterror("9", data=["Different request reference"]), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requestreference": "Ahc6uwqq6", "errorcode": "9", "errordata": ["Different request reference"], "requesttypedescription": "ERROR" }] }), ({}, sterror("9", data=["Different Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "9", "errordata": ["Different Issue"] }] }), ({}, sterror("1", data=["Generic Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "1", "errordata": ["Generic Issue"] }] }), ({}, sterror("2", data=["An Issue"]), "Ahc6uwqq6", { "requestreference": "Ahc6uwqq6", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Ahc6uwqq6", "errorcode": "2", "errordata": ["An Issue"] }] }), ({}, sendrecverror("4", data=["Send Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "4", "errordata": ["Send Issue"] }] }), ({}, sendrecverror("5", data=["Receive Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "5", "errordata": ["Receive Issue"] }] }), ({}, connecterror("7", data=["Connection Issue"]), "Arckthaau", { "requestreference": "Arckthaau", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Arckthaau", "errorcode": "7", "errordata": ["Connection Issue"] }] }), ({}, connecterror("8", data=["Unexpected Connection Issue"]), "Armm51h6v", { "requestreference": "Armm51h6v", "responses": [{ "requesttypedescription": "ERROR", "requestreference": "Armm51h6v", "errorcode": "8", "errordata": ["Unexpected Connection Issue"] }] }), ({ "locale": "fr_fr" }, apierror("10", data=["Api Issue"]), "Ahc6uwqq6", { u'requestreference': u'Ahc6uwqq6', u'responses': [{ 'errorcode': u'10', 'requesttypedescription': u'ERROR', 'requestreference': u'Ahc6uwqq6', 'errordata': [u'Api Issue'], }] }), ] for configData, exception, request_reference, exp_update in tests: config = self.get_config(data=configData) api = securetrading.Api(config) actual = api._generate_st_error(exception, request_reference) exp_response = securetrading.Response() exp_response.update(exp_update) self.assertEqual(actual, exp_response)
def get_securetrading_response(self, response_dict): securetrading_response = securetrading.Response() securetrading_response.update(response_dict) return securetrading_response