-------------------------------------------------------------------------------------
"""
Python sample code to execute the Con-way XML Rating interface
Submit a Rate Request, decode the results and print them
Requires:
ElementTree 1.X - http://effbot.org/downloads
expat (get PyXML from http://pyxml.sourceforge.net)
Before Using:
Edit the userid and password properties in the ConwayXml Class
"""
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,userid,password):
self.host = host
self.path = path
self.userid = userid
self.password = password
def getBasicAuth(self):
"""Return basic authentication userid password"""
s = self.userid+':'+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):
"""Get a Con-way rate"""
# replace the USERID and PASSWORD String values with your Con-way username and password:
USERID='USERID'
PASSWORD='PASSWORD'
HOST='www.Con-way.com'
PATH='/XMLj/X-Rate'
def __init__(self):
# initialize base class
HTTPRequest.__init__(self,self.HOST,self.PATH,self.USERID,self.PASSWORD)
#self.pronumber = pro
def getIt(self):
"""get the Rate"""
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('97006')
res.append('33179')
res.append('P')
res.append('100')
res.append('' + formattedDate + '')
res.append('- ')
res.append('775')
res.append('667')
res.append('
')
res.append('- ')
res.append('100')
res.append('555 ')
res.append('
')
res.append('SSC')
res.append('DNC')
res.append('GUR')
res.append('')
return self.encodeParams({'RateRequest':"\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())
-------------------------------------------------------------------------------------