#NEWLINE# def service( self ) : #NEWLINE# #INDENT# 'Handle a request\n    ' #NEWLINE# key = self . request . url . path . strip ( '/' ) #NEWLINE# self . response . headers = [ 'Content-Type: application/json' ] #NEWLINE# rsp = '{"status": "OK"}' #NEWLINE# if ( not key ) : #NEWLINE# #INDENT# if ( self . request . method == 'GET' ) : #NEWLINE# #INDENT# rsp = json_encode ( { #NEWLINE# #INDENT# 'keys' : self . entries . keys ( ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rsp = self . method_not_allowed ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . request . method == 'GET' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# rsp = json_encode ( self . entries [ key ] ) #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# rsp = self . not_found ( key ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . request . method in ( 'PUT' , 'POST' ) ) : #NEWLINE# #INDENT# self . entries [ self ] = json_decode ( self . request . input . read ( ( 1024 * 1024 ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . request . method == 'DELETE' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# del self . entries [ key ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# rsp = self . not_found ( key ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# rsp = self . method_not_allowed ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# self . response . headers . append ( ( 'Content-Length: %d' % ( len ( rsp ) + 1 ) ) ) #NEWLINE# self . response ( rsp , '\n' )