Skip to content

Commit

Permalink
Merge f3d826e into 42a6baa
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed Apr 20, 2017
2 parents 42a6baa + f3d826e commit 810f5b1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
7 changes: 4 additions & 3 deletions vumi/tests/fake_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def __init__(self, handler):
def endpoint(self):
return self.fake_server.endpoint

def get_agent(self, reactor=None, contextFactory=None):
def get_agent(self, reactor=None, pool=None, contextFactory=None):
"""
Returns an IAgent that makes requests to this fake server.
"""
Expand All @@ -297,6 +297,7 @@ def render_PUT(self, request):


class ProxyAgentWithContext(ProxyAgent):
def __init__(self, endpoint, reactor=None, contextFactory=None):
def __init__(self, endpoint, reactor=None, pool=None, contextFactory=None):
self.contextFactory = contextFactory # To assert on in tests.
super(ProxyAgentWithContext, self).__init__(endpoint, reactor=reactor)
super(ProxyAgentWithContext, self).__init__(
endpoint, reactor=reactor, pool=pool)
4 changes: 2 additions & 2 deletions vumi/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ def test_http_request_with_custom_context_factory(self):

ctxt = WebClientContextFactory()

def stashing_factory(reactor, contextFactory=None):
def stashing_factory(reactor, contextFactory=None, pool=None):
agent = self.fake_http.get_agent(
reactor, contextFactory=contextFactory)
reactor, contextFactory=contextFactory, pool=pool)
agents.append(agent)
return agent

Expand Down
52 changes: 52 additions & 0 deletions vumi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from twisted.web.iweb import IBodyProducer
from twisted.web.http import PotentialDataLoss
from twisted.web.resource import Resource
from treq._utils import default_pool, default_reactor
from treq.client import HTTPClient

from vumi.errors import VumiError

Expand Down Expand Up @@ -119,6 +121,56 @@ def connectionLost(self, reason):
def http_request_full(url, data=None, headers={}, method='POST',
timeout=None, data_limit=None, context_factory=None,
agent_class=None, reactor=None):
"""
This is a drop in replacement for the original `http_request_full` method
but it has its internals completely replaced by treq. Treq supports SNI
and our implementation does not for some reason. Also, we do not want
to continue maintaining this because we're favouring treq everywhere
anyway.
"""
agent_class = agent_class or Agent
if reactor is None:
# The import replaces the local variable.
from twisted.internet import reactor
pool = default_pool(reactor, pool=None, persistent=False)
context_factory = context_factory or WebClientContextFactory()
agent = agent_class(reactor, pool=pool, contextFactory=context_factory)
client = HTTPClient(agent)

def handle_response(response):
return SimplishReceiver(response, data_limit).deferred

d = client.request(method, url, headers=headers, data=data)
d.addCallback(handle_response)

if timeout is not None:
cancelling_on_timeout = [False]

def raise_timeout(reason):
if not cancelling_on_timeout[0] or reason.check(HttpTimeoutError):
return reason
return Failure(HttpTimeoutError("Timeout while connecting"))

def cancel_on_timeout():
cancelling_on_timeout[0] = True
d.cancel()

def cancel_timeout(r, delayed_call):
if delayed_call.active():
delayed_call.cancel()
return r

d.addErrback(raise_timeout)
delayed_call = reactor.callLater(timeout, cancel_on_timeout)
d.addCallback(cancel_timeout, delayed_call)

return d


def old_http_request_full(url, data=None, headers={}, method='POST',
timeout=None, data_limit=None, context_factory=None,
agent_class=None, reactor=None):
if reactor is None:
# The import replaces the local variable.
from twisted.internet import reactor
Expand Down

0 comments on commit 810f5b1

Please sign in to comment.