1
1
'''
2
- Created on Jul 6 , 2015
2
+ Created on Nov 1 , 2015
3
3
4
- @author: egodolja
4
+ @author: krgupta
5
5
'''
6
6
import abc
7
7
import logging
8
8
import os
9
+ from os .path import expanduser
10
+
9
11
import sys
10
12
import xml .dom .minidom
11
13
12
14
from pip ._vendor import requests
13
15
from _pyio import __metaclass__
14
16
from ConfigParser import SafeConfigParser
17
+ import ConfigParser
15
18
16
19
from authorizenet .constants import constants
17
20
from authorizenet import apicontractsv1
@@ -34,27 +37,27 @@ def getresponseclass(self):
34
37
pass
35
38
36
39
@abc .abstractmethod
37
- def getResponse (self ):
40
+ def getresponse (self ):
38
41
''' Returns the de-serialized response'''
39
42
pass
40
43
41
44
@abc .abstractmethod
42
- def getResultCode (self ):
45
+ def getresultcode (self ):
43
46
''' Returns the result code from the response '''
44
47
pass
45
48
46
49
@abc .abstractmethod
47
- def getMessageType (self ):
50
+ def getmessagetype (self ):
48
51
''' Returns the message type enum from the response '''
49
52
pass
50
53
51
54
@abc .abstractmethod
52
- def afterExecute (self ):
53
- '''TODO '''
55
+ def afterexecute (self ):
56
+ '''Returns the message received from binding after processing request '''
54
57
pass
55
58
56
59
@abc .abstractmethod
57
- def beforeExecute (self ):
60
+ def beforeexecute (self ):
58
61
'''TODO'''
59
62
pass
60
63
@@ -63,16 +66,28 @@ class APIOperationBase(APIOperationBaseInterface):
63
66
64
67
parser = SafeConfigParser ({"http" :"" ,"https" :"" ,"ftp" :"" })
65
68
69
+
66
70
try :
67
- #if #TODO
68
- parser .read (os .path .dirname (__file__ ) + "/../properties.ini" )
71
+ #if #TODO
72
+ home = os .path .expanduser ("~" )
73
+ homedirpropertiesfilename = os .path .join (home , "anet_python_sdk_properties.ini" )
74
+
75
+ currdir = os .getcwd ()
76
+ currdirpropertiesfilename = os .path .join (currdir , "anet_python_sdk_properties.ini" )
77
+
78
+ if (os .path .exists (homedirpropertiesfilename )):
79
+ parser .read (homedirpropertiesfilename )
80
+ elif (os .path .exists (currdirpropertiesfilename )):
81
+ parser .read (currdirpropertiesfilename )
82
+ else :
83
+ print "you do not have anet_python_sdk_properties.ini file neither in home nor in current working directory"
69
84
except IOError , error :
70
85
sys .exit ( error )
71
86
else :
72
87
logFile = parser .get ("properties" , "logfilename" )
73
88
#TODO format and level in config file
74
89
logging .basicConfig (filename = logFile , level = logging .DEBUG , format = '%(asctime)s %(message)s' )
75
- endpoint = parser . get ( "properties" , "sandbox" )
90
+ endpoint = constants . SANDBOX_TESTMODE
76
91
77
92
@abc .abstractmethod
78
93
def validaterequest (self ):
@@ -94,10 +109,10 @@ def validate(self):
94
109
self .validaterequest ()
95
110
return
96
111
97
- def _getRequest (self ): #protected method
112
+ def _getrequest (self ): #protected method
98
113
return self ._request
99
114
100
- def buildRequest (self ):
115
+ def buildrequest (self ):
101
116
logging .debug ('building request..' )
102
117
#TODO requestType = type( self._request)
103
118
requestType = self ._requestType
@@ -109,8 +124,8 @@ def buildRequest(self):
109
124
110
125
return xmlRequest
111
126
112
- def getPrettyXmlRequest (self ):
113
- xmlRequest = self .buildRequest ()
127
+ def getprettyxmlrequest (self ):
128
+ xmlRequest = self .buildrequest ()
114
129
requestDom = xml .dom .minidom .parseString (xmlRequest )
115
130
logging .debug ('Request is: %s' % requestDom .toprettyxml ())
116
131
@@ -119,26 +134,26 @@ def getPrettyXmlRequest(self):
119
134
def execute (self ):
120
135
logging .debug ('Executing http post to url: %s' , self .endpoint )
121
136
122
- self .beforeExecute ()
137
+ self .beforeexecute ()
123
138
124
139
proxyDictionary = {'http' : self .parser .get ("properties" , "http" ),
125
140
'https' : self .parser .get ("properties" , "https" ),
126
141
'ftp' : self .parser .get ("properties" , "ftp" )}
127
142
128
143
#requests is http request
129
144
try :
130
- xmlRequest = self .buildRequest ()
145
+ xmlRequest = self .buildrequest ()
131
146
self ._httpResponse = requests .post (self .endpoint , data = xmlRequest , headers = constants .headers , proxies = proxyDictionary )
132
147
except Exception as httpException :
133
- logging .error ( 'Error retrieving http response from: %s for request: %s' , self .endpoint , self .getPrettyXmlRequest ())
148
+ logging .error ( 'Error retrieving http response from: %s for request: %s' , self .endpoint , self .getprettyxmlrequest ())
134
149
logging .error ( 'Exception: %s, %s' , type (httpException ), httpException .args )
135
150
136
151
137
152
if self ._httpResponse :
138
153
#encoding of response should be changed to retrieve text of response
139
154
self ._httpResponse .encoding = constants .response_encoding
140
155
self ._httpResponse = self ._httpResponse .text [3 :] #strip BOM
141
- self .afterExecute ()
156
+ self .afterexecute ()
142
157
try :
143
158
self ._response = apicontractsv1 .CreateFromDocument (self ._httpResponse )
144
159
except Exception as createfromdocumentexception :
@@ -156,21 +171,21 @@ def execute(self):
156
171
else :
157
172
print "Did not receive http response"
158
173
159
- def getResponse (self ):
174
+ def getresponse (self ):
160
175
return self ._response
161
176
162
- def getResultCode (self ):
177
+ def getresultcode (self ):
163
178
if self ._response :
164
179
return self ._response .resultCode
165
180
166
- def getMessageType (self ):
181
+ def getmessagetype (self ):
167
182
if self ._response :
168
183
return self ._response .message
169
184
170
- def afterExecute (self ):
185
+ def afterexecute (self ):
171
186
return
172
187
173
- def beforeExecute (self ):
188
+ def beforeexecute (self ):
174
189
return
175
190
176
191
def __init__ (self , apiRequest , requestType ):
0 commit comments