-------------------------------------------------------------------------------------
"""
Con-way XML Interface Sample Code for Python - Bill of Lading
Requires:
ElementTree 1.X - http://effbot.org/downloads
expat (get PyXML from http://pyxml.sourceforge.net)
-----------------
TODO:
* replace the USERNAME and PASSWORD String values in the ConwayXml class
with your Con-way username and password
-----------------
Send questions to our Web Customer Support at web.support@Con-way.com
"""
import string, httplib, urllib, base64, sys, pprint
from elementtree.ElementTree import ElementTree
from datetime import date
class HTTPRequest:
"""Helper class that abstracts HTTP request process"""
def __init__(self,host,path,username,password):
self.host = host
self.path = path
self.username = username
self.password = password
def getBasicAuth(self):
"""Return basic authentication username password"""
s = self.username +':'+ self.password
z = base64.encodestring(s)[:-1] # strip trailing \12
return 'Basic '+z
def encodeParams(self,d={}):
"""Given a dict of key values, return encoded params"""
return urllib.urlencode(d)
def getPostBody(self):
"""return the body of the post, implement in subclass"""
raise NotImplementedError
def Process(self):
"""Make a rate quote request"""
body = self.getPostBody()
h = httplib.HTTP(self.host)
h.putrequest('POST',self.path)
h.putheader("Content-type","application/x-www-form-urlencoded")
h.putheader('Content-length',"%d" % len(body))
h.putheader('Authorization',self.getBasicAuth())
h.putheader('Accept','*/*')
h.putheader('Host',self.host)
h.endheaders()
h.send(body)
reply, msg, hdrs = h.getreply()
if reply != 200:
raise RuntimeError('HTTP request error',(reply,msg,hdrs))
return self.DecodeResponseFile(h.getfile())
def DecodeResponse(self,f):
"""Decode response data from file object f"""
raise NotImplementedError
class I:
"""A helper class containing various attributes"""
def __init__(self,**kw):
self.__dict__.update(kw)
def valueOf(self):
"""make printing prettier"""
prettyDict = {}
for k,v in self.__dict__.items():
if isinstance(v,I):
v = v.valueOf()
prettyDict[k] = v
return prettyDict
def __str__(self):
import pprint
return pprint.pformat(self.valueOf())
class ConwayXml(HTTPRequest):
"""Submit the XML request and return its detailed information"""
# replace the USERNAME and PASSWORD String values with your Con-way username and password:
USERNAME='USERNAME'
PASSWORD='PASSWORD'
HOST='www.Con-way.com'
PATH='/XMLj/X-BOL'
def __init__(self):
# initialize base class
HTTPRequest.__init__(self,self.HOST,self.PATH,self.USERNAME,self.PASSWORD)
def getIt(self):
"""Process the request"""
return self.Process()
def getPostBody(self):
# return the body of the post
# construct the request XML, send URL-Form-encoded instead of plain XML
now = date.today()
formattedDate = now.strftime("%m/%d/%y")
res = []
# Add the XML stream here
res.append('')
res.append('' + self.USERNAME + '')
res.append('P')
res.append('')
res.append('')
res.append(' 3338889')
res.append(' 3338890')
res.append(' 3213A')
res.append(' 789283')
res.append('')
res.append('')
res.append(' Alan Shipley')
res.append(' 1234 NE Main')
res.append(' Portland')
res.append(' OR')
res.append(' 97202')
res.append(' 503.450.6055')
res.append(' web.support@Con-way.com')
res.append('')
res.append('')
res.append(' 883885')
res.append(' 503.450.6436')
res.append(' web.support@Con-way.com')
res.append('')
res.append('- ')
res.append(' 11 ')
res.append(' 789')
res.append(' widget-arms')
res.append(' 775')
res.append(' ')
res.append(' N')
res.append('
')
res.append('DID')
res.append('TEST TEST TEST')
res.append('')
res.append('')
res.append(' ' + formattedDate + '')
res.append(' 4:00 pm')
res.append(' 7:00 pm')
res.append(' Frank')
res.append(' Franklin Arms')
res.append(' (333)444-4321')
res.append('')
res.append('')
res.append('')
return self.encodeParams({'BOLrequest':"\n".join(res)})
def DecodeResponseFile(self,f):
"""Decode response data from file object f"""
tree = ElementTree(file=f) # decode xml return in file object 'f'
##tree.write(sys.stdout) # dump it to stdout for diagnostics
root = tree.getroot()
error = root.find('Error')
if error is not None:
raise RuntimeError((error.text,error.get('returncode'),error.get('responsecode')))
result = I()
# we iterate over the top level children to populate the result object
# we'll descend at most one level to get subelements of top level children
# Tracking results aren't nested more than this
for child in root.getchildren():
children = child.getchildren()
if children: # has children
placeholder = I()
for item in children:
setattr(placeholder,item.tag,item.text)
else:
placeholder = child.text
setattr(result,child.tag,placeholder)
return result
if __name__ == "__main__":
t = ConwayXml()
res = t.getIt()
pprint.pprint(res.valueOf())
-------------------------------------------------------------------------------------