Skip to content

Python3 compatibility #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ language: python
sudo: false

python:
- "2.7"
# - "pypy" Disabling pypy until travis moves to newer version, known issue with lxml crashing pypy
- 3.5
- 3.4
- 2.7

install:
- pip install unittest2
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ createtransactioncontroller.execute()
response = createtransactioncontroller.getresponse()

if (response.messages.resultCode=="Ok"):
print "Transaction ID : %s" % response.transactionResponse.transId
print("Transaction ID : {}".format(response.transactionResponse.transId))
else:
print "response code: %s" % response.messages.resultCode
print("Response code: {}".format(response.messages.resultCode))

````
### Setting Production or Sandbox Environments
Expand Down
17 changes: 8 additions & 9 deletions authorizenet/apicontrollersbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys
import xml.dom.minidom
from pip._vendor import requests
from _pyio import __metaclass__
from lxml import objectify

from authorizenet.constants import constants
Expand Down Expand Up @@ -97,8 +96,8 @@ def buildrequest(self):

xmlRequest = self._request.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype())
#remove namespaces that toxml() generates
xmlRequest = xmlRequest.replace(constants.nsNamespace1, '')
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '')
xmlRequest = xmlRequest.replace(constants.nsNamespace1, b'')
xmlRequest = xmlRequest.replace(constants.nsNamespace2, b'')

return xmlRequest

Expand Down Expand Up @@ -138,8 +137,8 @@ def execute(self):
self._response = apicontractsv1.CreateFromDocument(self._httpResponse)
#objectify code
xmlResponse= self._response.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype())
xmlResponse = xmlResponse.replace(constants.nsNamespace1, '')
xmlResponse = xmlResponse.replace(constants.nsNamespace2, '')
xmlResponse = xmlResponse.replace(constants.nsNamespace1, b'')
xmlResponse = xmlResponse.replace(constants.nsNamespace2, b'')
self._mainObject = objectify.fromstring(xmlResponse)

except Exception as objectifyexception:
Expand All @@ -148,21 +147,21 @@ def execute(self):
self._response = apicontractsv1.CreateFromDocument(self._httpResponse)
#objectify code
xmlResponse= self._response.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype())
xmlResponse = xmlResponse.replace(constants.nsNamespace1, '')
xmlResponse = xmlResponse.replace(constants.nsNamespace2, '')
xmlResponse = xmlResponse.replace(constants.nsNamespace1, b'')
xmlResponse = xmlResponse.replace(constants.nsNamespace2, b'')
self._mainObject = objectify.fromstring(xmlResponse)
else:
#if type(self.getresponseclass()) == type(self._response):
if type(self.getresponseclass()) != type(self._mainObject):
if self._response.messages.resultCode == "Error":
print "Response error"
logging.debug("Response error")
domResponse = xml.dom.minidom.parseString(self._httpResponse)
logging.debug('Received response: %s' % domResponse.toprettyxml())
else:
#Need to handle ErrorResponse
logging.debug('Error retrieving response for request: %s' % self._request)
else:
print "Did not receive http response"
logging.debug("Did not receive http response")
return

def getresponse(self):
Expand Down
4 changes: 2 additions & 2 deletions authorizenet/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class constants(object):
note = ' note="Status with a capital \'S\' is obsolete."'

'''ns namespace 1'''
nsNamespace1 = 'ns1:'
nsNamespace1 = b'ns1:'

'''ns namespace 2'''
nsNamespace2 = ':ns1'
nsNamespace2 = b':ns1'

'''default log file name'''
defaultLogFileName = "anetsdk.log"
Expand Down
19 changes: 13 additions & 6 deletions authorizenet/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

