Skip to content

Commit a487185

Browse files
committed
Merge pull request #74 from drewrothstein/master
add proxy capability
2 parents e6bc9e4 + 984496f commit a487185

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

dyn/core.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
library, it is not recommened and could possible result in some strange
55
behavior.
66
"""
7+
import base64
78
import copy
89
import time
910
import 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

dyn/mm/session.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@ class MMSession(SessionEngine):
1919
_valid_methods = ('GET', 'POST')
2020
uri_root = '/rest/json'
2121

22-
def __init__(self, apikey, host='emailapi.dynect.net', port=443, ssl=True):
22+
def __init__(self, apikey, host='emailapi.dynect.net', port=443, ssl=True,
23+
proxy_host=None, proxy_port=None, proxy_user=None, proxy_pass=None):
2324
"""Initialize a Dynect Rest Session object and store the provided
2425
credentials
2526
2627
:param host: DynECT API server address
2728
:param port: Port to connect to DynECT API server
2829
:param ssl: Enable SSL
2930
:param apikey: your unique Email API key
31+
:param proxy_host: A proxy host to utilize
32+
:param proxy_port: The port that the proxy is served on
33+
:param proxy_user: A username to connect to the proxy with if required
34+
:param proxy_pass: A password to connect to the proxy with if required
3035
"""
31-
super(MMSession, self).__init__(host, port, ssl)
36+
super(MMSession, self).__init__(host, port, ssl, proxy_host, proxy_port,
37+
proxy_user, proxy_pass)
3238
self.apikey = apikey
3339
self.content_type = 'application/x-www-form-urlencoded'
3440
self._conn = None

dyn/tm/session.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class DynectSession(SessionEngine):
1919

2020
def __init__(self, customer, username, password, host='api.dynect.net',
2121
port=443, ssl=True, api_version='current', auto_auth=True,
22-
key=None, history=False):
22+
key=None, history=False, proxy_host=None, proxy_port=None,
23+
proxy_user=None, proxy_pass=None):
2324
"""Initialize a Dynect Rest Session object and store the provided
2425
credentials
2526
@@ -35,8 +36,14 @@ def __init__(self, customer, username, password, host='api.dynect.net',
3536
encrypting your password
3637
:param history: A boolean flag determining whether or not you would
3738
like to store a record of all API calls made to review later
39+
:param proxy_host: A proxy host to utilize
40+
:param proxy_port: The port that the proxy is served on
41+
:param proxy_user: A username to connect to the proxy with if required
42+
:param proxy_pass: A password to connect to the proxy with if required
3843
"""
39-
super(DynectSession, self).__init__(host, port, ssl, history)
44+
super(DynectSession, self).__init__(host, port, ssl, history,
45+
proxy_host, proxy_port,
46+
proxy_user, proxy_pass)
4047
self.__cipher = AESCipher(key)
4148
self.extra_headers = {'API-Version': api_version}
4249
self.customer = customer

0 commit comments

Comments
 (0)