diff --git a/.gitignore b/.gitignore index c18dd39..c6789fc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ build/ dist/ .virtualenv .noseids - +.idea diff --git a/embedly/client.py b/embedly/client.py index a12c08f..8d4e0ff 100644 --- a/embedly/client.py +++ b/embedly/client.py @@ -6,7 +6,7 @@ """ import re import urllib -import httplib2 +from embedly.httpclients import Httplib2Client try: import json @@ -22,7 +22,7 @@ class Embedly(object): Client """ - def __init__(self, key=None, user_agent=USER_AGENT): + def __init__(self, key=None, user_agent=USER_AGENT, http_client=Httplib2Client()): """ Initialize the Embedly client @@ -30,15 +30,21 @@ def __init__(self, key=None, user_agent=USER_AGENT): :type user_agent: str :param key: Embedly Pro key :type key: str + :param use_urllib2: Option to use urllib2 instead of httplib2 + :type use_urllib2: bool :returns: None """ self.user_agent = user_agent self.key = key + self.http_client = http_client self.services = [] self._regex = None + def _make_request(self, url, headers={}): + return self.http_client.request(url, headers) + def get_services(self): """ get_services makes call to services end point of api.embed.ly to fetch @@ -49,9 +55,8 @@ def get_services(self): url = 'http://api.embed.ly/1/services/python' - http = httplib2.Http() headers = {'User-Agent' : self.user_agent} - resp, content = http.request(url, headers=headers) + resp, content = self._make_request(url, headers) if resp['status'] == '200': resp_data = json.loads(content) @@ -118,11 +123,9 @@ def _get(self, version, method, url_or_urls, **kwargs): url = 'http://api.embed.ly/%s/%s?%s' % (version, method, query) - http = httplib2.Http() - headers = {'User-Agent' : self.user_agent} - resp, content = http.request(url, headers=headers) + resp, content = self._make_request(url, headers=headers) if resp['status'] == '200': data = json.loads(content) diff --git a/embedly/httpclients.py b/embedly/httpclients.py new file mode 100644 index 0000000..c3ed56a --- /dev/null +++ b/embedly/httpclients.py @@ -0,0 +1,49 @@ +""" +Httpclients +====== + +An http client has a request() method that accepts the following arguments: + url: the URL being requested + headers: a dictionary of headers to use with the request + +request() returns a dictionary of response headers and the body of the response + +Here are 2 HTTP clients, feel free to use your own +""" +import urllib2 +import httplib2 + +class Httplib2Client(object): + def __init__(self, timeout=30): + self.timeout = timeout + + def request(self, url, headers=None): + """ + Makes HTTP requests using httplib2 + """ + http = httplib2.Http(timeout=self.timeout) + resp, content = http.request(url, headers=headers) + + return resp, content + + +class Urllib2Client(object): + def __init__(self, timeout=30): + self.timeout = timeout + + def request(self, url, headers=None): + """ + Makes HTTP requests using urllib2 + """ + try: + request = urllib2.Request(url, headers=(headers or {})) + response = urllib2.urlopen(request, timeout=self.timeout) + resp = response.headers.dict + if "status" not in resp: + resp["status"] = str(response.code) + content = response.read() + except urllib2.HTTPError, e: + resp = {"status" : str(e.getcode())} + content = e.read() + + return resp, content diff --git a/embedly/tests.py b/embedly/tests.py index f6bd93a..9309121 100644 --- a/embedly/tests.py +++ b/embedly/tests.py @@ -3,6 +3,7 @@ import unittest from embedly.client import Embedly +from embedly.httpclients import Httplib2Client, Urllib2Client from embedly.models import Url class EmbedlyTestCase(unittest.TestCase): @@ -74,66 +75,81 @@ def test_model(self): self.assert_(obj.object.type is None) def test_provider(self): - http = Embedly(self.key) + def do_test(http): + obj = http.oembed('http://www.scribd.com/doc/13994900/Easter') + self.assert_(obj.provider_url == 'http://www.scribd.com/') - obj = http.oembed('http://www.scribd.com/doc/13994900/Easter') - self.assert_(obj.provider_url == 'http://www.scribd.com/') + obj = http.oembed('http://www.scribd.com/doc/28452730/Easter-Cards') + self.assert_(obj.provider_url == 'http://www.scribd.com/') - obj = http.oembed('http://www.scribd.com/doc/28452730/Easter-Cards') - self.assert_(obj.provider_url == 'http://www.scribd.com/') + obj = http.oembed('http://www.youtube.com/watch?v=Zk7dDekYej0') + self.assert_(obj.provider_url == 'http://www.youtube.com/') - obj = http.oembed('http://www.youtube.com/watch?v=Zk7dDekYej0') - self.assert_(obj.provider_url == 'http://www.youtube.com/') + obj = http.oembed('http://yfrog.com/h22eu4j') + self.assert_(obj.provider_url == 'http://yfrog.com') - obj = http.oembed('http://yfrog.com/h22eu4j') - self.assert_(obj.provider_url == 'http://yfrog.com') + http = Embedly(self.key, http_client=Httplib2Client()) + do_test(http) + http = Embedly(self.key, http_client=Urllib2Client()) + do_test(http) def test_providers(self): - http = Embedly(self.key) - - objs = http.oembed(['http://www.scribd.com/doc/13994900/Easter', + def do_test(http): + objs = http.oembed(['http://www.scribd.com/doc/13994900/Easter', 'http://www.scribd.com/doc/28452730/Easter-Cards']) - self.assert_(objs[0].provider_url == 'http://www.scribd.com/') - self.assert_(objs[1].provider_url == 'http://www.scribd.com/') + self.assert_(objs[0].provider_url == 'http://www.scribd.com/') + self.assert_(objs[1].provider_url == 'http://www.scribd.com/') - objs = http.oembed(['http://www.youtube.com/watch?v=Zk7dDekYej0', - 'http://yfrog.com/h22eu4']) - self.assert_(objs[0].provider_url == 'http://www.youtube.com/') - self.assert_(objs[1].provider_url == 'http://yfrog.com') + objs = http.oembed(['http://www.youtube.com/watch?v=Zk7dDekYej0', + 'http://yfrog.com/h22eu4']) + self.assert_(objs[0].provider_url == 'http://www.youtube.com/') + self.assert_(objs[1].provider_url == 'http://yfrog.com') - def test_error(self): - http = Embedly(self.key) + http = Embedly(self.key, http_client=Httplib2Client()) + do_test(http) + http = Embedly(self.key, http_client=Urllib2Client()) + do_test(http) - obj = http.oembed('http://www.embedly.com/this/is/a/bad/url') - self.assert_(obj.error is True, obj.dict) - obj = http.oembed('http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf') - self.assert_(obj.error is True, obj.dict) - obj = http.oembed('http://twitpic/nothing/to/see/here') - self.assert_(obj.error is True, obj.dict) + def test_error(self): + def do_test(http): + obj = http.oembed('http://www.embedly.com/this/is/a/bad/url') + self.assert_(obj.error is True, obj.dict) + obj = http.oembed('http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf') + self.assert_(obj.error is True, obj.dict) + obj = http.oembed('http://twitpic/nothing/to/see/here') + self.assert_(obj.error is True, obj.dict) + + http = Embedly(self.key, http_client=Httplib2Client()) + do_test(http) + http = Embedly(self.key, http_client=Urllib2Client()) + do_test(http) def test_multi_errors(self): - http = Embedly(self.key) - - objs = http.oembed(['http://www.embedly.com/this/is/a/bad/url', - 'http://blog.embed.ly/alsd/slsdlf/asdlfj']) - self.assert_(objs[0].type == 'error', objs[0].dict) - self.assert_(objs[1].type == 'error', objs[1].dict) - - objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', - 'http://twitpic.com/nothing/to/see/here']) - self.assert_(objs[0].type == 'error',objs[0].dict) - self.assert_(objs[1].type == 'error',objs[1].dict) - - objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', - 'http://yfrog.com/h22eu4j']) - self.assert_(objs[0].type == 'error',objs[0].dict) - self.assert_(objs[1].type == 'photo',objs[1].dict) - - objs = http.oembed(['http://yfrog.com/h22eu4j', - 'http://www.scribd.com/asdf/asdf/asdfasdf']) - self.assert_(objs[0].type == 'photo',objs[0].dict) - self.assert_(objs[1].type == 'error',objs[1].dict) - + def do_test(http): + objs = http.oembed(['http://www.embedly.com/this/is/a/bad/url', + 'http://blog.embed.ly/alsd/slsdlf/asdlfj']) + self.assert_(objs[0].type == 'error', objs[0].dict) + self.assert_(objs[1].type == 'error', objs[1].dict) + + objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', + 'http://twitpic.com/nothing/to/see/here']) + self.assert_(objs[0].type == 'error',objs[0].dict) + self.assert_(objs[1].type == 'error',objs[1].dict) + + objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', + 'http://yfrog.com/h22eu4j']) + self.assert_(objs[0].type == 'error',objs[0].dict) + self.assert_(objs[1].type == 'photo',objs[1].dict) + + objs = http.oembed(['http://yfrog.com/h22eu4j', + 'http://www.scribd.com/asdf/asdf/asdfasdf']) + self.assert_(objs[0].type == 'photo',objs[0].dict) + self.assert_(objs[1].type == 'error',objs[1].dict) + + http = Embedly(self.key, http_client=Httplib2Client()) + do_test(http) + http = Embedly(self.key, http_client=Urllib2Client()) + do_test(http) def test_too_many_urls(self): http = Embedly(self.key) diff --git a/setup.py b/setup.py index 8b7912f..789e641 100644 --- a/setup.py +++ b/setup.py @@ -43,4 +43,4 @@ 'Programming Language :: Python :: 2.7', ), **extra -) \ No newline at end of file +)