Skip to content

Commit 81bad6c

Browse files
woochicaAleffio
authored andcommitted
Make Adyen.httpclient module Python 3 compatible (#25)
* Make urlencode import Python 2/3 compatible * Make urllib2 import Python 2/3 compatible * Make StringIO import Python 2/3 compatible * Add missing urlopen import
1 parent 98b2e49 commit 81bad6c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Adyen/httpclient.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@
1212
except ImportError:
1313
pycurl = None
1414

15-
import urllib2
15+
try:
16+
# Python 3
17+
from urllib.parse import urlencode
18+
from urllib.request import Request, urlopen
19+
from urllib.error import HTTPError
20+
except ImportError:
21+
# Python 2
22+
from urllib import urlencode
23+
from urllib2 import Request, urlopen, HTTPError
24+
25+
try:
26+
# Python 3
27+
from io import StringIO
28+
except ImportError:
29+
# Python 2
30+
from StringIO import StringIO
1631

17-
from urllib import urlencode
18-
from StringIO import StringIO
1932
import json as json_lib
2033
import re
2134
import base64
@@ -223,7 +236,7 @@ def _urllib_post(self, url,
223236
raw_store = json
224237

225238
raw_request = json_lib.dumps(json) if json else urlencode(data)
226-
url_request = urllib2.Request(url,data=raw_request)
239+
url_request = Request(url,data=raw_request)
227240
if json:
228241
url_request.add_header('Content-Type','application/json')
229242
elif not data:
@@ -248,8 +261,8 @@ def _urllib_post(self, url,
248261

249262
#URLlib raises all non 200 responses as en error.
250263
try:
251-
response = urllib2.urlopen(url_request, timeout=timeout)
252-
except urllib2.HTTPError as e:
264+
response = urlopen(url_request, timeout=timeout)
265+
except HTTPError as e:
253266
raw_response = e.read()
254267

255268
return raw_response, raw_request, e.getcode(), e.headers

0 commit comments

Comments
 (0)