Skip to content

Commit 51f1052

Browse files
Merge pull request #68 from Ishrock/patch-2
HTTPClient interface #65
2 parents f238b2f + f9eee0d commit 51f1052

File tree

4 files changed

+245
-15
lines changed

4 files changed

+245
-15
lines changed

Adyen/client.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ def call_api(self, request_data, service, action, idempotency=False,
222222

223223
# username at self object has highest priority. fallback to root module
224224
# and ensure that it is set.
225+
xapikey = None
225226
if self.xapikey:
226227
xapikey = self.xapikey
227228
elif 'xapikey' in kwargs:
228229
xapikey = kwargs.pop("xapikey")
229230

231+
username = None
230232
if self.username:
231233
username = self.username
232234
elif 'username' in kwargs:
@@ -237,15 +239,18 @@ def call_api(self, request_data, service, action, idempotency=False,
237239
username = self._store_payout_username(**kwargs)
238240
else:
239241
username = self._review_payout_username(**kwargs)
240-
if not username:
242+
243+
if not username and not xapikey:
241244
errorstring = """Please set your webservice username.
242245
You can do this by running
243246
'Adyen.username = 'Your username'"""
244247
raise AdyenInvalidRequestError(errorstring)
245248
# password at self object has highest priority.
246249
# fallback to root module
247250
# and ensure that it is set.
248-
if self.password:
251+
252+
password = None
253+
if self.password and not xapikey:
249254
password = self.password
250255
elif 'password' in kwargs:
251256
password = kwargs.pop("password")
@@ -255,7 +260,8 @@ def call_api(self, request_data, service, action, idempotency=False,
255260
password = self._store_payout_pass(**kwargs)
256261
else:
257262
password = self._review_payout_pass(**kwargs)
258-
if not password:
263+
264+
if not password and not xapikey:
259265
errorstring = """Please set your webservice password.
260266
You can do this by running
261267
'Adyen.password = 'Your password'"""

Adyen/httpclient.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def _pycurl_post(self,
6060
data=None,
6161
username="",
6262
password="",
63+
xapikey="",
6364
headers={},
6465
timeout=30):
6566
"""This function will POST to the url endpoint using pycurl. returning
@@ -77,6 +78,8 @@ def _pycurl_post(self,
7778
as part of password.
7879
password (str, optional): Password for basic auth. Must be included
7980
as part of username.
81+
xapikey (str, optional): Adyen API key. Will be used for auth
82+
if username and password are absent.
8083
headers (dict, optional): Key/Value pairs of headers to include
8184
timeout (int, optional): Default 30. Timeout for the request.
8285
@@ -102,6 +105,11 @@ def _pycurl_post(self,
102105
# request can be identified as coming from the Adyen Python library.
103106
headers['User-Agent'] = self.user_agent
104107

108+
if username and password:
109+
curl.setopt(curl.USERPWD, '%s:%s' % (username, password))
110+
elif xapikey:
111+
headers["X-API-KEY"] = xapikey
112+
105113
# Convert the header dict to formatted array as pycurl needs.
106114
if sys.version_info[0] >= 3:
107115
header_list = ["%s:%s" % (k, v) for k, v in headers.items()]
@@ -120,9 +128,6 @@ def _pycurl_post(self,
120128
raw_request = json_lib.dumps(json) if json else urlencode(data)
121129
curl.setopt(curl.POSTFIELDS, raw_request)
122130

123-
if username and password:
124-
curl.setopt(curl.USERPWD, '%s:%s' % (username, password))
125-
126131
curl.setopt(curl.TIMEOUT, timeout)
127132
curl.perform()
128133

@@ -143,7 +148,7 @@ def _requests_post(self, url,
143148
username="",
144149
password="",
145150
xapikey="",
146-
headers=None,
151+
headers={},
147152
timeout=30):
148153
"""This function will POST to the url endpoint using requests.
149154
Returning an AdyenResult object on 200 HTTP response.
@@ -160,6 +165,8 @@ def _requests_post(self, url,
160165
as part of password.
161166
password (str, optional): Password for basic auth. Must be included
162167
as part of username.
168+
xapikey (str, optional): Adyen API key. Will be used for auth
169+
if username and password are absent.
163170
headers (dict, optional): Key/Value pairs of headers to include
164171
timeout (int, optional): Default 30. Timeout for the request.
165172
@@ -169,8 +176,6 @@ def _requests_post(self, url,
169176
int: HTTP status code, eg 200,404,401
170177
dict: Key/Value pairs of the headers received.
171178
"""
172-
if headers is None:
173-
headers = {}
174179

175180
# Adding basic auth if username and password provided.
176181
auth = None
@@ -194,11 +199,12 @@ def _requests_post(self, url,
194199
return request.text, message, request.status_code, request.headers
195200

196201
def _urllib_post(self, url,
197-
json="",
198-
data="",
202+
json=None,
203+
data=None,
199204
username="",
200205
password="",
201-
headers=None,
206+
xapikey="",
207+
headers={},
202208
timeout=30):
203209

204210
"""This function will POST to the url endpoint using urllib2. returning
@@ -216,6 +222,8 @@ def _urllib_post(self, url,
216222
uncluded as part of password.
217223
password (str, optional): Password for basic auth. Must be
218224
included as part of username.
225+
xapikey (str, optional): Adyen API key. Will be used for auth
226+
if username and password are absent.
219227
headers (dict, optional): Key/Value pairs of headers to include
220228
timeout (int, optional): Default 30. Timeout for the request.
221229
@@ -225,8 +233,6 @@ def _urllib_post(self, url,
225233
int: HTTP status code, eg 200,404,401
226234
dict: Key/Value pairs of the headers received.
227235
"""
228-
if headers is None:
229-
headers = {}
230236

231237
# Store regular dict to return later:
232238
raw_store = json
@@ -258,6 +264,8 @@ def _urllib_post(self, url,
258264
replace('\n', '')
259265
url_request.add_header("Authorization",
260266
"Basic %s" % basic_authstring)
267+
elif xapikey:
268+
headers["X-API-KEY"] = xapikey
261269

262270
# Adding the headers to the request.
263271
for key, value in headers.items():
@@ -302,6 +310,8 @@ def request(self, url,
302310
uncluded as part of password.
303311
password (str, optional): Password for basic auth. Must be
304312
included as part of username.
313+
xapikey (str, optional): Adyen API key. Will be used for auth
314+
if username and password are absent.
305315
headers (dict, optional): Key/Value pairs of headers to include
306316
Returns:
307317
str: Raw request placed

test/BaseTest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import mock
1+
2+
try:
3+
import mock
4+
except Exception:
5+
from unittest import mock
26
import json
37
from Adyen import httpclient
48

0 commit comments

Comments
 (0)