5
5
'''
6
6
import abc
7
7
import logging
8
- import os
9
- from os .path import expanduser
10
-
11
- import sys
12
8
import xml .dom .minidom
13
-
9
+ import ConfigParser
10
+ from ConfigParser import SafeConfigParser
14
11
from pip ._vendor import requests
15
12
from _pyio import __metaclass__
16
- from ConfigParser import SafeConfigParser
17
- import ConfigParser
18
13
19
14
from authorizenet .constants import constants
20
15
from authorizenet import apicontractsv1
21
-
16
+ from authorizenet import utility
17
+ '''
18
+ from authorizenet.apicontractsv1 import merchantAuthenticationType
19
+ from authorizenet.apicontractsv1 import ANetApiRequest
20
+ from authorizenet.apicontractsv1 import ANetApiResponse
21
+ '''
22
22
class APIOperationBaseInterface (object ):
23
23
24
24
__metaclass__ = abc .ABCMeta
@@ -68,49 +68,38 @@ def beforeexecute(self):
68
68
69
69
class APIOperationBase (APIOperationBaseInterface ):
70
70
__metaclass__ = abc .ABCMeta
71
-
72
- parser = SafeConfigParser ({"http" :"" ,"https" :"" ,"ftp" :"" })
73
-
74
-
75
- try :
76
- home = os .path .expanduser ("~" )
77
- homedirpropertiesfilename = os .path .join (home , "anet_python_sdk_properties.ini" )
78
-
79
- currdir = os .getcwd ()
80
- currdirpropertiesfilename = os .path .join (currdir , "anet_python_sdk_properties.ini" )
81
71
82
- if (os .path .exists (homedirpropertiesfilename )):
83
- parser .read (homedirpropertiesfilename )
84
- elif (os .path .exists (currdirpropertiesfilename )):
85
- parser .read (currdirpropertiesfilename )
86
- else :
87
- print "you do not have anet_python_sdk_properties.ini file neither in home nor in current working directory"
88
- except IOError , error :
89
- sys .exit ( error )
90
- else :
91
- logFile = parser .get ("properties" , "logfilename" )
92
- #TODO format and level in config file
93
- logging .basicConfig (filename = logFile , level = logging .DEBUG , format = '%(asctime)s %(message)s' )
94
- endpoint = constants .SANDBOX_TESTMODE
72
+ __initialized = "False"
73
+ __merchantauthentication = "null"
74
+
75
+ @staticmethod
76
+ def __classinitialized ():
77
+ return APIOperationBase .__initialized
95
78
96
79
@abc .abstractmethod
97
80
def validaterequest (self ):
98
81
return
99
82
100
83
def validate (self ):
101
- #add validation on merchant authentication #TODO
102
- #if ( "null" != authorizenet.apicontractsv1.merchantAuthenticationType.sessionToken)
103
- # raise ValueError("SessionToken needs to be null")
104
- '''
105
- if ( "null" != apicontractsv1.merchantAuthenticationType.__password.value)
106
- raise ValueError('Password needs to be null')
107
- if ( null != merchantAuthenticationType.getMobileDeviceId())
108
- throw new IllegalArgumentException("MobileDeviceId needs to be null");
109
- ImpersonationAuthenticationType impersonationAuthenticationType = merchantAuthenticationType.getImpersonationAuthentication();
110
- if ( null != impersonationAuthenticationType)
111
- throw new IllegalArgumentException("ImpersonationAuthenticationType needs to be null");
84
+ anetapirequest = self ._getrequest ()
85
+
86
+ #self.validateandsetmerchantauthentication()
112
87
'''
113
- self .validaterequest ()
88
+ # make sure proper authentication elements are present and no extra elements are present
89
+ merchantauthenticationtype = anetapirequest.merchantauthentication()
90
+ if (merchantauthenticationtype.sessionToken != "null"):
91
+ raise ValueError('sessionToken needs to be null')
92
+ if (merchantauthenticationtype.password != "null"):
93
+ raise ValueError('Password needs to be null')
94
+ if (merchantauthenticationtype.mobileDeviceId != "null"):
95
+ raise ValueError('MobileDeviceId needs to be null')
96
+
97
+ impersonationauthenticationtype = merchantauthenticationtype.impersonationAuthentication
98
+ if (impersonationauthenticationtype != "null"):
99
+ raise ValueError('ImpersonationAuthenticationType needs to be null')
100
+ '''
101
+ self .validaterequest ()
102
+
114
103
return
115
104
116
105
def _getrequest (self ): #protected method
@@ -134,15 +123,17 @@ def getprettyxmlrequest(self):
134
123
return requestDom
135
124
136
125
def execute (self ):
126
+ self .endpoint = constants .SANDBOX_TESTMODE
137
127
logging .debug ('Executing http post to url: %s' , self .endpoint )
138
128
139
129
self .beforeexecute ()
140
-
141
- proxyDictionary = {'http' : self .parser .get ("properties" , "http" ),
142
- 'https' : self .parser .get ("properties" , "https" ),
143
- 'ftp' : self .parser .get ("properties" , "ftp" )}
144
130
131
+ proxyDictionary = {'http' : utility .helper .getproperty ("http" ),
132
+ 'https' : utility .helper .getproperty ("https" ),
133
+ 'ftp' : utility .helper .getproperty ("ftp" )}
134
+
145
135
#requests is http request
136
+
146
137
try :
147
138
xmlRequest = self .buildrequest ()
148
139
self ._httpResponse = requests .post (self .endpoint , data = xmlRequest , headers = constants .headers , proxies = proxyDictionary )
@@ -172,24 +163,47 @@ def execute(self):
172
163
logging .debug ('Error retrieving response for request: %s' % self ._request )
173
164
else :
174
165
print "Did not receive http response"
166
+ return
175
167
176
168
def getresponse (self ):
177
169
return self ._response
178
170
179
171
def getresultcode (self ):
172
+ resultcode = 'null'
180
173
if self ._response :
181
- return self ._response .resultCode
174
+ resultcode = self ._response .resultCode
175
+ return resultcode
182
176
183
177
def getmessagetype (self ):
178
+ message = 'null'
184
179
if self ._response :
185
- return self ._response .message
180
+ message = self ._response .message
181
+ return message
186
182
187
183
def afterexecute (self ):
188
184
return
189
185
190
186
def beforeexecute (self ):
191
187
return
192
-
188
+
189
+ @staticmethod
190
+ def getmerchantauthentication (self ):
191
+ return self .__merchantauthentication
192
+
193
+ @staticmethod
194
+ def setmerchantauthentication (merchantauthentication ):
195
+ APIOperationBase .__merchantauthentication = merchantauthentication
196
+ return
197
+
198
+ def validateandsetmerchantauthentication (self ):
199
+ anetapirequest = apicontractsv1 .ANetApiRequest ()
200
+ if (anetapirequest .getmerchantauthentication () == "null" ):
201
+ if (self .getmerchantauthentication () != "null" ):
202
+ anetapirequest .setmerchantauthentication (self .getmerchantauthentication ())
203
+ else :
204
+ raise ValueError ('Merchant Authentication can not be null' )
205
+ return
206
+
193
207
def __init__ (self , apiRequest ):
194
208
self ._httpResponse = "null"
195
209
self ._request = "null"
@@ -198,9 +212,27 @@ def __init__(self, apiRequest):
198
212
199
213
if "null" == apiRequest :
200
214
raise ValueError ('Input request cannot be null' )
201
- #TOdo null check for types
202
-
215
+
203
216
self ._request = apiRequest
217
+ __merchantauthentication = apicontractsv1 .merchantAuthenticationType ()
218
+
219
+ APIOperationBase .setmerchantauthentication (__merchantauthentication )
220
+
221
+ if ( 'False' == APIOperationBase .__classinitialized ()):
222
+ loggingfilename = utility .helper .getpropertyfile ()
223
+ logginglevel = utility .helper .getproperty ("executionlogginglevel" )
224
+
225
+ #print ("logging level %s" %logginglevel)
226
+
227
+
228
+ if ("null" == loggingfilename ):
229
+ loggingfilename = constants .defaultLogFileName
230
+ if ("null" == logginglevel ):
231
+ logginglevel = constants .defaultLoggingLevel
232
+
233
+ logging .basicConfig (filename = loggingfilename , level = logginglevel , format = constants .defaultlogformat )
234
+ __initialized = "True"
235
+
204
236
self .validate ()
205
237
206
238
return
0 commit comments