Skip to content

Commit

Permalink
Error handling (#25)
Browse files Browse the repository at this point in the history
* Develop (#24)

* Issue/7 (#16)

* issue#7

now stores the time the token was created and re-uses it for 3:45 minutes before requesting a new one.

updated the example file as the manifest info will always change.

* issue#7

now stores the time the token was created and re-uses it for 3:45 minutes before requesting a new one.

updated the example file as the manifest info will always change.

* version bump 0.0.3

* Add service helpers (#19)

* Issue/7 (#16) (#17)

* issue#7

now stores the time the token was created and re-uses it for 3:45 minutes before requesting a new one.

updated the example file as the manifest info will always change.

* issue#7

now stores the time the token was created and re-uses it for 3:45 minutes before requesting a new one.

updated the example file as the manifest info will always change.

* version bump 0.0.3

* ready for 0.0.4

Better way of selecting service options by a friendly name
Added in dictionaries of possible options and the relevant RM codes to send.

all lowercase, spaces replaced with _

* adjust Exception types to be more logical

* remove cli, not using

add in if required later

* fix version

* Bump version: 0.0.3 → 0.0.4

* tox breaking

* tox breaking

* Tests (#23)

* add in defaults of None to some of our service offerings

start some tests

* more tests

add in some better exceptions to the delivery object builder

* Bump version: 0.0.4 → 0.0.5

* fix makefile to build the distribution after the clean step in release otherwise it was just deleting everything it wanted to upload!

* Create a RoyalMailError to raise when we get errors back from RM themselves.
create an error handler to use instead of just raise_for_status so we get a true error back and return the JSON so the requesting application can deal with the exceptions gracefully.
  • Loading branch information
Bobspadger committed Apr 6, 2018
1 parent d924661 commit 3219656
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ docs: ## generate Sphinx HTML documentation, including API docs
servedocs: docs ## compile the docs watching for changes
watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D .

release: clean ## package and upload a release
release: clean dist ## package and upload a release
twine upload dist/*

dist: clean ## builds source and wheel package
Expand Down
3 changes: 3 additions & 0 deletions royal_mail_rest_api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ class NotAuthorised(Exception):

class GeneralError(Exception):
pass

class RoyalMailError(Exception):
pass
4 changes: 2 additions & 2 deletions royal_mail_rest_api/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
body.add_service_type('royal_mail_tracked')
body.add_service_format('inland_large_letter')
body.add_service_offering('royal_mail_tracked_24')
body.add_service_enhancements('e-mail_notification')
body.add_service_enhancements('sms_and_e-mail_notification')
body.add_service_occurence()
body.add_signature()
body.customer_reference = 'D123456'
body.department_reference = 'Q123456'
body.sender_reference = 'A123456'
body.add_items(1, 100, 'g')
body.add_receipient_contact('Joe Bloggs', 'joe.bloggs@royalmail.com', None, '07970810000')
body.add_receipient_address('Broadgate Circle', 'London', None, 'EC1A 1BB', country='GB', building_number='1',
body.add_receipient_address('Broadgate Circle', 'London', None, 'BH10 6JJ', country='GB', building_number='1',
address_line2='Add line 2', address_line3='Add line 3', building_name='My building')

# Request our body to use to request a label from royal mail
Expand Down
25 changes: 18 additions & 7 deletions royal_mail_rest_api/shipping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import datetime
from royal_mail_rest_api.api import RoyalMailBaseClass

from royal_mail_rest_api.errors import RoyalMailError


class ShippingApi(RoyalMailBaseClass):
Expand Down Expand Up @@ -76,6 +76,17 @@ def get_token(self):
self.token_created = datetime.datetime.now()
self._create_token_header()

def _error_handler(self, result):
"""
Deal with returning a useful error up the chain, not just an http response error for the client app to deal with.
:param result:
:return:
"""
try:
result.raise_for_status()
except Exception:
raise RoyalMailError(result.json())


def post_domestic(self, data):
"""
Expand All @@ -94,7 +105,7 @@ def post_domestic(self, data):
self.get_token()
data = data
result = requests.post('{}{}'.format(self.url, self.post_domestic_url), json=data, headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

def put_shipment(self, shipment_number, data):
Expand All @@ -116,7 +127,7 @@ def put_shipment(self, shipment_number, data):
self.get_token()
result = requests.put('{}{}{}'.format(self.url, self.put_shipment_update_url, shipment_number), json=data,
headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

def delete_shipment(self, shipment_number):
Expand All @@ -130,7 +141,7 @@ def delete_shipment(self, shipment_number):
self.get_token()
result = requests.delete('{}{}{}'.format(self.url, self.delete_shipment_url, shipment_number),
headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

def put_shipment_label(self, shipment_number):
Expand All @@ -145,7 +156,7 @@ def put_shipment_label(self, shipment_number):
self.get_token()
result = requests.put('{}{}{}/label'.format(self.url, self.put_shipment_label_url, shipment_number),
headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

def post_manifest(self, manifest_options=None):
Expand All @@ -160,7 +171,7 @@ def post_manifest(self, manifest_options=None):
self.get_token()
result = requests.post('{}{}'.format(self.url, self.post_manifest_url), json=manifest_options,
headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

def put_manifest(self, sales_order_number=None, manifest_batch_number=None):
Expand All @@ -181,5 +192,5 @@ def put_manifest(self, sales_order_number=None, manifest_batch_number=None):

result = requests.put('{}{}?{}'.format(self.url, self.post_manifest_url, query_params),
headers=self.tokenheader)
result.raise_for_status()
self._error_handler(result)
return result.json()

0 comments on commit 3219656

Please sign in to comment.