import json
import tornado.ioloop
import tornado.web
import tornado.httpclient


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Blocking REQUEST!\n")
        http_client = tornado.httpclient.HTTPClient()
        url = 'http://catalog.data.gov/api/3/'
        response = http_client.fetch(url)
        # since response.body returns a "bytes" type,
        # I must decode it to a UTF-8 string:
        response_as_str = response.body.decode('utf-8')
        version_number = json.loads(response_as_str)['version']
        contents = 'VERSION NUMBER: {}'.format(version_number)
        self.write(contents)

application = tornado.web.Application([
    (r'/blocking', MainHandler),
])

if __name__ == '__main__':
    application.listen(5551)
    tornado.ioloop.IOLoop.instance().start()
