Commits

benoitc  committed 16f0887

close issue #2 . thanks!

  • Participants
  • Parent commits c0fc384

Comments (0)

Files changed (4)

File restclient/__init__.py

 except:
     __version__ = '?'
 
-
+from restclient.transport import CurlTransport, HTTPLib2Transport
 from restclient.rest import * 
 
 

File restclient/rest.py

 class ResourceError(Exception):
 
     def __init__(self, msg=None, http_code=None, response=None):
-        self.msg = msg
+        self.msg = msg or ''
         self.status_code = http_code
         self.response = response
 

File restclient/transport.py

 #
 
 import codecs
+import httplib
+import logging
 import StringIO
-import httplib
 
 import re
 import sys
 except ImportError:
     pycurl=None
 
+
+
 _default_http = None
 
 class TransportError(Exception):
 USER_AGENT = "py-restclient/%s (%s)" % (restclient.__version__, sys.platform)
 DEFAULT_MAX_REDIRECT = 3
 
+# set to a non-zero value to get debug output
+debuglevel = 0
+
 NORMALIZE_SPACE = re.compile(r'(?:\r\n)?[ \t]+')
 def _normalize_headers(headers):
     return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.iteritems()])
         setDefaultHTTPTransport(createHTTPTransport())
 
     return isinstance(_default_http, CurlTransport)
+    
+
+
+    
+
 
 class HTTPError(Exception):
     """ raised when there is an HTTP error """
     CURLE_SSL_CACERT = _get_pycurl_errcode('E_SSL_CACERT', 60)
     CURLE_SSL_CACERT_BADFILE = _get_pycurl_errcode('E_SSL_CACERT_BADFILE', 77)    
 
-
 class CurlTransport(HTTPTransportBase):
     """
     An HTTP transportthat uses pycurl.
 
         # encode url
         url = iri2uri(to_bytestring(url))
-        
+
         c = pycurl.Curl()
         try:
             # set curl options
             c.setopt(pycurl.FOLLOWLOCATION, 1)
             c.setopt(pycurl.MAXREDIRS, 5)
             c.setopt(pycurl.NOSIGNAL, 1)
+            if debuglevel > 0:
+                 c.setopt(pycurl.VERBOSE, 1)
 
             if self.cabundle:
                 c.setopt(pycurl.CAINFO, celf.cabundle)
                 c.perform()
             except pycurl.error, e:
                 if e[0] != CURLE_SEND_ERROR:
+                    if debuglevel > 0:
+                        print >>sys.stderr, str(e)
                     raise TransportError(e)
  
             response_headers = self._parseHeaders(header)
             code = c.getinfo(pycurl.RESPONSE_CODE)
-            
             return self._make_response(final_url=url, status=code,
                     headers=response_headers, body=data.getvalue())
         finally:
 
         super(HTTPLib2Transport, self).__init__(proxy_infos=proxy_infos)
         
+        # set debug level
+        httplib2.debuglevel = debuglevel
+        
         _proxy_infos = None
         if proxy_infos and proxy_infos is not None:
             try:
             headers.setdefault('Content-Length', str(content_length))
 
         if not (url.startswith('http://') or url.startswith('https://')):
-            raise ValueError('URL is not a HTTP URL: %r' % (url,))
+            error = 'URL is not a HTTP URL: %r' % (url,)
+            if DEBUG:
+                print >>sys.stderr, str(error)
+            raise ValueError(error)
 
         headers.setdefault('User-Agent', USER_AGENT)
         

File tests/transports_test.py

 import unittest
 import urlparse
 
-
 from restclient.transport import CurlTransport
 from restclient.rest import Resource, RestClient, RequestFailed, \
 ResourceNotFound, Unauthorized
                 headers={'Content-Type': 'application/json'})
         self.assert_(self.res.response.status == 200 )
         def bad_post():
-            result = self.res.post('/json', payload="test", 
+            return self.res.post('/json', payload="test", 
                     headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_post)
 
                 headers={'Content-Type': 'application/json'})
         self.assert_(self.res.response.status == 200 )
         def bad_put():
-            result = self.res.put('/json', payload="test",
+            return self.res.put('/json', payload="test",
                     headers={'Content-Type': 'text/plain'})
         self.assertRaises(RequestFailed, bad_put)