Skip to content

Commit 40d9b78

Browse files
author
Claudio
committed
replacing default http engine for httplib2
1 parent 3374237 commit 40d9b78

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

bitly.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
VERSION = '0.2'
17+
VERSION = '0.3'
1818
__version__ = VERSION
1919

2020
import simplejson
21-
import urllib, urllib2
21+
import urllib
22+
import httplib2
2223
import socket
2324
from urllib2 import URLError
2425
import urlparse
@@ -66,7 +67,7 @@ def get_api_domain(self):
6667
def __init__(self, login, apikey, domain=None):
6768
self.login = login
6869
self.apikey = apikey
69-
self._urllib = urllib2
70+
self._urllib = httplib2
7071
if domain is not None:
7172
self.domain = domain
7273

@@ -136,7 +137,7 @@ def setUrllib(self, urllib):
136137
'''Override the default urllib implementation.
137138
138139
Args:
139-
urllib: an instance that supports the same API as the urllib2 module
140+
urllib or httplib2
140141
'''
141142
self._urllib = urllib
142143

@@ -174,13 +175,17 @@ def _fetchUrl(self, url):
174175

175176
# Open and return the URL
176177
try:
177-
178-
socket.setdefaulttimeout(TIMEOUT);
179-
url_data = self._urllib.urlopen(url=url).read()
180-
except URLError, err:
178+
if self._urllib is httplib2:
179+
http = httplib2.Http(timeout=TIMEOUT)
180+
resp, content = http.request(url)
181+
url_data = content
182+
else:
183+
url_data = self._urllib.urlopen(url=url).read()
184+
185+
except (URLError, socket.error), err:
181186
# nasty bit of hack, i know but unfortunatly urllib2 has no smart way of telling me
182187
# that it was an timeout error
183-
if err.reason == 'urlopen error timed out':
188+
if hasattr(err,'reason') and err.reason == 'urlopen error timed out' or unicode(err) == u'timed out':
184189
raise BitlyTimeoutError('The url %s has timed out' % url)
185190
raise err
186191

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
py_modules=['bitly'],
1616
install_requires=[
1717
'simplejson',
18+
'httplib2'
1819
],
1920
)
2021

test/test_shortening_urls.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mox import Mox
77

88
import bitly
9-
from bitly import urllib2
9+
from bitly import httplib2
1010

1111
def test_urls_shortening_scenario_for_plain_urls():
1212
api = bitly.Api(login='jcfigueiredo', apikey='R_1cf5dc0fa14c2df34261fb620bd256aa')
@@ -108,8 +108,8 @@ def verifying_that_a_custom_exception_is_raised_on_timeout(api):
108108

109109
bitly_shortening_url = 'http://api.bit.ly/shorten?login=jcfigueiredo&version=2.0.1&apiKey=R_1cf5dc0fa14c2df34261fb620bd256aa&format=json&longUrl=http%3A%2F%2Fwww.matandorobosgigantes.com'
110110

111-
mox.StubOutWithMock(urllib2, "urlopen")
112-
urllib2.urlopen(url=bitly_shortening_url).AndRaise(URLError('urlopen error timed out'))
111+
mox.StubOutWithMock(httplib2.Http, "request")
112+
httplib2.Http.request(bitly_shortening_url).AndRaise(URLError('urlopen error timed out'))
113113

114114
url_to_be_shortened = 'http://www.matandorobosgigantes.com'
115115

@@ -125,8 +125,8 @@ def verifying_that_urlerror_exceptions_are_reraised_but_timeout_exceptions(api):
125125

126126
bitly_shortening_url = 'http://api.bit.ly/shorten?login=jcfigueiredo&version=2.0.1&apiKey=R_1cf5dc0fa14c2df34261fb620bd256aa&format=json&longUrl=http%3A%2F%2Fwww.matandorobosgigantes.com'
127127

128-
mox.StubOutWithMock(urllib2, "urlopen")
129-
urllib2.urlopen(url=bitly_shortening_url).AndRaise(URLError('something different from timeout'))
128+
mox.StubOutWithMock(httplib2.Http, "request")
129+
httplib2.Http.request(bitly_shortening_url).AndRaise(URLError('something different from timeout'))
130130

131131
url_to_be_shortened = 'http://www.matandorobosgigantes.com'
132132

@@ -142,8 +142,8 @@ def verifying_that_any_other_exceptions_are_reraised_but_timeout_exceptions(api)
142142

143143
bitly_shortening_url = 'http://api.bit.ly/shorten?login=jcfigueiredo&version=2.0.1&apiKey=R_1cf5dc0fa14c2df34261fb620bd256aa&format=json&longUrl=http%3A%2F%2Fwww.matandorobosgigantes.com'
144144

145-
mox.StubOutWithMock(urllib2, "urlopen")
146-
urllib2.urlopen(url=bitly_shortening_url).AndRaise(ValueError('something different'))
145+
mox.StubOutWithMock(httplib2.Http, "request")
146+
httplib2.Http.request(bitly_shortening_url).AndRaise(ValueError('something different'))
147147

148148
url_to_be_shortened = 'http://www.matandorobosgigantes.com'
149149

0 commit comments

Comments
 (0)