Skip to content

Commit eb92448

Browse files
author
Claudio
committed
adding support to multi shortening domains
1 parent 7f08add commit eb92448

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

bitly.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import urlparse
2424
import string
2525

26-
BITLY_BASE_URL = "http://api.bit.ly/"
2726
BITLY_API_VERSION = "2.0.1"
2827

2928
VERBS_PARAM = {
@@ -49,11 +48,24 @@ class BitlyTimeoutError(BitlyError):
4948

5049

5150
class Api(object):
51+
52+
ALLOWED_API_DOMAINS = {
53+
'bit.ly': 'http://api.bit.ly/',
54+
'j.mp': 'http://api.j.mp/',
55+
}
56+
57+
domain = 'bit.ly'
58+
59+
def get_api_domain(self):
60+
return self.ALLOWED_API_DOMAINS[self.domain]
61+
5262
""" API class for bit.ly """
53-
def __init__(self, login, apikey):
63+
def __init__(self, login, apikey, domain=None):
5464
self.login = login
5565
self.apikey = apikey
5666
self._urllib = urllib2
67+
if domain is not None:
68+
self.domain = domain
5769

5870
def shorten(self, longURLs, params={}):
5971
"""
@@ -145,7 +157,7 @@ def _getURL(self, verb, paramVal, more_params={}):
145157
params.append(( verbParam, val ))
146158

147159
encoded_params = urllib.urlencode(params)
148-
return "%s%s?%s" % (BITLY_BASE_URL, verb, encoded_params)
160+
return "%s%s?%s" % (self.get_api_domain(), verb, encoded_params)
149161

150162
def _fetchUrl(self, url):
151163
'''Fetch a URL

test/test_shortening_urls.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def should_have_an_API_authenticated_by_my_credentials(api):
2626

2727
def should_shorten_an_url_consistently_when_a_single_string_url_is_provided(api):
2828
url_to_be_shortened = 'http://globoesporte.globo.com/motor/formula-1/noticia/2010/10/apos-maus-resultados-ferrari-reforca-apoio-massa-no-fim-da-temporada.html'
29-
expected_url = 'http://bit.ly/9n93fw'
29+
expected_hash = '/9n93fw'
3030

3131
shortened_url = api.shorten(longURLs=url_to_be_shortened)
3232

33-
assert shortened_url == expected_url, 'The shortened version of %s url should\'ve been %s but was %s' % (url_to_be_shortened, expected_url, shortened_url)
33+
assert shortened_url.endswith(expected_hash) , 'The shortened version of %s url should\'ve ended with %s but was %s' % (url_to_be_shortened, expected_url, shortened_url)
3434

3535
def should_shorten_many_urls_consistently_when_a_list_of_urls_is_provided(api):
3636
urls_to_be_shortened = [ 'http://globoesporte.globo.com/motor/formula-1/noticia/2010/10/apos-maus-resultados-ferrari-reforca-apoio-massa-no-fim-da-temporada.html'
@@ -153,3 +153,9 @@ def verifying_that_any_other_exceptions_are_reraised_but_timeout_exceptions(api)
153153
mox.VerifyAll()
154154
finally:
155155
mox.UnsetStubs()
156+
157+
def test_i_can_use_the_freaking_jmp_domain_instead():
158+
api = bitly.Api(login='jcfigueiredo', apikey='R_1cf5dc0fa14c2df34261fb620bd256aa', domain='j.mp')
159+
160+
yield should_have_an_API_authenticated_by_my_credentials, api
161+
yield should_shorten_an_url_consistently_when_a_single_string_url_is_provided, api

0 commit comments

Comments
 (0)