diff --git a/authorizenet/apicontrollersbase.py b/authorizenet/apicontrollersbase.py index bab6e0f..11fc29d 100644 --- a/authorizenet/apicontrollersbase.py +++ b/authorizenet/apicontrollersbase.py @@ -7,6 +7,7 @@ import logging import pyxb import sys +import threading import xml.dom.minidom import requests from lxml import objectify @@ -75,6 +76,7 @@ class APIOperationBase(APIOperationBaseInterface): __metaclass__ = abc.ABCMeta __initialized = False + __environment_lock = threading.Lock() __merchantauthentication = "null" __environment = "null" @@ -118,7 +120,12 @@ def getprettyxmlrequest(self): def execute(self): - self.endpoint = APIOperationBase.__environment + # Read environment at execute time with lock to ensure consistency + with APIOperationBase.__environment_lock: + # Initialize class environment to SANDBOX if not already set + if APIOperationBase.__environment == "null": + APIOperationBase.__environment = constants.SANDBOX + self.endpoint = APIOperationBase.__environment anetLogger.debug('Executing http post to url: %s', self.endpoint) @@ -193,8 +200,8 @@ def beforeexecute(self): return @staticmethod - def getmerchantauthentication(self): - return self.__merchantauthentication + def getmerchantauthentication(): + return APIOperationBase.__merchantauthentication @staticmethod def setmerchantauthentication(merchantauthentication): @@ -204,20 +211,22 @@ def setmerchantauthentication(merchantauthentication): def validateandsetmerchantauthentication(self): anetapirequest = apicontractsv1.ANetApiRequest() if (anetapirequest.merchantAuthentication == "null"): - if (self.getmerchantauthentication() != "null"): - anetapirequest.merchantAuthentication = self.getmerchantauthentication() + if (APIOperationBase.getmerchantauthentication() != "null"): + anetapirequest.merchantAuthentication = APIOperationBase.getmerchantauthentication() else: raise ValueError('Merchant Authentication can not be null') return @staticmethod - def getenvironment(self): - return APIOperationBase.__environment + def getenvironment(): + with APIOperationBase.__environment_lock: + return APIOperationBase.__environment @staticmethod def setenvironment(userenvironment): - APIOperationBase.__environment = userenvironment + with APIOperationBase.__environment_lock: + APIOperationBase.__environment = userenvironment return def __init__(self, apiRequest): @@ -233,9 +242,8 @@ def __init__(self, apiRequest): raise ValueError('Input request cannot be null') self._request = apiRequest - __merchantauthentication = apicontractsv1.merchantAuthenticationType() - APIOperationBase.__environment = constants.SANDBOX + __merchantauthentication = apicontractsv1.merchantAuthenticationType() APIOperationBase.setmerchantauthentication(__merchantauthentication) self.validate()