4
4
library, it is not recommened and could possible result in some strange
5
5
behavior.
6
6
"""
7
+ import base64
7
8
import copy
8
9
import time
9
10
import locale
@@ -78,7 +79,8 @@ class SessionEngine(Singleton):
78
79
_valid_methods = tuple ()
79
80
uri_root = '/'
80
81
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 ):
82
84
"""Initialize a Dynect Rest Session object and store the provided
83
85
credentials
84
86
@@ -87,6 +89,10 @@ def __init__(self, host=None, port=443, ssl=True, history=False):
87
89
:param ssl: Enable SSL
88
90
:param history: A boolean flag determining whether or not you would
89
91
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
90
96
:return: SessionEngine object
91
97
"""
92
98
super (SessionEngine , self ).__init__ ()
@@ -96,6 +102,10 @@ def __init__(self, host=None, port=443, ssl=True, history=False):
96
102
self .host = host
97
103
self .port = port
98
104
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
99
109
self .poll_incomplete = True
100
110
self .content_type = 'application/json'
101
111
self ._encoding = locale .getdefaultlocale ()[- 1 ] or 'UTF-8'
@@ -148,7 +158,8 @@ def name(self):
148
158
149
159
def connect (self ):
150
160
"""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.
152
163
"""
153
164
if self ._token :
154
165
self .logger .debug ('Forcing logout from old session' )
@@ -158,17 +169,48 @@ def connect(self):
158
169
self .poll_incomplete = orig_value
159
170
self ._token = None
160
171
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 )
166
203
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 )
172
214
173
215
def _process_response (self , response , method , final = False ):
174
216
"""API Method. Process an API response for failure, incomplete, or
0 commit comments