Skip to content

Commit

Permalink
Update to use Python 3 print function
Browse files Browse the repository at this point in the history
This includes all the examples and the commented prints in the source.
  • Loading branch information
PeterJCLaw committed Nov 28, 2017
1 parent adabe92 commit 4286db3
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 79 deletions.
14 changes: 8 additions & 6 deletions examples/cancel_labels_create.py
@@ -1,10 +1,12 @@
from __future__ import print_function

from credentials import *

# TODO put ID of the label you wish to cancel
label = None

if label is None:
print 'label is not set, modify cancel_labels_create.py'
print('label is not set, modify cancel_labels_create.py')

payload = {
'label': {
Expand All @@ -15,10 +17,10 @@
try:
api = Postmen(key, region)
result = api.create('cancel-labels', payload)
print "RESULT"
print("RESULT")
pp.pprint(result)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/cancel_labels_retrieve.py
@@ -1,22 +1,24 @@
from __future__ import print_function

from credentials import *

# TODO put ID of a particular label
label = None

if label is None:
print 'label is not set, modify cancel_labels_retrieve.py'
print('label is not set, modify cancel_labels_retrieve.py')

try:
api = Postmen(key, region)
# get all the cancelled labels
result_all = api.get('cancel-labels')
# get a particular cancelled label
result_particular = api.get('cancel-labels', label)
print "RESULT"
print("RESULT")
pp.pprint(result_all)
pp.pprint(result_particular)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
6 changes: 4 additions & 2 deletions examples/credentials.py
@@ -1,3 +1,5 @@
from __future__ import print_function

import pprint

pp = pprint.PrettyPrinter(indent=4)
Expand All @@ -15,7 +17,7 @@
endpoint = None

if key is None :
print 'key is not set, modify file credentials.py'
print('key is not set, modify file credentials.py')

if region is None:
print 'region is not set, modify file credentials.py'
print('region is not set, modify file credentials.py')
26 changes: 14 additions & 12 deletions examples/error.py
@@ -1,3 +1,5 @@
from __future__ import print_function

from credentials import *

# most obvious way would be to surround our
Expand All @@ -10,10 +12,10 @@
api = Postmen('THIS IS NOT A VALID API KEY', region)
result = api.get('labels')
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())

# we also can enable the safe mode,
# this way try...except... is no
Expand All @@ -24,17 +26,17 @@
api = Postmen('THIS IS NOT A VALID API KEY', region, safe=True)
e = api.getError()
if e is not None:
print "ERROR IN THE CONSTRUCTOR"
print e.code()
print e.message()
print e.details()
print("ERROR IN THE CONSTRUCTOR")
print(e.code())
print(e.message())
print(e.details())

# perform call anyway

result = api.get('labels')
if result is None:
e = api.getError()
print "ERROR IN THE CALL"
print e.code()
print e.message()
print e.details()
print("ERROR IN THE CALL")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/labels_create.py
@@ -1,10 +1,12 @@
from __future__ import print_function

from credentials import *

# TODO put ID of your shipper account
shipper = None

if shipper is None:
print 'shipper is not set, modify labels_create.py'
print('shipper is not set, modify labels_create.py')

parcel = {
'description': 'info about the parcel',
Expand Down Expand Up @@ -99,10 +101,10 @@
try:
api = Postmen(key, region)
result = api.create('labels', payload)
print "RESULT"
print("RESULT")
pp.pprint(result)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/labels_retrieve.py
@@ -1,22 +1,24 @@
from __future__ import print_function

from credentials import *

# TODO put ID of a particular label
label = None

if label is None:
print 'label is not set, modify labels_retrieve.py'
print('label is not set, modify labels_retrieve.py')

try:
api = Postmen(key, region)
# get all the cancelled labels
result_all = api.get('labels')
# get a particular cancelled label
result_particular = api.get('labels', label)
print "RESULT"
print("RESULT")
pp.pprint(result_all)
pp.pprint(result_particular)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/manifests_create.py
@@ -1,10 +1,12 @@
from __future__ import print_function

from credentials import *

# TODO put ID of your shipper account
shipper = None

if shipper is None:
print 'shipper is not set, modify manifests_create.py'
print('shipper is not set, modify manifests_create.py')

payload = {
'shipper_account': {
Expand All @@ -16,10 +18,10 @@
try:
api = Postmen(key, region)
result = api.create('manifests', payload)
print "RESULT"
print("RESULT")
pp.pprint(result)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/manifests_retrieve.py
@@ -1,22 +1,24 @@
from __future__ import print_function

from credentials import *

# TODO put ID of a particular manifest
manifest = None

if manifest is None:
print 'manifest is not set, modify manifests_retrieve.py'
print('manifest is not set, modify manifests_retrieve.py')

try:
api = Postmen(key, region)
# get all manifests
result_all = api.get('manifests')
# get a particular manifest
result_particular = api.get('manifests', manifest)
print "RESULT"
print("RESULT")
pp.pprint(result_all)
pp.pprint(result_particular)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
12 changes: 7 additions & 5 deletions examples/proxy.py
@@ -1,3 +1,5 @@
from __future__ import print_function

from credentials import *

# follow proxy scheme used in requests library
Expand All @@ -11,10 +13,10 @@
try:
api = Postmen(key, region, proxy = proxies)
result = api.get('labels')
print "RESULT"
print("RESULT")
pp.pprint(result)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
16 changes: 9 additions & 7 deletions examples/rates_create.py
@@ -1,10 +1,12 @@
from __future__ import print_function

from credentials import *

# TODO put ID of your shipper account
shipper = None

if shipper is None:
print 'shipper is not set, modify rates_create.py'
print('shipper is not set, modify rates_create.py')

item = {
'description': 'Food Bar',
Expand Down Expand Up @@ -79,18 +81,18 @@
}
],
'ship_from': sender,
'ship_to': receiver
'ship_to': receiver
},
'is_document': False
}

try:
api = Postmen(key, region)
result = api.create('rates', payload)
print "RESULT"
print("RESULT")
pp.pprint(result)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
14 changes: 8 additions & 6 deletions examples/rates_retrieve.py
@@ -1,22 +1,24 @@
from __future__ import print_function

from credentials import *

# TODO put ID of a particular rate
rate = None

if rate is None:
print 'rate is not set, modify rates_retrieve.py'
print('rate is not set, modify rates_retrieve.py')

try:
api = Postmen(key, region)
# get all rates
result_all = api.get('rates')
# get a particular rate
result_particular = api.get('rates', rate)
print "RESULT"
print("RESULT")
pp.pprint(result_all)
pp.pprint(result_particular)
except PostmenException as e:
print "ERROR"
print e.code()
print e.message()
print e.details()
print("ERROR")
print(e.code())
print(e.message())
print(e.details())
20 changes: 11 additions & 9 deletions postmen/__init__.py
@@ -1,3 +1,5 @@
from __future__ import print_function

"""Postmen and PostmenException classes are intended for SDK users.
"""

Expand Down Expand Up @@ -151,12 +153,12 @@ def _response(self, response, **kwargs):
raw = kwargs.get('raw', self._raw)
time = kwargs.get('time', self._time)

# print response.headers
# print response.text
# print(response.headers)
# print(response.text)

sec_before_reset = response.headers.get('x-ratelimit-reset', '0')
sec_before_reset = int(sec_before_reset) / 1000
#print sec_before_reset
#print(sec_before_reset)
if sec_before_reset:
if not self._time_before_reset or self._time_before_reset < sec_before_reset:
self._time_before_reset = int(sec_before_reset)
Expand All @@ -165,8 +167,8 @@ def _response(self, response, **kwargs):
if self._calls_left:
self._calls_left = int(self._calls_left)

# print 'self._time_before_reset', self._time_before_reset
# print 'self._calls_left', self._calls_left
# print('self._time_before_reset', self._time_before_reset)
# print('self._calls_left', self._calls_left)

if response.text:
if raw:
Expand All @@ -179,7 +181,7 @@ def _response(self, response, **kwargs):
error_message = "Something went wrong on Postmen's end"
raise PostmenException(message = error_message, code = 500)
meta_code = ret.get('meta', {}).get('code', None)
# print ret
# print(ret)
if not meta_code:
raise PostmenException(message='API response missed meta info', **ret)
if int(meta_code) != 200 and int(meta_code / 1000) != 3:
Expand Down Expand Up @@ -228,14 +230,14 @@ def _get_requests_params(self, method, path, **kwargs):

def _apply_rate_limit(self):
if isinstance(self._calls_left, six.integer_types) and self._calls_left <= 0:
# print 'self._time_before_reset', self._time_before_reset
# print 'int(time_module.time())', int(time_module.time())
# print('self._time_before_reset', self._time_before_reset)
# print('int(time_module.time())', int(time_module.time()))
delta = self._time_before_reset - int(time_module.time())
if delta > 0:
if not self._rate:
raise PostmenException(message = 'You have exceeded the API call rate limit. Please retry again at X-RateLimit-Reset header timestamp', code = 429, retryable = True)
else :
# print 'apply delay', delta
# print('apply delay', delta)
self._delay(delta)

def _call_ones(self, method, path, **kwargs):
Expand Down

0 comments on commit 4286db3

Please sign in to comment.