Skip to content

Commit

Permalink
Issue/7 (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Bobspadger committed Apr 3, 2018
1 parent 6e2a57a commit 1fd65fd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
10 changes: 10 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
History
=======

0.0.3 (2018-04-03)
------------------
Store token for 4 hours (issue #7)
Fix some poor documentation
Fix some examples

0.0.2 (2018-04-03)
------------------
Fixing PyPI install not working

0.0.1 (2018-03-26)
------------------

Expand Down
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ It is VERY MUCH a work in progress, so help is hugely appreciated, and be carefu
Features
--------

* Better error handling
* Country code verification
* Postcode regex
Create Labels
Update Labels
Create Manifests
Post Manifests


Credits
Expand Down
16 changes: 13 additions & 3 deletions royal_mail_rest_api/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import datetime
from royal_mail_rest_api.tools import RoyalMailBody
from royal_mail_rest_api.shipping import ShippingApi
from royal_mail_rest_api.tracking import TrackingApi
Expand Down Expand Up @@ -59,9 +59,19 @@

# If we have some labels to manifest - request it
manifest_info = {'yourReference': '123'}
manifest_data = my_shipping.post_manifest(manifest_info)
try:
manifest_data = my_shipping.post_manifest(manifest_info)
# Get the manifest doumentation - note, you will need the maniefest number to get this
manifest_label = my_shipping.put_manifest(manifest_batch_number=5)
manifest_label = my_shipping.put_manifest(manifest_batch_number=5)
except Exception as e:
# this will probably fail unless you change some details
print(e)
# Test we don't need a new token:

my_shipping.get_token()
# now change the self.token_created to be 5 hours ago
my_shipping.token_created = datetime.datetime.now() - datetime.timedelta(hours=5)
my_shipping.get_token()


# Now, a period of time has passed, we can track those packages
Expand Down
25 changes: 19 additions & 6 deletions royal_mail_rest_api/shipping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import requests
import datetime
from royal_mail_rest_api.api import RoyalMailBaseClass



class ShippingApi(RoyalMailBaseClass):
"""
Royal Mail Shipping Class, used to communicate with the Royal Mail Rest API to create labels
Expand Down Expand Up @@ -29,6 +31,9 @@ def __init__(self, client_id, client_secret, username, password):
self.client_secret = client_secret
self.username = username
self.password = password

# make a date 30 days in the past so the token is expired.
self.token_created = datetime.datetime.now() - datetime.timedelta(days=30)
self._create_authentication_header()

def _create_authentication_header(self):
Expand Down Expand Up @@ -62,12 +67,15 @@ def get_token(self):
:return:
"""
# Token expires after 4 hours, lets give it 3.45 hours to be safe
if self.token_created < (datetime.datetime.now() - datetime.timedelta(hours=3, minutes=45)):
result = requests.get('{}{}'.format(self.url, self.token_url), headers=self.header)
result.raise_for_status()
result = result.json()
self.token = result['token']
self.token_created = datetime.datetime.now()
self._create_token_header()

result = requests.get('{}{}'.format(self.url, self.token_url), headers=self.header)
result.raise_for_status()
result = result.json()
self.token = result['token']
self._create_token_header()

def post_domestic(self, data):
"""
Expand All @@ -83,6 +91,7 @@ def post_domestic(self, data):
it will return the shipment numbers and item details.
:return:
"""
self.get_token()
data = data
result = requests.post('{}{}'.format(self.url, self.post_domestic_url), json=data, headers=self.tokenheader)
result.raise_for_status()
Expand All @@ -104,7 +113,7 @@ def put_shipment(self, shipment_number, data):
:return:
"""
# TODO: returning 400 errors at present
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()
Expand All @@ -118,6 +127,7 @@ def delete_shipment(self, shipment_number):
:return:
"""
self.get_token()
result = requests.delete('{}{}{}'.format(self.url, self.delete_shipment_url, shipment_number),
headers=self.tokenheader)
result.raise_for_status()
Expand All @@ -132,6 +142,7 @@ def put_shipment_label(self, shipment_number):
:return:
"""
self.get_token()
result = requests.put('{}{}{}/label'.format(self.url, self.put_shipment_label_url, shipment_number),
headers=self.tokenheader)
result.raise_for_status()
Expand All @@ -146,6 +157,7 @@ def post_manifest(self, manifest_options=None):
:return:
"""
self.get_token()
result = requests.post('{}{}'.format(self.url, self.post_manifest_url), json=manifest_options,
headers=self.tokenheader)
result.raise_for_status()
Expand All @@ -161,6 +173,7 @@ def put_manifest(self, sales_order_number=None, manifest_batch_number=None):
:return:
"""
self.get_token()
items = {'salesOrderNumber': sales_order_number, 'manifestBatchNumber': manifest_batch_number}
params = ['{}={}'.format(k, str(v)) for k, v in items.items() if v is not None]

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
'Documentation': 'http://royal-mail-rest-api.readthedocs.io/en/latest/index.html'},
download_url='https://github.com/Bobspadger/royal_mail_rest_api',
url='https://github.com/bobspadger/royal_mail_rest_api',
version='0.0.2',
version='0.0.3',
zip_safe=False,
)

0 comments on commit 1fd65fd

Please sign in to comment.