@author: krgupta
'''
try:
from ConfigParser import SafeConfigParser
from ConfigParser import NoSectionError
except ImportError:
from configparser import SafeConfigParser
from configparser import NoSectionError

from ConfigParser import SafeConfigParser
from ConfigParser import NoSectionError
import logging
import os
import sys
#from __future__ import print_function

logger = logging.getLogger(__name__)


class helper():
__parser = "null"
Expand Down Expand Up @@ -45,20 +52,20 @@ def getproperty(propertyname):
try:
helper.__parser = SafeConfigParser({"http":"","https":"","ftp":""})
except:
print ("Parser could not be initialized")
logger.debug("Parser could not be initialized")

if ('null' != helper.getparser()):
try:
helper.getparser().read(helper.__propertyfilename)
helper.__initialized = True
except:
print ("Unable to load the property file")
logger.debug("Unable to load the property file")

if (True == helper.__classinitialized()):
try:
stringvalue = helper.getparser().get("properties", propertyname)
except:
sys.stderr.write("'%s' not found\n" %propertyname )
logger.exception("'{}' not found".format(propertyname))

if ( "null" == stringvalue):
stringvalue = os.getenv(propertyname)
Expand Down
6 changes: 5 additions & 1 deletion tests/apitestbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import random
import test

from ConfigParser import SafeConfigParser
try:
from ConfigParser import SafeConfigParser
except ImportError:
from configparser import SafeConfigParser

from authorizenet import apicontractsv1, apicontrollersbase
from authorizenet.utility import *
#from authorizenet.apicontractsv1 import CTD_ANON
Expand Down
36 changes: 5 additions & 31 deletions tests/testpyxb.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def __PyxbDeserialization(self, lastElement = None):
try:
xmlRequest = createtransactionrequest.toxml(encoding=constants.xml_encoding, element_name='createTransactionRequest')
xmlRequest = xmlRequest.replace(constants.nsNamespace1, '')
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '')
##print ("xmlRequest %s " %xmlRequest)
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Xml Request: %s" % xmlRequest)
except Exception as ex:
logging.debug( "Xml Exception: %s" % ex)
Expand All @@ -72,17 +71,14 @@ def __PyxbDeserialization(self, lastElement = None):
self.assertIsNotNone(deserializedObject, "Null deserializedObject ")

if type(createtransactionrequest) == type(deserializedObject):
##print (" for good xml objects are equal")
logging.debug( "createtransactionrequest object is equal to deserializedObject")
else:
##print ("for good xml some error: objects are NOT equal" )
logging.debug( "createtransactionrequest object is NOT equal to deserializedObject")

deseriaziedObjectXmlRequest = deserializedObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject')
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace1, '')
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Good Dom Request: %s " % deseriaziedObjectXmlRequest )
##print ( "Good De-serialized XML: %s \n" % deseriaziedObjectXmlRequest )
except Exception as ex:
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args )
else:
Expand All @@ -93,9 +89,7 @@ def __PyxbDeserialization(self, lastElement = None):
badXmlElement = "<badElement>BadElement</badElement>"
badXmlRequest = lines[0] + badXmlElement + splitString + lines[1]
logging.debug( "Bad XmlRequest: %s" % badXmlRequest)
##print ("ElementInMidXML Request: %s \n" %badXmlRequest)
except Exception as ex:
##print ("ElementInMidXML can not be inserted: %s, %s",type(ex), ex.args)
logging.debug( "ElementInMidXML can not be inserted: %s, %s" ,type(ex), ex.args)
if (lastElement == True):
try:
Expand All @@ -104,9 +98,7 @@ def __PyxbDeserialization(self, lastElement = None):
badXmlElementAtLast = "<badElementAtLast>BadElementAtLast</badElementAtLast>"
badXmlRequest = lines[0] + badXmlElementAtLast + splitStringAtLast + lines[1]
logging.debug( "Bad XmlRequest at Last: %s" % badXmlRequest)
##print ("ElementAtLastXML Request: %s \n" %badXmlRequest)
except Exception as ex:
##print ("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args)
logging.debug("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args)
try:
deserializedBadObject = apicontractsv1.CreateFromDocument(badXmlRequest)
Expand All @@ -115,11 +107,10 @@ def __PyxbDeserialization(self, lastElement = None):
badDomXml = badDomXml.replace(constants.nsNamespace1, '')
badDomXml = badDomXml.replace(constants.nsNamespace2, '')
logging.debug( "Bad Dom Request: %s " % badDomXml )
##print ("Bad Dom De-serialized: %s \n" %badDomXml)
except Exception as ex:
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args )
##print ("Exception while de-serializing bad dom: %s, %s",type(ex), ex.args)


class test_CustomerProfile(unittest.TestCase):
def testGetCustomerProfile(self):
loggingfilename = utility.helper.getproperty(constants.propertiesloggingfilename)
Expand All @@ -144,7 +135,6 @@ def testGetCustomerProfile(self):
xmlRequest = xmlRequest.replace(constants.nsNamespace1, '')
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Xml Request: %s" % xmlRequest)
#print( "Xml Request: %s" % xmlRequest)
except Exception as ex:
logging.debug( "Xml Exception: %s" % ex)

Expand All @@ -155,29 +145,23 @@ def testGetCustomerProfile(self):
self.assertIsNotNone(deserializedObject, "Null deserializedObject ")

if type(getCustomerProfileRequest) == type(deserializedObject):
#print ("objects are equal")
logging.debug( "createtransactionrequest object is equal to deserializedObject")
else:
#print ("some error: objects are NOT equal" )
logging.debug( "createtransactionrequest object is NOT equal to deserializedObject")

deseriaziedObjectXmlRequest = deserializedObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject')
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace1, '')
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Good Dom Request: %s " % deseriaziedObjectXmlRequest )
#print( "Good Dom Request: %s " % deseriaziedObjectXmlRequest )
#print("de-serialized successfully. GOOD CASE COMPLETE \n ")
except Exception as ex:

logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args )

self.assertEquals(type(getCustomerProfileRequest), type(deserializedObject), "deseriaziedObject does not match original object")

try:
#print("starting with element in mid")
newxml = '<?xml version="1.0" encoding="utf-8"?><getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"><merchantAuthentication><name>unknown</name><transactionKey>anon</transactionKey></merchantAuthentication><kriti>11Jan</kriti><customerProfileId>36152115</customerProfileId></getCustomerProfileRequest>'

#print ("newxml: %s" %newxml)

DEserializedNEWObject = apicontractsv1.CreateFromDocument(newxml)
self.assertIsNotNone(DEserializedNEWObject, "Null deserializedObject ")

Expand All @@ -186,29 +170,19 @@ def testGetCustomerProfile(self):
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace1, '')
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Good Dom Request: %s " % DEseriaziedNEWObjectXmlRequest )
#print( " DEseriaziedNEWObjectXmlRequest Request: %s " % DEseriaziedNEWObjectXmlRequest )
#print("de-serialized successfully")
#print("FINISHED element in mid \n ")
except Exception as ex:
#print("DEseriaziedNEWObjectXmlRequest is NOT DESerialized")
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args )


try:
#print("starting with element at last")
newxmlATLAst = '<?xml version="1.0" encoding="utf-8"?><getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"><merchantAuthentication><name>unknown</name><transactionKey>anon</transactionKey></merchantAuthentication><customerProfileId>36152115</customerProfileId><gupta>11Jan</gupta></getCustomerProfileRequest>'
#print ("newxmlATLAst: %s" %newxmlATLAst)
DEserializedNEWObject = apicontractsv1.CreateFromDocument(newxmlATLAst)
self.assertIsNotNone(DEserializedNEWObject, "Null deserializedObject ")
DEseriaziedNEWObjectXmlRequest = DEserializedNEWObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject')
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace1, '')
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace2, '')
logging.debug( "Good Dom Request: %s " % DEseriaziedNEWObjectXmlRequest )
#print( " DEseriaziedNEWATLASTObjectXmlRequest Request: %s " % DEseriaziedNEWObjectXmlRequest )
#print("de-serialized successfully")
#print("Finished element at last \n " )
except Exception as ex:
#print("DEseriaziedNEWATLASTObjectXmlRequest is NOT DESerialized")
except Exception as ex:
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args )

if __name__ =='__main__':
Expand Down