Last active
August 29, 2015 14:10
-
-
Save gregpinero/95469bd5f3b027457a08 to your computer and use it in GitHub Desktop.
Simple SOAP Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""""" | |
Sample script using Lyons ABA web service methods | |
"""""" | |
import httplib | |
import urllib | |
import xml.dom.minidom | |
#-------SETTINGS------- | |
HOST = 'www.lyonsreg.com' | |
URL = '/webservices/aba/ABAService.asmx' | |
httplib.HTTPConnection.debuglevel = 0 | |
#---------------------- | |
def str_to_bool(val): | |
""""""Convenience function"""""" | |
val=str(val).lower() | |
assert val in ['false','true'] | |
return bool(['false','true'].index(val)) | |
def SOAP_post(SOAPAction,xml): | |
""""""Handles making the SOAP request"""""" | |
h = httplib.HTTPConnection(HOST) | |
headers={ | |
'Host':HOST, | |
'Content-Type':'text/xml; charset=utf-8', | |
'Content-Length':len(xml), | |
'SOAPAction':'""%s""' % SOAPAction, | |
} | |
h.request ('POST', URL, body=xml,headers=headers) | |
r = h.getresponse() | |
d = r.read() | |
if r.status!=200: | |
raise ValueError('Error connecting: %s, %s' % (r.status, r.reason)) | |
return d | |
def logon(companyID,username,password): | |
""""""First call logon to get your token"""""" | |
in_xml=""""""\ | |
<?xml version=""1.0"" encoding=""utf-8""?> | |
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> | |
<soap:Body> | |
<Logon xmlns=""http://www.lyonsreg.com/WebService/ABAService""> | |
<companyID>%s</companyID> | |
<userName>%s</userName> | |
<password>%s</password> | |
</Logon> | |
</soap:Body> | |
</soap:Envelope>"""""" % (companyID,username,password) | |
result_xml=SOAP_post(""http://www.lyonsreg.com/WebService/ABAService/Logon"",in_xml) | |
doc=xml.dom.minidom.parseString(result_xml) | |
response=doc.getElementsByTagName('LogonResult')[0].firstChild.data | |
return response | |
def logoff(token): | |
""""""I guess call this when you're done"""""" | |
in_xml=""""""\ | |
<?xml version=""1.0"" encoding=""utf-8""?> | |
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> | |
<soap:Body> | |
<Logoff xmlns=""http://www.lyonsreg.com/WebService/ABAService""> | |
<token>%s</token> | |
</Logoff> | |
</soap:Body> | |
</soap:Envelope>"""""" % token | |
result_xml=SOAP_post(""http://www.lyonsreg.com/WebService/ABAService/Logoff"",in_xml) | |
return True | |
def required_logon(token): | |
""""""Use this to know if you need to login again"""""" | |
in_xml=""""""\ | |
<?xml version=""1.0"" encoding=""utf-8""?> | |
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> | |
<soap:Body> | |
<RequiredLogon xmlns=""http://www.lyonsreg.com/WebService/ABAService""> | |
<token>%s</token> | |
</RequiredLogon> | |
</soap:Body> | |
</soap:Envelope>"""""" % token | |
result_xml=SOAP_post(""http://www.lyonsreg.com/WebService/ABAService/RequiredLogon"",in_xml) | |
doc=xml.dom.minidom.parseString(result_xml) | |
response=doc.getElementsByTagName('RequiredLogonResult')[0].firstChild.data | |
return str_to_bool(response) | |
def bool_verify_ABA(token,aba): | |
""""""Lookup ABA number on Lyons Server and return if found."""""" | |
in_xml=""""""\ | |
<?xml version=""1.0"" encoding=""utf-8""?> | |
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> | |
<soap:Body> | |
<VerifyABA xmlns=""http://www.lyonsreg.com/WebService/ABAService""> | |
<token>%s</token> | |
<aba>%s</aba> | |
</VerifyABA> | |
</soap:Body> | |
</soap:Envelope>"""""" % (token,aba) | |
result_xml=SOAP_post(""http://www.lyonsreg.com/WebService/ABAService/VerifyABA"",in_xml) | |
doc=xml.dom.minidom.parseString(result_xml) | |
response=doc.getElementsByTagName('VerifyABAResult')[0].firstChild.data | |
return str_to_bool(response) | |
if __name__=='__main__': | |
#Quick Tests | |
token=logon('113','user','pw') | |
print token | |
assert required_logon(token)==False | |
logoff(token) | |
assert required_logon(token)==True | |
#print bool_verify_ABA(token,'greg') | |
token=logon('113','user','pw') | |
print token | |
assert bool_verify_ABA(token,'greg')==False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment