11#!/bin/python
22
33from __future__ import absolute_import , division , print_function , unicode_literals
4+ import sys
45
56try :
67 import requests
2324 from urllib2 import Request , urlopen , HTTPError
2425
2526try :
26- # Python 3
27- from io import StringIO
28- except ImportError :
2927 # Python 2
3028 from StringIO import StringIO
29+ except ImportError :
30+ # Python 3
31+ from io import BytesIO
3132
3233import json as json_lib
3334import re
@@ -107,15 +108,23 @@ def handle_header(header_line):
107108 curl = pycurl .Curl ()
108109 curl .setopt (curl .URL , url )
109110
110- stringbuffer = StringIO ()
111+ if sys .version_info [0 ] >= 3 :
112+ stringbuffer = BytesIO ()
113+ else :
114+ stringbuffer = StringIO ()
115+ #stringbuffer = StringIO()
111116 curl .setopt (curl .WRITEDATA , stringbuffer )
112117
113118 # Add User-Agent header to request so that the request can be identified as coming
114119 # from the Adyen Python library.
115120 headers ['User-Agent' ] = self .user_agent
116121
117122 # Convert the header dict to formatted array as pycurl needs.
118- header_list = ["%s:%s" % (k ,v ) for k ,v in headers .iteritems ()]
123+ # header_list = ["%s:%s" % (k,v) for k,v in headers.iteritems()]
124+ if sys .version_info [0 ] >= 3 :
125+ header_list = ["%s:%s" % (k , v ) for k , v in headers .items ()]
126+ else :
127+ header_list = ["%s:%s" % (k , v ) for k , v in headers .iteritems ()]
119128 #Ensure proper content-type when adding headers
120129 if json :
121130 header_list .append ("Content-Type:application/json" )
@@ -236,7 +245,7 @@ def _urllib_post(self, url,
236245 raw_store = json
237246
238247 raw_request = json_lib .dumps (json ) if json else urlencode (data )
239- url_request = Request (url ,data = raw_request )
248+ url_request = Request (url ,data = raw_request . encode ( 'utf8' ) )
240249 if json :
241250 url_request .add_header ('Content-Type' ,'application/json' )
242251 elif not data :
@@ -251,8 +260,12 @@ def _urllib_post(self, url,
251260
252261 #Adding basic auth is username and password provided.
253262 if username and password :
254- basicAuthstring = base64 .encodestring ('%s:%s' % (username ,
255- password )).replace ('\n ' , '' )
263+ if sys .version_info [0 ] >= 3 :
264+ basicAuthstring = base64 .encodebytes (('%s:%s' % (username ,
265+ password )).encode ()).decode ().replace ('\n ' , '' )
266+ else :
267+ basicAuthstring = base64 .encodestring ('%s:%s' % (username ,
268+ password )).replace ('\n ' , '' )
256269 url_request .add_header ("Authorization" , "Basic %s" % basicAuthstring )
257270
258271 #Adding the headers to the request.
0 commit comments