Skip to content

Commit

Permalink
Use schema.validate as opposed to simply validate in Hello World example
Browse files Browse the repository at this point in the history
  • Loading branch information
hfaran committed Feb 18, 2014
1 parent 3a3bb8a commit fd8853e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 11 additions & 8 deletions demos/helloworld/helloworld/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tornado import gen

from tornado_json.requesthandlers import APIHandler
from tornado_json.schema import validate
from tornado_json import schema


class HelloWorldHandler(APIHandler):
Expand All @@ -17,12 +17,12 @@ class HelloWorldHandler(APIHandler):
"doc": "Shouts hello to the world!",
}

# Decorate any HTTP methods with the `validate` decorator
# Decorate any HTTP methods with the `schema.validate` decorator
# to validate input to it and output from it as per the
# the schema for the method defined in `apid`
# Simply use `return` rather than `self.write` to write back
# your output.
@validate
@schema.validate
def get(self):
return "Hello world!"

Expand All @@ -43,13 +43,13 @@ class AsyncHelloWorld(APIHandler):
def hello(self, callback=None):
callback("Hello (asynchronous) world!")

@validate
@schema.validate
@gen.coroutine
def get(self):
# Asynchronously yield a result from a method
res = yield gen.Task(self.hello)

# When using the validate decorator asynchronously,
# When using the `schema.validate` decorator asynchronously,
# we can return the output desired by raising
# `tornado.gen.Return(value)` which returns a
# Future that the decorator will yield.
Expand Down Expand Up @@ -97,9 +97,9 @@ class PostIt(APIHandler):
"""
}

@validate
@schema.validate
def post(self):
# validate will JSON-decode `self.request.body` for us
# `schema.validate` will JSON-decode `self.request.body` for us
# and set self.body as the result, so we can use that here
return {
"message": "{} was posted.".format(self.body["title"])
Expand All @@ -124,7 +124,7 @@ class Greeting(APIHandler):
# arguments; here, you can GET /api/greeting/John/Smith and you will
# get a response back that says, "Greetings, John Smith!"
# You can match the regex equivalent of `\w+`.
@validate
@schema.validate
def get(self, fname, lname):
return "Greetings, {} {}!".format(fname, lname)

Expand All @@ -135,6 +135,9 @@ class FreeWilledHandler(APIHandler):
# if you want your handlers to do something more custom,
# they definitely can.
def get(self):
# If you don't know where `self.success` comes from, it is defined
# in the `JSendMixin` mixin in tornado_json.jsend. `APIHandler`
# inherits from this and thus gets the methods.
self.success("I don't need no stinkin' schema validation.")
# If you're feeling really bold, you could even skip JSend
# altogether and do the following EVIL thing:
Expand Down
1 change: 0 additions & 1 deletion tests/test_tornado_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
try:
sys.path.append('.')
from tornado_json import routes
#from tornado_json import utils
from tornado_json import schema
from tornado_json import exceptions
from tornado_json import jsend
Expand Down

0 comments on commit fd8853e

Please sign in to comment.