Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional support for urllib2 instead of httplib2 #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build/
dist/
.virtualenv
.noseids

.idea
17 changes: 10 additions & 7 deletions embedly/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import re
import urllib
import httplib2
from embedly.httpclients import Httplib2Client

try:
import json
Expand All @@ -22,23 +22,29 @@ 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

:param user_agent: User Agent passed to Embedly
: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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions embedly/httpclients.py
Original file line number Diff line number Diff line change
@@ -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
112 changes: 64 additions & 48 deletions embedly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
'Programming Language :: Python :: 2.7',
),
**extra
)
)