Added connection_type argument to Http.request() per Simon Willison request
diff --git a/CHANGELOG b/CHANGELOG
index 17824aa..67563c1 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,11 @@
+0.X.0
+
+ XXX
+
+ Added 'connection_type' parameter to Http.request().
+
+
+
0.3.0
Calling Http.request() with a relative URI, as opposed to an absolute URI,
will now throw a specific exception.
diff --git a/httplib2/__init__.py b/httplib2/__init__.py
index 0fa2e06..c103628 100644
--- a/httplib2/__init__.py
+++ b/httplib2/__init__.py
@@ -370,7 +370,7 @@
# So we also need each Auth instance to be able to tell us
# how close to the 'top' it is.
-class Authentication:
+class Authentication(object):
def __init__(self, credentials, host, request_uri, headers, response, content, http):
(scheme, authority, path, query, fragment) = parse_uri(request_uri)
self.path = path
@@ -599,7 +599,7 @@
def _md5(s):
return
-class FileCache:
+class FileCache(object):
"""Uses a local directory as a store for cached files.
Not really safe to use if multiple threads or processes are going to
be running on the same cache.
@@ -632,7 +632,7 @@
if os.path.exists(cacheFullPath):
os.remove(cacheFullPath)
-class Credentials:
+class Credentials(object):
def __init__(self):
self.credentials = []
@@ -708,7 +708,7 @@
-class Http:
+class Http(object):
"""An HTTP client that handles:
- all methods
- caching
@@ -875,7 +875,7 @@
# including all socket.* and httplib.* exceptions.
- def request(self, uri, method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS):
+ def request(self, uri, method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None):
""" Performs a single HTTP request.
The 'uri' is the URI of the HTTP resource and can begin
with either 'http' or 'https'. The value of 'uri' must be an absolute URI.
@@ -913,7 +913,8 @@
if conn_key in self.connections:
conn = self.connections[conn_key]
else:
- connection_type = (scheme == 'https') and HTTPSConnectionWithTimeout or HTTPConnectionWithTimeout
+ if not connection_type:
+ connection_type = (scheme == 'https') and HTTPSConnectionWithTimeout or HTTPConnectionWithTimeout
certs = list(self.certificates.iter(authority))
if scheme == 'https' and certs:
conn = self.connections[conn_key] = connection_type(authority, key_file=certs[0][0], cert_file=certs[0][1], timeout=self.timeout)
diff --git a/httplib2test.py b/httplib2test.py
index f6dbdb7..bc8829d 100755
--- a/httplib2test.py
+++ b/httplib2test.py
@@ -23,7 +23,7 @@
import urlparse
import time
import base64
-
+import StringIO
# Python 2.3 support
if not hasattr(unittest.TestCase, 'assertTrue'):
@@ -101,6 +101,40 @@
if sys.version_info >= (2,3):
self.assertEqual( "xn--http,-4y1d.org,fred,a=b,579924c35db315e5a32e3d9963388193", httplib2.safename(u"http://\u2304.org/fred/?a=b"))
+class _MyResponse(StringIO.StringIO):
+ def __init__(self, body, **kwargs):
+ StringIO.StringIO.__init__(self, body)
+ self.headers = kwargs
+
+ def iteritems(self):
+ return self.headers.iteritems()
+
+
+class _MyHTTPConnection(object):
+ "This class is just a mock of httplib.HTTPConnection used for testing"
+
+ def __init__(self, host, port=None, key_file=None, cert_file=None,
+ strict=None, timeout=None):
+ self.host = host
+ self.port = port
+ self.timeout = timeout
+ self.log = ""
+
+ def set_debuglevel(self, level):
+ pass
+
+ def connect(self):
+ "Connect to a host on a given port."
+ pass
+
+ def close(self):
+ pass
+
+ def request(self, method, request_uri, body, headers):
+ pass
+
+ def getresponse(self):
+ return _MyResponse("the body", status="200")
class HttpTest(unittest.TestCase):
@@ -110,6 +144,11 @@
self.http = httplib2.Http(cacheDirName)
self.http.clear_credentials()
+ def testConnectionType(self):
+ response, content = self.http.request("http://bitworking.org", connection_type=_MyHTTPConnection)
+ self.assertEqual(response['content-location'], "http://bitworking.org")
+ self.assertEqual(content, "the body")
+
def testGetUnknownServer(self):
self.http.force_exception_to_status_code = False
try:
diff --git a/libhttplib2.tex b/libhttplib2.tex
index 5ab860c..a2b5190 100644
--- a/libhttplib2.tex
+++ b/libhttplib2.tex
@@ -196,7 +196,7 @@
Http objects have the following methods:
-\begin{methoddesc}[Http]{request}{uri, \optional{method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS}}
+\begin{methoddesc}[Http]{request}{uri, \optional{method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None}}
Performs a single HTTP request.
The \var{uri} is the URI of the HTTP resource and can begin with either \code{http} or \code{https}. The value of \var{uri} must be an absolute URI.
@@ -211,6 +211,9 @@
The maximum number of redirect to follow before raising an exception is \var{redirections}. The default is 5.
+The \var{connection_type} is the type of connection object to use. The supplied class
+should implement the interface of httplib.HTTPConnection.
+
The return value is a tuple of (response, content), the first being and instance of the
\class{Response} class, the second being a string that contains the response entity body.
\end{methoddesc}
diff --git a/ref/http-objects.html b/ref/http-objects.html
index 4def852..1cd49ad 100644
--- a/ref/http-objects.html
+++ b/ref/http-objects.html
@@ -61,7 +61,7 @@
<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-14' xml:id='l2h-14' class="method">request</tt></b>(</nobr></td>
- <td><var>uri, </var><big>[</big><var>method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS</var><big>]</big><var></var>)</td></tr></table></dt>
+ <td><var>uri, </var><big>[</big><var>method="GET", body=None, headers=None, redirections=DEFAULT_MAX_REDIRECTS, connection_type=None</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Performs a single HTTP request.
The <var>uri</var> is the URI of the HTTP resource and can begin with either <code>http</code> or <code>https</code>. The value of <var>uri</var> must be an absolute URI.
@@ -82,6 +82,10 @@
The maximum number of redirect to follow before raising an exception is <var>redirections</var>. The default is 5.
<p>
+The <var>connection_type</var> is the type of connection object to use. The supplied class
+should implement the interface of httplib.HTTPConnection.
+
+<p>
The return value is a tuple of (response, content), the first being and instance of the
<tt class="class">Response</tt> class, the second being a string that contains the response entity body.
</dl>