import json
from tornado.httpserver import HTTPServer
from  tornado.web import URLSpec
import tornado.ioloop
import tornado.web
from tornado.options import define, options

from orders import Book, Buy, Sell

define("port", default=3000, help="run on the given port", type=int)
Book()


class BookHandler(tornado.web.RequestHandler):

    def get(self):
        ret = json.dumps(Book().orders())
        self.write(ret)


class OrderHandler(tornado.web.RequestHandler):

    def post(self, **kwargs):
        order = None
        body = json.loads(self.request.body)
        if self.request.uri == "/buy":
            order = Buy(**body)
        if self.request.uri == "/sell":
            order = Sell(**body)
        fills = Book().match(order)
        self.write(json.dumps(fills))


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        URLSpec(r"/book", BookHandler, name="book"),
        URLSpec(r"/buy", OrderHandler, name="buy"),
        URLSpec(r"/sell", OrderHandler, name="sell"),
    ])
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

import json
from tornado.httpserver import HTTPServer
from tornado.httputil import HTTPHeaders
from  tornado.web import URLSpec
import tornado.ioloop
import tornado.web
from tornado.options import define, options

from orders import Book, Buy, Sell

define("port", default=3000, help="run on the given port", type=int)
Book()


class BookHandler(tornado.web.RequestHandler):

    def get(self):
        ret = json.dumps(Book().orders())
        self.write(ret)


class OrderHandler(tornado.web.RequestHandler):

    def post(self, **kwargs):
        order = None
        body = json.loads(self.request.body)
        if self.request.uri == "/buy":
            order = Buy(**body)
        if self.request.uri == "/sell":
            order = Sell(**body)
        fills = Book().match(order)
        self.set_header("content-type", "application/json")
        self.set_header("location", "{}://{}{}".format(self.request.protocol,
            self.request.host, self.reverse_url("book")))
        self.write(json.dumps(fills))


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        URLSpec(r"/book", BookHandler, name="book"),
        URLSpec(r"/buy", OrderHandler, name="buy"),
        URLSpec(r"/sell", OrderHandler, name="sell"),
    ])
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

Add location and content-type headers to response.
Add location and content-type headers to response.

  import json
  from tornado.httpserver import HTTPServer
+ from tornado.httputil import HTTPHeaders
  from  tornado.web import URLSpec
  import tornado.ioloop
  import tornado.web
  from tornado.options import define, options
  
  from orders import Book, Buy, Sell
  
  define("port", default=3000, help="run on the given port", type=int)
  Book()
  
  
  class BookHandler(tornado.web.RequestHandler):
  
      def get(self):
          ret = json.dumps(Book().orders())
          self.write(ret)
  
  
  class OrderHandler(tornado.web.RequestHandler):
  
      def post(self, **kwargs):
          order = None
          body = json.loads(self.request.body)
          if self.request.uri == "/buy":
              order = Buy(**body)
          if self.request.uri == "/sell":
              order = Sell(**body)
          fills = Book().match(order)
+         self.set_header("content-type", "application/json")
+         self.set_header("location", "{}://{}{}".format(self.request.protocol,
+             self.request.host, self.reverse_url("book")))
          self.write(json.dumps(fills))
  
  
  def main():
      tornado.options.parse_command_line()
      application = tornado.web.Application([
          URLSpec(r"/book", BookHandler, name="book"),
          URLSpec(r"/buy", OrderHandler, name="buy"),
          URLSpec(r"/sell", OrderHandler, name="sell"),
      ])
      http_server = HTTPServer(application)
      http_server.listen(options.port)
      tornado.ioloop.IOLoop.current().start()
  
  if __name__ == "__main__":
      main()
  
Add location and content-type headers to response.
## Code Before:
import json
from tornado.httpserver import HTTPServer
from  tornado.web import URLSpec
import tornado.ioloop
import tornado.web
from tornado.options import define, options

from orders import Book, Buy, Sell

define("port", default=3000, help="run on the given port", type=int)
Book()


class BookHandler(tornado.web.RequestHandler):

    def get(self):
        ret = json.dumps(Book().orders())
        self.write(ret)


class OrderHandler(tornado.web.RequestHandler):

    def post(self, **kwargs):
        order = None
        body = json.loads(self.request.body)
        if self.request.uri == "/buy":
            order = Buy(**body)
        if self.request.uri == "/sell":
            order = Sell(**body)
        fills = Book().match(order)
        self.write(json.dumps(fills))


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        URLSpec(r"/book", BookHandler, name="book"),
        URLSpec(r"/buy", OrderHandler, name="buy"),
        URLSpec(r"/sell", OrderHandler, name="sell"),
    ])
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()

## Instruction:
Add location and content-type headers to response.
## Code After:
import json
from tornado.httpserver import HTTPServer
from tornado.httputil import HTTPHeaders
from  tornado.web import URLSpec
import tornado.ioloop
import tornado.web
from tornado.options import define, options

from orders import Book, Buy, Sell

define("port", default=3000, help="run on the given port", type=int)
Book()


class BookHandler(tornado.web.RequestHandler):

    def get(self):
        ret = json.dumps(Book().orders())
        self.write(ret)


class OrderHandler(tornado.web.RequestHandler):

    def post(self, **kwargs):
        order = None
        body = json.loads(self.request.body)
        if self.request.uri == "/buy":
            order = Buy(**body)
        if self.request.uri == "/sell":
            order = Sell(**body)
        fills = Book().match(order)
        self.set_header("content-type", "application/json")
        self.set_header("location", "{}://{}{}".format(self.request.protocol,
            self.request.host, self.reverse_url("book")))
        self.write(json.dumps(fills))


def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        URLSpec(r"/book", BookHandler, name="book"),
        URLSpec(r"/buy", OrderHandler, name="buy"),
        URLSpec(r"/sell", OrderHandler, name="sell"),
    ])
    http_server = HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

if __name__ == "__main__":
    main()
