Skip to content

Commit

Permalink
Add service helpers (#19)
Browse files Browse the repository at this point in the history
* 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 _
  • Loading branch information
Bobspadger committed Apr 5, 2018
1 parent 1fd65fd commit 9f383c5
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 16 deletions.
13 changes: 10 additions & 3 deletions royal_mail_rest_api/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

# Add our items to it
body.add_ship_date(None)
body.add_service('P', 1, 'TPN', 'T', True, ['14'])
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_occurence()
body.add_signature()
body.customer_reference = 'D123456'
body.department_reference = 'Q123456'
body.sender_reference = 'A123456'
Expand All @@ -37,8 +42,10 @@
# Request an authentication token
my_shipping.get_token()
# Post a shipping request
post_shipping = my_shipping.post_domestic(my_rm_body)

try:
post_shipping = my_shipping.post_domestic(my_rm_body)
except Exception as e:
print(e)
# Store our tracking id for use.
tracking_ref = post_shipping['completedShipments'][0]['shipmentItems'][0]['shipmentNumber']

Expand Down
218 changes: 205 additions & 13 deletions royal_mail_rest_api/tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,157 @@
import datetime

from royal_mail_rest_api.errors import *

class RoyalMailBody:
service_formats = {
'inland_large_letter': 'F',
'inland_letter': 'L',
'inland_format_not_applicable': 'N',
'inland_parcel': 'P',
'international_parcel': 'E',
'international_large_letter': 'G',
'international_format_not_applicable': 'N',
'international_letter': 'P'
}

service_types = {
'royal_mail_24': '1',
'Royal Mail 48': '2',
'special_delivery': 'D',
'BFPO': 'H',
'international': 'I',
'tracked_returns': 'R',
'royal_mail_tracked': 'T'
}

service_offerings = {
"royal_mail_24_48": "CRL",
"intl_bus_parcels_zero_sort_hi_vol_priority_i": "DE1",
"intl_bus_parcels_zero_sort_hi_vol_economy_": "DE3",
"intl_bus_parcels_zero_srt_lo_vol_priority_": "DE4",
"intl_bus_parcels_zero_srt_lo_vol_economy_": "DE6",
"intl_bus_mail_l_ltr_ctry_srt_hi_vol_priority_": "DG1",
"intl_bus_mail_l_ltr_ctry_srt_hi_vol_economy_": "DG3",
"intl_bus_mail_l_ltr_ctry_srt_lo_vol_priority_": "DG4",
"intl_bus_mail_l_ltr_ctry_srt_lo_vol_economy_": "DG6",
"royal_mail_24_sort8_ll_flat_rate": "FS1",
"royal_mail_48_sort8_ll_flat_rate": "FS2",
"intl_bus_parcels_zone_sort_priority_": "IE1",
"intl_bus_parcels_zone_sort_economy": "IE3",
"intl_bus_mail_lrg_ltr_zone_sort_pri": "IG1",
"intl_bus_mail_lrg_ltr_zone_sort_economy": "IG3",
"intl_bus_mail_lrg_ltr_zone_srt_pri_mch": "IG4",
"intl_bus_mail_l_ltr_zone_srt_economy_mch_": "IG6",
"intl_bus_parcels_print_direct_priority": "MB1",
"intl_bus_parcels_print_direct_standard": "MB2",
"intl_bus_parcels_print_direct_economy": "MB3",
"intl_bus_parcels_signed_extra_comp_ctry": "MP0",
"intl_bus_parcels_tracked": "MP1",
"intl_bus_parcels_tracked_extra_comp": "MP4",
"intl_bus_parcels_signed": "MP5",
"intl_bus_parcels_signed_extra_comp": "MP6",
"intl_bus_parcels_tracked_country_priced": "MP7",
"intl_bus_parcels_tracked_extra_comp_ctry": "MP8",
"intl_bus_parcels_signed_country_priced": "MP9",
"intl_bus_parcels_tracked_and_signed": "MTA",
"intl_bus_parcels_tracked_signed_xtr_comp": "MTB",
"intl_bus_mail_tracked_and_signed": "MTC",
"intl_bus_mail_tracked_and_signed_xtr_comp": "MTD",
"intl_bus_parcels_tracked_and_signed__ctry": "MTE",
"intl_bus_parcel_track&sign_xtr_cmp_ctry": "MTF",
"intl_bus_mail_tracked_and_signed_country": "MTG",
"intl_bus_mail_track_and_sign_xtr_comp_ctry": "MTH",
"intl_bus_mail_tracked": "MTI",
"intl_bus_mail_tracked_extra_comp": "MTJ",
"intl_bus_mail_tracked_country_priced": "MTK",
"intl_bus_mail_tracked_extra_comp_ctry": "MTL",
"intl_bus_mail_signed": "MTM",
"intl_bus_mail_signed_extra_comp": "MTN",
"intl_bus_mail_signed_country_priced": "MTO",
"intl_bus_mail_signed_extra_comp_country": "MTP",
"intl_bus_parcels_zone_sort_plus_priority": "MTQ",
"intl_bus_parcels_zone_srt_plus_economy": "MTS",
"intl_standard_on_account": "OLA",
"intl_economy_on_account": "OLS",
"international_signed_on_account": "OSA",
"intl_signed_on_account_extra_comp": "OSB",
"international_tracked_on_account": "OTA",
"intl_tracked_on_account_extra_comp": "OTB",
"international_tracked_and_signed_on_acct": "OTC",
"intl_tracked_and_signed_on_acct_extra_comp": "OTD",
"intl_bus_mail_mixed_zone_sort_priority": "OZ1",
"intl_bus_mail_mixed_zone_sort_economy": "OZ3",
"intl_bus_mail_mixed_zone_sort_pri_mch": "OZ4",
"intl_bus_mail_mixed_zone_srt_economy_mch": "OZ6",
"royal_mail_48_ll_flat_rate": "PK0",
"royal_mail_24_sort8_p_flat_rate": "PK1",
"royal_mail_48_sort8_p_flat_rate": "PK2",
"royal_mail_24_sort8_llp_daily_rate": "PK3",
"royal_mail_48_sort8_llp_daily_rate": "PK4",
"royal_mail_24_ll_flat_rate": "PK9",
"royal_mail_24_48_p_flat_rate": "PPF",
"intl_bus_parcels_max_sort_economy": "PS0",
"intl_bus_parcels_max_sort_standard": "PSC",
"intl_bus_parcels_max_sort_priority": "PS9",
"intl_bus_mail_lrg_ltr_max_sort_economy": "PS8",
"intl_bus_mail_lrg_ltr_max_sort_standard": "PSB",
"intl_bus_mail_lrg_ltr_max_sort_priority": "PS7",
"royal_mail_48_sort8_p_daily_rate": "RM0",
"royal_mail_24_ll_daily_rate": "RM1",
"royal_mail_24_p_daily_rate": "RM2",
"royal_mail_48_ll_daily_rate": "RM3",
"royal_mail_48_p_daily_rate": "RM4",
"royal_mail_24_p_flat_rate": "RM5",
"royal_mail_48_p_flat_rate": "RM6",
"royal_mail_24_sort8_ll_daily_rate": "RM7",
"royal_mail_24_sort8_p_daily_rate": "RM8",
"royal_mail_48_sort8_ll_daily_rate": "RM9",
"sd_guaranteed_by_1pm": "SD1",
"sd_guaranteed_by_1pm_1000": "SD2",
"sd_guaranteed_by_1pm_2500": "SD3",
"sd_guaranteed_by_9am": "SD4",
"sd_guaranteed_by_9am_1000": "SD5",
"sd_guaranteed_by_9am_2500": "SD6",
"1st_and_2nd_class_account_mail": "STL",
"royal_mail_tracked_48_hv": "TPL",
"royal_mail_tracked_24_hv": "TPM",
"royal_mail_tracked_24": "TPN",
"royal_mail_tracked_48": "TPS",
"royal_mail_tracked_48_lbt_hv": "TRL",
"royal_mail_tracked_24_lbt_hv": "TRM",
"royal_mail_tracked_24_lbt": "TRN",
"royal_mail_tracked_48_lbt": "TRS",
"royal_mail_tracked_returns_24": "TSN",
"royal_mail_tracked_returns_48": "TSS",
"intl_bus_parcels_zero_sort_priority": "WE1",
"intl_bus_parcels_zero_sort_economy": "WE3",
"intl_bus_mail_lrg_ltr_zero_srt_priority": "WG1",
"intl_bus_mail_lrg_ltr_zero_sort_economy": "WG3",
"intl_bus_mail_lrg_ltr_zero_srt_pri_mch": "WG4",
"intl_bus_mail_l_ltr_zero_srt_economy_mch": "WG6",
"intl_bus_mail_mixed_zero_sort_priority": "WW1",
"intl_bus_mail_mixed_zero_sort_economy": "WW3",
"intl_bus_mail_mixed_zero_sort_pri_mch": "WW4",
"intl_bus_mail_mixd_zero_srt_economy_mch": "WW6",
"intl_bus_mail_mixed_zero_sort_premium": "ZC1",
}

service_enhancements = {
"loss_1000": "1",
"loss_2500": "2",
"loss_5000": "3",
"loss_7500": "4",
"loss_10000": "5",
"recorded": "6",
"loss_750": "11",
"signature": "12",
"sms_notification": "13",
"e-mail_notification": "14",
"safeplace": "15",
"sms_and_e-mail_notification": "16",
"local_collect": "22",
"saturday_guaranteed": "24",
}

def __init__(self, shipment_type):
self.receipient = None
self.address = None
Expand All @@ -14,7 +164,7 @@ def __init__(self, shipment_type):
self.items = []
self.item_count = len(self.items)
self.safe_place = None

self.enhancements = []
def return_domestic_body(self):
"""
build domestic body from items
Expand All @@ -23,7 +173,7 @@ def return_domestic_body(self):

domestic_body = {
'shipmentType': self.shipment_type,
'service': self.service,
'service': self._add_service(),
'shippingDate': self.shipping_date,
'items': self.items,
'recipientContact': self.receipient,
Expand Down Expand Up @@ -84,20 +234,62 @@ def add_ship_date(self, date_obj=None):
date_obj = datetime.datetime.today()
self.shipping_date = datetime.datetime.strftime(date_obj, '%Y-%m-%d')

def add_service(self, format=None, occurrence=None, offering=None, _type=None, signature=None, enhancements=None):
if not isinstance(enhancements, list):
enhancements = [enhancements]
def _add_service(self):

service = {
"format": format,
"occurrence": occurrence,
"offering": offering,
"type": _type,
"signature": signature,
"enhancements": enhancements
"format": self.service_format,
"occurrence": self.service_occurence,
"offering": self.service_offering,
"type": self.service_type,
"signature": self.signature,
"enhancements": self.enhancements
}

self.service = service
return service

def add_service_format(self, format=None):
if format is None:
raise(KeyError('No service format selected'))
if format not in self.service_formats:
raise(ValueError('Invalid service format'))
self.service_format = self.service_formats[format]


def add_service_type(self, service_type):
if service_type is None:
raise(KeyError('No service type selected'))
if service_type not in self.service_types:
raise(ValueError('Invalid service type'))
self.service_type = self.service_types[service_type]


def add_service_offering(self, service_offering):
if service_offering is None:
raise(KeyError('No service type selected'))
if service_offering not in self.service_offerings:
raise(ValueError('Invalid service type'))
self.service_offering = self.service_offerings[service_offering]


def add_service_occurence(self):
# TODO - what is this, can't find anything in the docs
self.service_occurence = 1


def add_signature(self, signature=False):
if isinstance(signature, bool):
self.signature = signature
else:
raise(ValueError('Must be a boolean, True or False'))


def add_service_enhancements(self, enhancement):
if enhancement is None:
raise(KeyError('No Enhancement Selected'))
if enhancement not in self.service_enhancements:
raise(ValueError('Not in service_enhancements'))
self.enhancements.append(self.service_enhancements[enhancement])


def add_receipient_contact(self, name, email, complementary_name=None, telephone=None):
receipient = {
Expand Down

0 comments on commit 9f383c5

Please sign in to comment.