Commit 41a1c84a authored by Craig Weber's avatar Craig Weber

Add new setting to allow setting SOAP proxy per domain

parent 33c6c1ce
......@@ -16,4 +16,5 @@ INSTALLED_APPS = [
SECRET_KEY = 'li0$-gnv)76g$yf7p@(cg-^_q7j6df5cx$o-gsef5hd68phj!4'
SOAP_PROXY_URL = None
SOAP_PROXIES = {
}
......@@ -2,6 +2,7 @@ from django_statsd.clients import statsd
from suds.transport import Transport, Reply
from . import settings
import urllib.request
import urllib.parse
import logging
import requests
import io
......@@ -45,11 +46,14 @@ class HttpTransport(Transport):
if url.startswith('file://'):
content = urllib.request.urlopen(url)
else:
resp = self.session.get(url, proxies=self.proxies, timeout=self.open_timeout)
resp = self.session.get(url,
proxies=self.proxies(url),
timeout=self.open_timeout)
resp.raise_for_status()
content = io.BytesIO(resp.content)
return content
def send(self, request):
"""
Send a SOAP method call
......@@ -64,21 +68,30 @@ class HttpTransport(Transport):
logger.debug('Sending SOAP request: %s' % url)
statsd.incr('soap.send')
with statsd.timer('soap.send'):
resp = self.session.post(url, proxies=self.proxies, timeout=self.send_timeout, data=msg, headers=headers)
resp = self.session.post(url,
proxies=self.proxies(url),
timeout=self.send_timeout,
data=msg,
headers=headers)
resp.raise_for_status()
reply = Reply(requests.codes.OK, resp.headers, resp.content)
return reply
@property
def proxies(self):
def proxies(self, url):
"""
Get the transport proxy configuration
:param url: string
:return: Proxy configuration dictionary
:rtype: Dictionary
"""
netloc = urllib.parse.urlparse(url).netloc
proxies = {}
if settings.PROXY_URL:
if settings.PROXIES and settings.PROXIES.get(netloc):
proxies["http"] = settings.PROXIES[netloc]
proxies["https"] = settings.PROXIES[netloc]
elif settings.PROXY_URL:
proxies["http"] = settings.PROXY_URL
proxies["https"] = settings.PROXY_URL
return proxies
import warnings
try:
from django.conf import settings
except ImportError:
......@@ -25,8 +26,20 @@ REMOVE_CACHE_ON_EXIT = overridable('SOAP_REMOVE_CACHE_ON_EXIT', False)
WSDL_INTERCEPTS = overridable('SOAP_WSDL_INTERCEPTS', {})
#: Optional HTTP/HTTPS proxy URL
#: DEPRECATED: Use SOAP_PROXIES instead
#: Set Django ``settings.SOAP_PROXY_URL`` to override.
PROXY_URL = overridable('SOAP_PROXY_URL')
if PROXY_URL:
warnings.warn("The SOAP_PROXY_URL setting is deprecated. Migrate to the SOAP_PROXIES setting.", DeprecationWarning)
#: Optional HTTP/HTTPS proxy URL
#: Set Django ``settings.SOAP_PROXY_URL`` to override.
#: Format:
#:
#: SOAP_PROXIES = {
#: "soap.my-server.com": "http://myproxy.com:3128/",
#: }
PROXIES = overridable('SOAP_PROXIES')
#: Timeout for opening WSDLs. Should be a tuple containing (1) the TCP connect
#: timeout and (2) the response timeout.
......
......@@ -9,7 +9,7 @@ class SOAPTest(TestCase):
client = soap.get_client(wsdl, 'LOCATIONS')
client.set_options(port='NumberConversionSoap')
resp = client.service.NumberToWords('42')
self.assertEquals(str(resp).strip(), 'forty two')
self.assertEqual(str(resp).strip(), 'forty two')
class SOAPProxyTest(TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment