Created
September 12, 2010 17:15
-
-
Save eyeseast/576251 to your computer and use it in GitHub Desktop.
A Sunlight subclass that uses Tornado's AsyncHTTPClient instead of urllib2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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