44library, it is not recommened and could possible result in some strange
55behavior.
66"""
7+ import base64
78import copy
89import time
910import locale
@@ -78,7 +79,8 @@ class SessionEngine(Singleton):
7879 _valid_methods = tuple ()
7980 uri_root = '/'
8081
81- def __init__ (self , host = None , port = 443 , ssl = True , history = False ):
82+ def __init__ (self , host = None , port = 443 , ssl = True , history = False ,
83+ proxy_host = None , proxy_port = None , proxy_user = None , proxy_pass = None ):
8284 """Initialize a Dynect Rest Session object and store the provided
8385 credentials
8486
@@ -87,6 +89,10 @@ def __init__(self, host=None, port=443, ssl=True, history=False):
8789 :param ssl: Enable SSL
8890 :param history: A boolean flag determining whether or not you would
8991 like to store a record of all API calls made to review later
92+ :param proxy_host: A proxy host to utilize
93+ :param proxy_port: The port that the proxy is served on
94+ :param proxy_user: A username to connect to the proxy with if required
95+ :param proxy_pass: A password to connect to the proxy with if required
9096 :return: SessionEngine object
9197 """
9298 super (SessionEngine , self ).__init__ ()
@@ -96,6 +102,10 @@ def __init__(self, host=None, port=443, ssl=True, history=False):
96102 self .host = host
97103 self .port = port
98104 self .ssl = ssl
105+ self .proxy_host = proxy_host
106+ self .proxy_port = proxy_port
107+ self .proxy_user = proxy_user
108+ self .proxy_pass = proxy_pass
99109 self .poll_incomplete = True
100110 self .content_type = 'application/json'
101111 self ._encoding = locale .getdefaultlocale ()[- 1 ] or 'UTF-8'
@@ -148,7 +158,8 @@ def name(self):
148158
149159 def connect (self ):
150160 """Establishes a connection to the REST API server as defined by the
151- host, port and ssl instance variables
161+ host, port and ssl instance variables. If a proxy is specified, it
162+ is used.
152163 """
153164 if self ._token :
154165 self .logger .debug ('Forcing logout from old session' )
@@ -158,17 +169,48 @@ def connect(self):
158169 self .poll_incomplete = orig_value
159170 self ._token = None
160171 self ._conn = None
161- if self .ssl :
162- msg = 'Establishing SSL connection to {}:{}' .format (self .host ,
163- self .port )
164- self .logger .info (msg )
165- self ._conn = HTTPSConnection (self .host , self .port , timeout = 300 )
172+ use_proxy = False
173+ headers = {}
174+
175+ if self .proxy_host and not self .proxy_port :
176+ msg = 'Proxy missing port, please specify a port'
177+ raise ValueError (msg )
178+
179+ if self .proxy_host and self .proxy_port :
180+ use_proxy = True
181+
182+ if self .proxy_user and self .proxy_pass :
183+ auth = '{}:{}' .format (self .proxy_user , self .proxy_pass )
184+ headers ['Proxy-Authorization' ] = 'Basic ' + base64 .b64encode (auth )
185+
186+ if use_proxy :
187+ if self .ssl :
188+ msg = 'Establishing SSL connection to {}:{} with proxy {}:{}' .format (self .host ,
189+ self .port ,
190+ self .proxy_host ,
191+ self .proxy_port )
192+ self .logger .info (msg )
193+ self ._conn = HTTPSConnection (self .proxy_host , self .proxy_port , timeout = 300 )
194+ self ._conn .set_tunnel (self .host , self .port , headers )
195+ else :
196+ msg = 'Establishing unencrypted connection to {}:{} with proxy {}:{}' .format (self .host ,
197+ self .port ,
198+ self .proxy_host ,
199+ self .proxy_port )
200+ self .logger .info (msg )
201+ self ._conn = HTTPConnection (self .proxy_host , self .proxy_port , timeout = 300 )
202+ self ._conn .set_tunnel (self .host , self .port , headers )
166203 else :
167- msg = \
168- 'Establishing unencrypted connection to {}:{}' .format (self .host ,
169- self .port )
170- self .logger .info (msg )
171- self ._conn = HTTPConnection (self .host , self .port , timeout = 300 )
204+ if self .ssl :
205+ msg = 'Establishing SSL connection to {}:{}' .format (self .host ,
206+ self .port )
207+ self .logger .info (msg )
208+ self ._conn = HTTPSConnection (self .host , self .port , timeout = 300 )
209+ else :
210+ msg = 'Establishing unencrypted connection to {}:{}' .format (self .host ,
211+ self .port )
212+ self .logger .info (msg )
213+ self ._conn = HTTPConnection (self .host , self .port , timeout = 300 )
172214
173215 def _process_response (self , response , method , final = False ):
174216 """API Method. Process an API response for failure, incomplete, or
0 commit comments