Skip to content

Commit 9dfe728

Browse files
thaelathyAleffio
authored andcommitted
deleted unused, rephrased somethin (#50)
* deleted unused, rephrased somethin * style * regex must be raw string * Turned back to BytesIO
1 parent 91b02d8 commit 9dfe728

File tree

4 files changed

+98
-131
lines changed

4 files changed

+98
-131
lines changed

Adyen/client.py

Lines changed: 58 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
AdyenAPIValidationError,
1515
AdyenInvalidRequestError,
1616
AdyenAPIInvalidFormat,
17-
AdyenAPIInvalidAmount)
17+
AdyenAPIInvalidAmount,
18+
)
1819
from . import settings
1920

2021

@@ -68,7 +69,7 @@ class AdyenClient(object):
6869
def __init__(self, username=None, password=None,
6970
review_payout_username=None, review_payout_password=None,
7071
store_payout_username=None, store_payout_password=None,
71-
platform=None, merchant_account=None,
72+
platform="test", merchant_account=None,
7273
merchant_specific_url=None, skin_code=None,
7374
hmac=None, app_name=None,
7475
http_force=None):
@@ -79,8 +80,6 @@ def __init__(self, username=None, password=None,
7980
self.store_payout_username = store_payout_username
8081
self.store_payout_password = store_payout_password
8182
self.platform = platform
82-
if not self.platform:
83-
self.platform = 'test'
8483
self.merchant_specific_url = merchant_specific_url
8584
self.hmac = hmac
8685
self.merchant_account = merchant_account
@@ -124,55 +123,43 @@ def _determine_hpp_url(self, platform, action):
124123

125124
def _review_payout_username(self, **kwargs):
126125
if 'username' in kwargs:
127-
review_payout_username = kwargs['username']
126+
return kwargs['username']
128127
elif self.review_payout_username:
129-
review_payout_username = self.review_payout_username
130-
else:
131-
errorstring = """AdyenInvalidRequestError: Please set your review payout
132-
webservice username. You can do this by running
133-
'Adyen.review_payout_username = 'Your payout username' """
134-
raise AdyenInvalidRequestError(errorstring)
135-
136-
return review_payout_username
128+
return self.review_payout_username
129+
errorstring = """Please set your review payout
130+
webservice username. You can do this by running
131+
'Adyen.review_payout_username = 'Your payout username' """
132+
raise AdyenInvalidRequestError(errorstring)
137133

138134
def _review_payout_pass(self, **kwargs):
139135
if 'password' in kwargs:
140-
review_payout_password = kwargs["password"]
136+
return kwargs["password"]
141137
elif self.review_payout_password:
142-
review_payout_password = self.review_payout_password
143-
else:
144-
errorstring = """AdyenInvalidRequestError: Please set your review payout
145-
webservice password. You can do this by running
146-
'Adyen.review_payout_password = 'Your payout password'"""
147-
raise AdyenInvalidRequestError(errorstring)
148-
149-
return review_payout_password
138+
return self.review_payout_password
139+
errorstring = """Please set your review payout
140+
webservice password. You can do this by running
141+
'Adyen.review_payout_password = 'Your payout password'"""
142+
raise AdyenInvalidRequestError(errorstring)
150143

151144
def _store_payout_username(self, **kwargs):
152145
if 'username' in kwargs:
153-
store_payout_username = kwargs['username']
146+
return kwargs['username']
154147
elif self.store_payout_username:
155-
store_payout_username = self.store_payout_username
156-
else:
157-
errorstring = """AdyenInvalidRequestError: Please set your store payout
158-
webservice username. You can do this by running
159-
'Adyen.store_payout_username = 'Your payout username'"""
160-
raise AdyenInvalidRequestError(errorstring)
161-
162-
return store_payout_username
148+
return self.store_payout_username
149+
errorstring = """Please set your store payout
150+
webservice username. You can do this by running
151+
'Adyen.store_payout_username = 'Your payout username'"""
152+
raise AdyenInvalidRequestError(errorstring)
163153

164154
def _store_payout_pass(self, **kwargs):
165155
if 'password' in kwargs:
166-
store_payout_password = kwargs["password"]
156+
return kwargs["password"]
167157
elif self.store_payout_password:
168-
store_payout_password = self.store_payout_password
169-
else:
170-
errorstring = """AdyenInvalidRequestError: Please set your store payout
171-
webservice password. You can do this by running
172-
'Adyen.store_payout_password = 'Your payout password'"""
173-
raise AdyenInvalidRequestError(errorstring)
174-
175-
return store_payout_password
158+
return self.store_payout_password
159+
errorstring = """Please set your store payout
160+
webservice password. You can do this by running
161+
'Adyen.store_payout_password = 'Your payout password'"""
162+
raise AdyenInvalidRequestError(errorstring)
176163

177164
def call_api(self, request_data, service, action, idempotency=False,
178165
**kwargs):
@@ -203,62 +190,53 @@ def call_api(self, request_data, service, action, idempotency=False,
203190

204191
# username at self object has highest priority. fallback to root module
205192
# and ensure that it is set.
206-
if 'username' in kwargs:
207-
username = kwargs["username"]
193+
if self.username:
194+
username = self.username
195+
elif 'username' in kwargs:
196+
username = kwargs.pop("username")
208197
elif service == "Payout":
209198
if any(substring in action for substring in ["store", "submit"]):
210199
username = self._store_payout_username(**kwargs)
211200
else:
212201
username = self._review_payout_username(**kwargs)
213-
elif self.username:
214-
username = self.username
215202
if not username:
216-
errorstring = """AdyenInvalidRequestError: Please set your webservice username."
203+
errorstring = """Please set your webservice username.
217204
You can do this by running 'Adyen.username = 'Your username'"""
218205
raise AdyenInvalidRequestError(errorstring)
219-
# Ensure that username has been removed so as not to be passed to adyen
220-
if 'username' in kwargs:
221-
del kwargs['username']
222206

223207
# password at self object has highest priority. fallback to root module
224208
# and ensure that it is set.
225-
if 'password' in kwargs:
226-
password = kwargs["password"]
209+
if self.password:
210+
password = self.password
211+
elif 'password' in kwargs:
212+
password = kwargs.pop("password")
227213
elif service == "Payout":
228214
if any(substring in action for substring in ["store", "submit"]):
229215
password = self._store_payout_pass(**kwargs)
230216
else:
231217
password = self._review_payout_pass(**kwargs)
232-
elif self.password:
233-
password = self.password
234218
if not password:
235-
errorstring = """AdyenInvalidRequestError: Please set your webservice password.
219+
errorstring = """Please set your webservice password.
236220
You can do this by running 'Adyen.password = 'Your password'"""
237221
raise AdyenInvalidRequestError(errorstring)
238-
# Ensure that password has been removed so as not to be passed to adyen
239-
if 'password' in kwargs:
240-
del kwargs["password"]
241222

242223
# platform at self object has highest priority. fallback to root module
243224
# and ensure that it is set to either 'live' or 'test'.
244-
if 'platform' in kwargs:
245-
platform = kwargs['platform']
246-
del kwargs['platform']
247-
elif self.platform:
225+
if self.platform:
248226
platform = self.platform
227+
elif 'platform' in kwargs:
228+
platform = kwargs.pop('platform')
249229

250-
if platform.lower() not in ['live', 'test']:
251-
errorstring = "'platform' must be the value of 'live' or 'test'"
252-
raise ValueError(errorstring)
253-
elif not isinstance(platform, str):
230+
if not isinstance(platform, str):
254231
errorstring = "'platform' value must be type of string"
255232
raise TypeError(errorstring)
233+
elif platform.lower() not in ['live', 'test']:
234+
errorstring = "'platform' must be the value of 'live' or 'test'"
235+
raise ValueError(errorstring)
256236

257237
message = request_data
258238

259-
if 'merchantAccount' not in message:
260-
message['merchantAccount'] = self.merchant_account
261-
if message['merchantAccount'] == "":
239+
if not message.get('merchantAccount'):
262240
message['merchantAccount'] = self.merchant_account
263241

264242
# Adyen requires this header to be set and uses the combination of
@@ -314,7 +292,8 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
314292
if self.hmac:
315293
hmac = self.hmac
316294
elif not hmac:
317-
errorstring = """Please set an hmac with your Adyen.Adyen class instance.
295+
errorstring = """Please set an hmac with your Adyen.Adyen
296+
class instance.
318297
'Adyen.hmac = \"!WR#F@...\"' or as an additional
319298
parameter in the function call ie.
320299
'Adyen.hpp.directory_lookup(hmac=\"!WR#F@...\"'. Please reach
@@ -323,14 +302,13 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
323302

324303
# platform provided in self has highest priority,
325304
# fallback to root module and ensure that it is set.
326-
if self.platform:
327-
platform = self.platform
328-
if platform.lower() not in ['live', 'test']:
329-
errorstring = " 'platform' must be the value of 'live' or 'test' "
330-
raise ValueError(errorstring)
331-
elif not isinstance(platform, str):
305+
platform = self.platform
306+
if not isinstance(platform, str):
332307
errorstring = "'platform' must be type string"
333308
raise TypeError(errorstring)
309+
elif platform.lower() not in ['live', 'test']:
310+
errorstring = " 'platform' must be the value of 'live' or 'test' "
311+
raise ValueError(errorstring)
334312

335313
if 'skinCode' not in message:
336314
message['skinCode'] = self.skin_code
@@ -363,12 +341,12 @@ def hpp_payment(self, request_data, action, hmac_key="", **kwargs):
363341
self.http_init = True
364342

365343
platform = self.platform
366-
if platform.lower() not in ['live', 'test']:
367-
errorstring = " 'platform' must be the value of 'live' or 'test' "
368-
raise ValueError(errorstring)
369-
elif not isinstance(platform, str):
344+
if not isinstance(platform, str):
370345
errorstring = "'platform' must be type string"
371346
raise TypeError(errorstring)
347+
elif platform.lower() not in ['live', 'test']:
348+
errorstring = " 'platform' must be the value of 'live' or 'test' "
349+
raise ValueError(errorstring)
372350

373351
if 'skinCode' not in request_data:
374352
request_data['skinCode'] = self.skin_code
@@ -444,14 +422,11 @@ def _handle_response(self, url, raw_response, raw_request,
444422
reference = message.get("reference",
445423
message.get("merchantReference"))
446424

447-
errorstring = """AdyenInvalidRequestError: Unable to retrieve payment "
425+
errorstring = """Unable to retrieve payment "
448426
list. Received the error: {}. Please verify your request "
449427
and try again. If the issue persists, please reach out to "
450428
support@adyen.com including the "
451429
merchantReference: {}""".format(error, reference),
452-
raw_request = message,
453-
raw_response = raw_response,
454-
url = url
455430

456431
raise AdyenInvalidRequestError(errorstring)
457432

@@ -568,6 +543,6 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
568543

569544
def _error_from_hpp(self, html):
570545
# Must be updated when Adyen response is changed:
571-
match_obj = re.search('>Error:\s*(.*?)<br', html)
546+
match_obj = re.search(r'>Error:\s*(.*?)<br', html)
572547
if match_obj:
573548
return match_obj.group(1)

Adyen/exceptions.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ def __init__(self,
1919
self.status_code = status_code
2020

2121
def __str__(self):
22-
return repr(self.message)
22+
return repr("{}:{}".format(self.__class__.__name__, self.message))
2323

2424
def debug(self):
25-
return "message: {}\nHTTP status_code:{}\nurl: {}\nrequest:" \
26-
" {}\nresponse: {}\nheaders: {}".format(
27-
self.message,
28-
self.status_code,
29-
self.url,
30-
self.raw_request,
31-
self.raw_response,
32-
self.headers)
25+
return ("class: {}\nmessage: {}\nHTTP status_code:{}\nurl: {}"
26+
"request: {}\nresponse: {}\nheaders: {}".format(
27+
self.__class__.__name__,
28+
self.message,
29+
self.status_code,
30+
self.url,
31+
self.raw_request,
32+
self.raw_response,
33+
self.headers
34+
)
35+
)
3336

3437

3538
class AdyenInvalidRequestError(AdyenError):
@@ -47,9 +50,6 @@ def __init__(self,
4750
self.error_code = error_code
4851
self.result = result
4952

50-
def __str__(self):
51-
return repr(self.message)
52-
5353

5454
class AdyenAPIAuthenticationError(AdyenAPIResponseError):
5555
pass

Adyen/httpclient.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,13 @@ def _pycurl_post(self,
8989

9090
response_headers = {}
9191

92-
def handle_header(header_line):
93-
header_line = header_line.decode('iso-8859-1')
94-
if ':' in header_line:
95-
name, value = header_line.split(':', 1)
96-
name = name.strip()
97-
value = value.strip()
98-
response_headers[name] = value
99-
10092
curl = pycurl.Curl()
10193
curl.setopt(curl.URL, url)
102-
10394
if sys.version_info[0] >= 3:
10495
stringbuffer = BytesIO()
10596
else:
10697
stringbuffer = StringIO()
107-
# stringbuffer = StringIO()
98+
10899
curl.setopt(curl.WRITEDATA, stringbuffer)
109100

110101
# Add User-Agent header to request so that the
@@ -151,7 +142,7 @@ def _requests_post(self, url,
151142
data=None,
152143
username="",
153144
password="",
154-
headers={},
145+
headers=None,
155146
timeout=30):
156147
"""This function will POST to the url endpoint using requests.
157148
Returning an AdyenResult object on 200 HTTP response.
@@ -177,13 +168,13 @@ def _requests_post(self, url,
177168
int: HTTP status code, eg 200,404,401
178169
dict: Key/Value pairs of the headers received.
179170
"""
171+
if headers is None:
172+
headers = {}
180173

181174
# Adding basic auth if username and password provided.
182-
auth = ""
175+
auth = None
183176
if username and password:
184177
auth = requests.auth.HTTPBasicAuth(username, password)
185-
else:
186-
auth = None
187178

188179
# Add User-Agent header to request so that the request
189180
# can be identified as coming from the Adyen Python library.
@@ -204,7 +195,7 @@ def _urllib_post(self, url,
204195
data="",
205196
username="",
206197
password="",
207-
headers={},
198+
headers=None,
208199
timeout=30):
209200

210201
"""This function will POST to the url endpoint using urllib2. returning
@@ -231,6 +222,8 @@ def _urllib_post(self, url,
231222
int: HTTP status code, eg 200,404,401
232223
dict: Key/Value pairs of the headers received.
233224
"""
225+
if headers is None:
226+
headers = {}
234227

235228
# Store regular dict to return later:
236229
raw_store = json
@@ -288,7 +281,7 @@ def request(self, url,
288281
data="",
289282
username="",
290283
password="",
291-
headers={},
284+
headers=None,
292285
timout=30):
293286
"""This is overridden on module initialization. This function will make
294287
an HTTP POST to a given url. Either json/data will be what is posted to

0 commit comments

Comments
 (0)