Skip to content

Commit

Permalink
tornado async httpclient support
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettg committed Mar 16, 2012
1 parent 86dd503 commit 3060736
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ziggy/tornado_utils.py
Expand Up @@ -19,6 +19,7 @@

import tornado.web
import tornado.gen
import tornado.simple_httpclient

import ziggy

Expand Down Expand Up @@ -143,3 +144,29 @@ def run(self):
finally:
if self.ziggy_ctx:
self.ziggy_ctx.stop()

class AsyncHTTPClient(tornado.simple_httpclient.SimpleAsyncHTTPClient):
def __init__(self, *args, **kwargs):
self.ziggy_name = 'tornado.httpclient'
return super(AsyncHTTPClient, self).__init__(*args, **kwargs)

def fetch(self, request, callback, **kwargs):
ctx = ziggy.Context(self.ziggy_name)
ctx.start()
if isinstance(request, basestring):
ctx.set('request.uri', request)
else:
ctx.set('request.uri', request.url)
ctx.set('request.size', len(request.body) if request.body else 0)

ctx.stop()

def wrap_callback(response):
ctx.start()
ctx.set('response.code', response.code)
ctx.set('response.size', len(response.body))
ctx.done()
callback(response)

return super(AsyncHTTPClient, self).fetch(request, wrap_callback, **kwargs)

0 comments on commit 3060736

Please sign in to comment.