Database Reference
In-Depth Information
Log Trampoline
Start by looking at how the client communicates with the web application
to implement registration and logging. Just like the client had a single
class, _JSONHandler , encapsulate the details of the client server protocol
in the mobile client, there is a class in the server code that deals with the
detail of unpacking requests and packing responses. Subclasses implement
the handle(self, arg) method and perform the actual operation and
return a result as a JSON object, which in Python is any dictionary-like
object that the JSON library can serialize. The method can optionally raise
an exception, which the base class catches and transforms into an error as
specified by the simple communication protocol used in this application.
Listing 8.4 contains the source code for the base class and the two handlers
implemented in the application.
Listing 8.4 : Command handlers (main.py)
class _JsonHandler(webapp2.RequestHandler):
'''Generic JSON command handler.'''
MAX_PAYLOAD_SIZE = 16 * 1024
def post(self):
if self.request.headers.get('Content-Type') !=
'application/json':
self.response.set_status(
httplib.UNSUPPORTED_MEDIA_TYPE,
message='Expected Content-Type: application/
json')
return
if len(self.request.body) > self.MAX_PAYLOAD_SIZE:
self.response.set_status(
httplib.REQUEST_ENTITY_TOO_LARGE,
message=('Max payload size (%d) exceeded' %
self.MAX_PAYLOAD_SIZE))
return
try:
arg = json.loads(self.request.body)
except ValueError, e:
self.response.set_status(
httplib.BAD_REQUEST,
 
 
Search WWH ::




Custom Search