Skip to content

Instantly share code, notes, and snippets.

@eyeseast
Created September 12, 2010 17:15
Show Gist options
  • Select an option

  • Save eyeseast/576251 to your computer and use it in GitHub Desktop.

Select an option

Save eyeseast/576251 to your computer and use it in GitHub Desktop.
A Sunlight subclass that uses Tornado's AsyncHTTPClient instead of urllib2
from sunlight import Sunlight, SunlightError, RESPONSE_KEYS
from tornado import escape, httpclient
class TornadoSunlight(Sunlight):
"""
Subclassing Sunlight (from simple-sunlight) to use Tornado's AsyncHTTPClient
"""
def __init__(self, *args, **kwargs):
super(TornadoSunlight, self).__init__(*args, **kwargs)
self.client = httpclient.AsyncHTTPClient()
def __call__(self, **params):
params['apikey'] = self.apikey
url = BASE_URL % (self.method, urllib.urlencode(params))
return self.client.fetch(url, self._on_response)
def _on_response(self, response):
if response.error:
raise SunlightError(response.error)
yield escape.json_decode(response.body)['response'][RESPONSE_KEYS[self.method]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment