From 72cb788e8a8a974c5b0c412be2d911cfd48ce9dd Mon Sep 17 00:00:00 2001 From: AlexisLikesTea Date: Wed, 22 Jan 2025 12:05:37 +1100 Subject: [PATCH 1/4] New test fixtures --- test/fixtures/sample_client_error.json | 4 ++++ test/fixtures/sample_server_error.json | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 test/fixtures/sample_client_error.json create mode 100644 test/fixtures/sample_server_error.json diff --git a/test/fixtures/sample_client_error.json b/test/fixtures/sample_client_error.json new file mode 100644 index 0000000..7efdb88 --- /dev/null +++ b/test/fixtures/sample_client_error.json @@ -0,0 +1,4 @@ +{ + "Code": 418, + "Message": " I'm a teapot" +} diff --git a/test/fixtures/sample_server_error.json b/test/fixtures/sample_server_error.json new file mode 100644 index 0000000..53b24e7 --- /dev/null +++ b/test/fixtures/sample_server_error.json @@ -0,0 +1,4 @@ +{ + "Code": 500, + "Message": "Internal Server Error" +} From 215c4f64f78fba5fba4ff6f68a6ac9276518d125 Mon Sep 17 00:00:00 2001 From: AlexisLikesTea Date: Wed, 22 Jan 2025 12:07:27 +1100 Subject: [PATCH 2/4] Update tests to use new fixtures --- test/test_createsend.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_createsend.py b/test/test_createsend.py index 720e9fa..da0566d 100644 --- a/test/test_createsend.py +++ b/test/test_createsend.py @@ -147,11 +147,11 @@ def test_not_found_on_get(self): self.assertRaises(self.error_responses[404], self.cs.countries) def test_other_client_error_on_get(self): - self.cs.stub_request('countries.json', None, status=418) + self.cs.stub_request('countries.json', 'sample_client_error.json', status=418) self.assertRaises(self.error_responses[418], self.cs.countries) def test_server_error_on_get(self): - self.cs.stub_request('countries.json', None, status=500) + self.cs.stub_request('countries.json', 'sample_server_error.json', status=500) self.assertRaises(self.error_responses[500], self.cs.countries) def test_bad_request_on_post(self): @@ -176,13 +176,13 @@ def test_not_found_on_post(self): def test_other_client_error_on_post(self): client = Client(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") - client.stub_request('clients.json', None, status=418) + client.stub_request('clients.json', 'sample_client_error.json', status=418) self.assertRaises(self.error_responses[418], client.create, "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia") def test_server_error_on_post(self): client = Client(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") - client.stub_request('clients.json', None, status=500) + client.stub_request('clients.json', 'sample_server_error.json', status=500) self.assertRaises(self.error_responses[500], client.create, "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia") @@ -210,14 +210,14 @@ def test_not_found_on_put(self): def test_other_client_error_on_put(self): template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") template.stub_request( - 'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=418) + 'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_client_error.json', status=418) self.assertRaises(self.error_responses[418], template.update, "Template One Updated", "http://templates.org/index.html", "http://templates.org/files.zip") def test_server_error_on_put(self): template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") template.stub_request( - 'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500) + 'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_server_error.json', status=500) self.assertRaises(self.error_responses[500], template.update, "Template One Updated", "http://templates.org/index.html", "http://templates.org/files.zip") @@ -242,13 +242,13 @@ def test_not_found_on_delete(self): def test_other_client_error_on_delete(self): template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") template.stub_request( - 'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=418) + 'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_client_error.json', status=418) self.assertRaises(self.error_responses[418], template.delete) def test_server_error_on_delete(self): template = Template(self.cs.auth_details, "uhiuhiuhiuhiuhiuhiuh") template.stub_request( - 'templates/uhiuhiuhiuhiuhiuhiuh.json', None, status=500) + 'templates/uhiuhiuhiuhiuhiuhiuh.json', 'sample_server_error.json', status=500) self.assertRaises(self.error_responses[500], template.delete) From ae2a07fd9c8527b5ea2456dcd20fdab37a0853cb Mon Sep 17 00:00:00 2001 From: AlexisLikesTea Date: Wed, 22 Jan 2025 12:07:56 +1100 Subject: [PATCH 3/4] Update json_to_py --- lib/createsend/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/createsend/utils.py b/lib/createsend/utils.py index 7396916..6e0ec31 100644 --- a/lib/createsend/utils.py +++ b/lib/createsend/utils.py @@ -107,8 +107,9 @@ def connect(self): raise -def json_to_py(j): - o = json.loads(j.decode('utf-8')) +def json_to_py(o): + if isinstance(o,bytes): + o = json.loads(o.decode('utf-8')) if isinstance(o, dict): return dict_to_object(o) else: From a78ecbe9ceec76a89a9b56ae33a8e687304f66ad Mon Sep 17 00:00:00 2001 From: AlexisLikesTea Date: Wed, 22 Jan 2025 12:08:17 +1100 Subject: [PATCH 4/4] Update ClientError and ServerError methods --- lib/createsend/createsend.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/createsend/createsend.py b/lib/createsend/createsend.py index 6e749dd..b1156dd 100644 --- a/lib/createsend/createsend.py +++ b/lib/createsend/createsend.py @@ -229,9 +229,11 @@ def handle_response(self, status, data): elif status == 404: raise NotFound() elif status in range(400, 500): - raise ClientError() + json_data = json_to_py(data) + raise ClientError(f"Code: {json_data.Code} {json_data.Message}") elif status in range(500, 600): - raise ServerError() + json_data = json_to_py(data) + raise ServerError(f"Code: {json_data.Code} {json_data.Message}") return data def _get(self, path, params={}, username=None, password=None):