Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Lots of goodness

  • Loading branch information...
commit f49effc4e55232011b1f8cc0db45593d9eb0f822 1 parent d49a73b
@benweatherman authored
View
2  __init__.py
@@ -1 +1 @@
-__all__ = [ 'UPS', 'USPS', 'endicia', 'shipping_common' ]
+__all__ = [ 'UPS', 'USPS', 'endicia', 'common' ]
View
56 endicia/endicia.py → endicia.py
@@ -3,7 +3,7 @@
import base64
import xml.etree.ElementTree as etree
-class package(object):
+class Package(object):
shipment_types = [
'Priority',
'Express',
@@ -33,13 +33,13 @@ def __init__(self, weight_oz, shape, length, width, height, description='', valu
self.description = description
self.value = str(value)
-class request(object):
+class EndiciaRequest(object):
def __init__(self, url, api):
self.debug = True
self.url = url
self.api = api
- def Send(self):
+ def send(self):
root = self._get_xml()
request_text = etree.tostring(root)
@@ -49,7 +49,7 @@ def Send(self):
data = '%s=%s' % (self.api, quote(request_text))
request = Request(full_url, data)
response_text = urlopen(request).read()
- response = self.__ParseResponse(response_text)
+ response = self.__parse_response(response_text)
except URLError, e:
if hasattr(e, 'reason'):
print 'Could not reach the server, reason: %s' % e.reason
@@ -59,7 +59,7 @@ def Send(self):
return response
- def __ParseResponse(self, response_text):
+ def __parse_response(self, response_text):
"""Parses the text from an Endicia web service call"""
root = etree.fromstring(response_text)
namespace = re.search('{(.*)}', root.tag).group(1)
@@ -67,12 +67,12 @@ def __ParseResponse(self, response_text):
status = int(root.findtext(status_path))
response = None
if status != 0:
- response = error(status, root, namespace)
+ response = Error(status, root, namespace)
else:
response = self._parse_response_body(root, namespace)
return response
-class error(object):
+class Error(object):
def __init__(self, status, root, namespace):
self.status = status
error_path = '{%s}ErrorMessage' % namespace
@@ -81,11 +81,11 @@ def __init__(self, status, root, namespace):
def __repr__(self):
return 'Endicia error %d: %s' % (self.status, self.message)
-class label_request(request):
+class LabelRequest(EndiciaRequest):
def __init__(self, partner_id, account_id, passphrase, package, shipper, recipient):
url = u'GetPostageLabelXML'
api = u'labelRequestXML'
- super(label_request, self).__init__(url, api)
+ super(LabelRequest, self).__init__(url, api)
self.partner_id = partner_id
self.account_id = account_id
@@ -96,7 +96,7 @@ def __init__(self, partner_id, account_id, passphrase, package, shipper, recipie
self.recipient = recipient
def _parse_response_body(self, root, namespace):
- return label_response(root, namespace)
+ return LabelResponse(root, namespace)
def _get_xml(self):
root = etree.Element('LabelRequest')
@@ -144,7 +144,7 @@ def __add_address(self, address, type, root):
element_key = '%s%s' % (type, key)
etree.SubElement(root, element_key).text = value
-class label_response(object):
+class LabelResponse(object):
def __init__(self, root, namespace):
self.root = root
self.tracking = root.findtext('{%s}TrackingNumber' % namespace)
@@ -155,12 +155,12 @@ def __init__(self, root, namespace):
def __repr__(self):
return 'Tracking: %s, cost: $%s' % (self.tracking, self.postage)
-class recredit_request(request):
+class RecreditRequest(EndiciaRequest):
def __init__(self, partner_id, account_id, passphrase, amount):
self.debug = True
url = u'BuyPostageXML'
api = u'recreditRequestXML'
- super(recredit_request, self).__init__(url, api)
+ super(RecreditRequest, self).__init__(url, api)
self.partner_id = partner_id
self.account_id = account_id
@@ -169,7 +169,7 @@ def __init__(self, partner_id, account_id, passphrase, amount):
self.amount = str(amount)
def _parse_response_body(self, root, namespace):
- return recredit_response(root, namespace)
+ return RecreditResponse(root, namespace)
def _get_xml(self):
root = etree.Element('RecreditRequest')
@@ -184,7 +184,7 @@ def _get_xml(self):
return root
-class recredit_response(object):
+class RecreditResponse(object):
def __init__(self, root, namespace):
self.root = root
self.account_status = root.findtext('{%s}CertifiedIntermediary/{%s}AccountStatus' % (namespace, namespace))
@@ -194,12 +194,12 @@ def __init__(self, root, namespace):
def __repr__(self):
return 'Status: %s, Balance: $%s, Total Printed: $%s' % (self.account_status, self.postage_balance, self.postage_printed)
-class change_password_request(request):
+class ChangePasswordRequest(EndiciaRequest):
def __init__(self, partner_id, account_id, passphrase, new_passphrase):
self.debug = True
url = u'ChangePassPhraseXML'
api = u'changePassPhraseRequestXML'
- super(change_password_request, self).__init__(url, api)
+ super(ChangePasswordRequest, self).__init__(url, api)
self.partner_id = partner_id
self.account_id = account_id
@@ -208,7 +208,7 @@ def __init__(self, partner_id, account_id, passphrase, new_passphrase):
self.new_passphrase = new_passphrase
def _parse_response_body(self, root, namespace):
- return change_password_response(root, namespace)
+ return ChangePasswordResponse(root, namespace)
def _get_xml(self):
root = etree.Element('ChangePassPhraseRequest')
@@ -223,7 +223,7 @@ def _get_xml(self):
return root
-class change_password_response(object):
+class ChangePasswordResponse(object):
def __init__(self, root, namespace):
self.root = root
self.status = root.findtext('{%s}Status' % namespace)
@@ -231,12 +231,12 @@ def __init__(self, root, namespace):
def __repr__(self):
return 'Password Change: %s' % ('OK' if int(self.status) == 0 else 'Error')
-class rate_request(request):
+class RateRequest(EndiciaRequest):
def __init__(self, partner_id, account_id, passphrase, package, shipper, recipient):
self.debug = True
url = u'CalculatePostageRateXML'
api = u'postageRateRequestXML'
- super(rate_request, self).__init__(url, api)
+ super(RateRequest, self).__init__(url, api)
self.partner_id = partner_id
self.account_id = account_id
@@ -247,7 +247,7 @@ def __init__(self, partner_id, account_id, passphrase, package, shipper, recipie
self.recipient = recipient
def _parse_response_body(self, root, namespace):
- return rate_response(root, namespace)
+ return RateResponse(root, namespace)
def _get_xml(self):
root = etree.Element('PostageRateRequest')
@@ -270,9 +270,7 @@ def _get_xml(self):
return root
- return root
-
-class rate_response(object):
+class RateResponse(object):
def __init__(self, root, namespace):
self.root = root
self.postage_price = root.find('{%s}PostagePrice' % namespace).get('TotalAmount')
@@ -280,19 +278,19 @@ def __init__(self, root, namespace):
def __repr__(self):
return 'Estimated Cost: $%s' % self.postage_price
-class account_status_request(request):
+class AccountStatusRequest(EndiciaRequest):
def __init__(self, partner_id, account_id, passphrase):
self.debug = True
url = u'GetAccountStatusXML'
api = u'accountStatusRequestXML'
- super(account_status_request, self).__init__(url, api)
+ super(AccountStatusRequest, self).__init__(url, api)
self.partner_id = partner_id
self.account_id = account_id
self.passphrase = passphrase
def _parse_response_body(self, root, namespace):
- return account_status_response(root, namespace)
+ return AccountStatusResponse(root, namespace)
def _get_xml(self):
root = etree.Element('AccountStatusRequest')
@@ -305,7 +303,7 @@ def _get_xml(self):
return root
-class account_status_response(object):
+class AccountStatusResponse(object):
def __init__(self, root, namespace):
self.root = root
self.account_status = root.findtext('{%s}CertifiedIntermediary/{%s}AccountStatus' % (namespace, namespace))
View
6 endicia/__init__.py
@@ -1,6 +0,0 @@
-from endicia import package, error
-from endicia import label_request, label_response
-from endicia import rate_request, rate_response
-from endicia import account_status_request, account_status_response
-from endicia import recredit_request, recredit_response
-from endicia import change_password_request, change_password_response
View
30 shipping.py
@@ -0,0 +1,30 @@
+def indent(elem, level=0):
+ """Indents an etree element so printing that element is actually human-readable"""
+ i = "\n" + level*" "
+ if len(elem):
+ if not elem.text or not elem.text.strip():
+ elem.text = i + " "
+ if not elem.tail or not elem.tail.strip():
+ elem.tail = i
+ for elem in elem:
+ indent(elem, level+1)
+ if not elem.tail or not elem.tail.strip():
+ elem.tail = i
+ else:
+ if level and (not elem.tail or not elem.tail.strip()):
+ elem.tail = i
+
+def debug_print_tree(elem):
+ indent(elem)
+ etree.dump(elem)
+
+class Address(object):
+ def __init__(self, name, address, city, state, zip, country, address2='', phone=''):
+ self.name = name
+ self.address1 = address
+ self.address2 = address2
+ self.city = city
+ self.state = state
+ self.zip = str(zip)
+ self.country = country
+ self.phone = phone
View
36 test.py
@@ -7,7 +7,7 @@
raise
import os, tempfile
-from common import address
+from shipping_common import Address
import UPS
import USPS
import endicia
@@ -69,11 +69,11 @@ def TestUSPS():
TestUSPSExpressMail()
def TestEndiciaLabel():
- package = endicia.package(15, endicia.package.shapes[0], 10, 10, 10)
- shipper = address('Adobe', "345 Park Avenue", 'San Jose', 'CA', 95110, 'US')
- recipient = address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'US')
- request = endicia.label_request(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient)
- response = request.Send()
+ package = endicia.Package(15, endicia.Package.shapes[0], 10, 10, 10)
+ shipper = Address('Adobe', "345 Park Avenue", 'San Jose', 'CA', 95110, 'US')
+ recipient = Address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'US')
+ request = endicia.LabelRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient)
+ response = request.send()
print response
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_file:
@@ -81,29 +81,29 @@ def TestEndiciaLabel():
os.system('open %s' % temp_file.name)
def TestEndiciaRate():
- shipper = address('Adobe', "345 Park Avenue", 'San Jose', 'CA', 95110, 'US')
- recipient = address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'US')
- for shape in endicia.package.shapes:
- package = endicia.package(15, shape, 12, 12, 12)
- request = endicia.rate_request(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient)
- response = request.Send()
+ shipper = Address('Adobe', "345 Park Avenue", 'San Jose', 'CA', 95110, 'US')
+ recipient = Address('Apple', "1 Infinite Loop", 'Cupertino', 'CA', 95014, 'US')
+ for shape in endicia.Package.shapes:
+ package = endicia.Package(15, shape, 12, 12, 12)
+ request = endicia.RateRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, package, shipper, recipient)
+ response = request.send()
print response
def TestAccountStatus():
- request = endicia.account_status_request(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase)
- response = request.Send()
+ request = endicia.AccountStatusRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase)
+ response = request.send()
print response
def TestEndiciaRecredit():
- request = endicia.recredit_request(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 200)
- response = request.Send()
+ request = endicia.RecreditRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 200)
+ response = request.send()
print response
def TestEndiciaChangePassword():
- request = endicia.change_password_request(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 'NewPassword')
- response = request.Send()
+ request = endicia.ChangePasswordRequest(EndiciaPartnerID, EndiciaAccountID, EndiciaPassphrase, 'NewPassword')
+ response = request.send()
print response
Please sign in to comment.
Something went wrong with that request. Please try again.