summaryrefslogtreecommitdiff
path: root/src/leap/common
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2015-08-11 11:44:42 -0300
committerdrebs <drebs@leap.se>2015-08-17 12:47:18 -0300
commit8965be42773e3e6c7e154f2c1a27f4e34031ae91 (patch)
tree1dab02151392b438018d89cd842e40cddcd0fc90 /src/leap/common
parentf1639503a58c76b00afc0a22b7928661dd08c771 (diff)
[feature] allow passing callback to http client
Diffstat (limited to 'src/leap/common')
-rw-r--r--src/leap/common/http.py26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/leap/common/http.py b/src/leap/common/http.py
index cda8ee8..6fc10b4 100644
--- a/src/leap/common/http.py
+++ b/src/leap/common/http.py
@@ -31,6 +31,7 @@ except ImportError:
from leap.common.certs import get_compatible_ssl_context_factory
+from leap.common.check import leap_assert
from zope.interface import implements
@@ -150,7 +151,7 @@ class HTTPClient(object):
pool.maxPersistentPerHost = maxPersistentPerHost
return pool
- def _request(self, url, method, body, headers):
+ def _request(self, url, method, body, headers, callback):
"""
Perform an HTTP request.
@@ -162,6 +163,9 @@ class HTTPClient(object):
:type body: str
:param headers: The headers of the request.
:type headers: dict
+ :param callback: A callback to be added to the request's deferred
+ callback chain.
+ :type callback: callable
:return: A deferred that fires with the body of the request.
:rtype: twisted.internet.defer.Deferred
@@ -170,14 +174,21 @@ class HTTPClient(object):
body = _StringBodyProducer(body)
d = self._agent.request(
method, url, headers=Headers(headers), bodyProducer=body)
- d.addCallback(readBody)
+ d.addCallback(callback)
return d
- def request(self, url, method='GET', body=None, headers={}):
+ def request(self, url, method='GET', body=None, headers={},
+ callback=readBody):
"""
Perform an HTTP request, but limit the maximum amount of concurrent
connections.
+ May be passed a callback to be added to the request's deferred
+ callback chain. The callback is expected to receive the response of
+ the request and may do whatever it wants with the response. By
+ default, if no callback is passed, we will use a simple body reader
+ which returns a deferred that is fired with the body of the response.
+
:param url: The URL for the request.
:type url: str
:param method: The HTTP method of the request.
@@ -186,11 +197,18 @@ class HTTPClient(object):
:type body: str
:param headers: The headers of the request.
:type headers: dict
+ :param callback: A callback to be added to the request's deferred
+ callback chain.
+ :type callback: callable
:return: A deferred that fires with the body of the request.
:rtype: twisted.internet.defer.Deferred
"""
- return self._semaphore.run(self._request, url, method, body, headers)
+ leap_assert(
+ callable(callback),
+ message="The callback parameter should be a callable!")
+ return self._semaphore.run(self._request, url, method, body, headers,
+ callback)
def close(self):
"""