Skip to content

Commit

Permalink
Merge branch 'add_requests'
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Jacobson committed Apr 5, 2012
2 parents 2942dff + ec9e66f commit a2d257c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
15 changes: 10 additions & 5 deletions refreshbooks/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ def OAuthAuthorization(consumer, token, sig_method=None):
raise NotImplementedError('oauth support requires the "oauth" module.')

try:
from refreshbooks.transports import use_httplib2 as transport
from refreshbooks.transports import use_requests as transport
except ImportError:
import warnings
warnings.warn("Unable to load httplib2 transport, falling back to urllib2. SSL cert verification disabled.")
from refreshbooks.transports import use_urllib2 as transport
try:
from refreshbooks.transports import use_httplib2 as transport
except ImportError:
import warnings
warnings.warn(
"Unable to load requests or httplib2 transports, falling back to urllib2. SSL cert verification disabled."
)
from refreshbooks.transports import use_urllib2 as transport

class TokenAuthorization(object):
"""Generates HTTP BASIC authentication headers obeying FreshBooks'
Expand Down Expand Up @@ -60,4 +65,4 @@ def __call__(self):
return headers

HttpTransport = transport.Transport
TransportException = exceptions.TransportException
TransportException = exceptions.TransportException
18 changes: 18 additions & 0 deletions refreshbooks/transports/use_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests

class Transport(object):
def __init__(self, url, headers_factory):
self.url = url
self.headers_factory = headers_factory

def __call__(self, entity):

resp = requests.post(
self.url,
headers=self.headers_factory(),
data=entity
)
if resp.status_code >= 400:
raise TransportException(resp.status, content)

return resp.content
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
],
extras_require={
'oauth': ['oauth'],
'httplib2': ['httplib2 >= 0.7.2']
'httplib2': ['httplib2 >= 0.7.2'],
'requests': ['requests >= 0.11.1']
},

test_suite = 'nose.collector'
Expand Down

0 comments on commit a2d257c

Please sign in to comment.