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
4041# ['raw_response','raw_request','status_code','headers'])
4142
4243class HTTPClient (object ):
43- def __init__ (self ,app_name ,USER_AGENT_SUFFIX ,LIB_VERSION , force_request = None ):
44+ def __init__ (self ,app_name ,USER_AGENT_SUFFIX ,LIB_VERSION ,force_request = None ):
4445 #Check if requests already available, default to urllib
4546 # self.app_name = app_name
4647 # self.LIB_VERSION = LIB_VERSION
4748 # self.USER_AGENT_SUFFIX = USER_AGENT_SUFFIX
4849 self .user_agent = app_name + " " + USER_AGENT_SUFFIX + LIB_VERSION
49-
5050 if not force_request :
5151 if requests :
5252 self .request = self ._requests_post
@@ -107,15 +107,22 @@ def handle_header(header_line):
107107 curl = pycurl .Curl ()
108108 curl .setopt (curl .URL , url )
109109
110- stringbuffer = StringIO ()
110+ if sys .version_info [0 ] >= 3 :
111+ stringbuffer = BytesIO ()
112+ else :
113+ stringbuffer = StringIO ()
114+ #stringbuffer = StringIO()
111115 curl .setopt (curl .WRITEDATA , stringbuffer )
112116
113117 # Add User-Agent header to request so that the request can be identified as coming
114118 # from the Adyen Python library.
115119 headers ['User-Agent' ] = self .user_agent
116120
117121 # Convert the header dict to formatted array as pycurl needs.
118- header_list = ["%s:%s" % (k ,v ) for k ,v in headers .iteritems ()]
122+ if sys .version_info [0 ] >= 3 :
123+ header_list = ["%s:%s" % (k , v ) for k , v in headers .items ()]
124+ else :
125+ header_list = ["%s:%s" % (k , v ) for k , v in headers .iteritems ()]
119126 #Ensure proper content-type when adding headers
120127 if json :
121128 header_list .append ("Content-Type:application/json" )
@@ -236,7 +243,7 @@ def _urllib_post(self, url,
236243 raw_store = json
237244
238245 raw_request = json_lib .dumps (json ) if json else urlencode (data )
239- url_request = Request (url ,data = raw_request )
246+ url_request = Request (url ,data = raw_request . encode ( 'utf8' ) )
240247 if json :
241248 url_request .add_header ('Content-Type' ,'application/json' )
242249 elif not data :
@@ -251,8 +258,12 @@ def _urllib_post(self, url,
251258
252259 #Adding basic auth is username and password provided.
253260 if username and password :
254- basicAuthstring = base64 .encodestring ('%s:%s' % (username ,
255- password )).replace ('\n ' , '' )
261+ if sys .version_info [0 ] >= 3 :
262+ basicAuthstring = base64 .encodebytes (('%s:%s' % (username ,
263+ password )).encode ()).decode ().replace ('\n ' , '' )
264+ else :
265+ basicAuthstring = base64 .encodestring ('%s:%s' % (username ,
266+ password )).replace ('\n ' , '' )
256267 url_request .add_header ("Authorization" , "Basic %s" % basicAuthstring )
257268
258269 #Adding the headers to the request.
0 commit comments