Commits

Reid Draper  committed 41cdafe

make the test app take in values from the url, add an _encode method for unicode keys and values

  • Participants
  • Parent commits 0827d5a

Comments (0)

Files changed (2)

 import swirl
 
 class RedisTornado(object):
-    def __init__(self, host = None, port = None):
+    def __init__(self, host = None, port = None, encoding = 'utf-8'):
         """
         Asynchronous Redis client using the Tornado IOLoop
         """
         self.host       = host or 'localhost'
         self.port       = port or 6379
+        self.encoding   = encoding
         self._queue     = deque()
         self.callback   = None
         self._waiting   = False
                 self.waiting = False
                 self._work()
                 return
+
             response = yield lambda cb : self.stream.read_bytes(num_bytes + self.DELBYTES, cb)
             callback(response[:-2])
             self._waiting = False
             self._work()
             return
 
+    def _encode(self, value):
+        if isinstance(value, unicode):
+            return value.encode(self.encoding)
+        else:
+            return value
+
     def _cmd_creator(self, command, args):
         num_args    = len(args) + 1 # add one for the command
-        args_string = ''.join(['$%d\r\n%s\r\n' % (len(arg), arg) for arg in args])
+        args = (self._encode(arg) for arg in args)
+        args_string = ''.join('$%d\r\n%s\r\n' % (len(arg), arg) for arg in args)
         cmd = '*%d\r\n$%d\r\n%s\r\n%s' % (num_args, len(command), command, args_string)
         return cmd
         
 
 class GetHandler(tornado.web.RequestHandler):
     @swirl.asynchronous
-    def get(self):
-        value = (yield lambda cb : r.get('foo', cb)) or 'Nope'
+    def get(self, key):
+        value = (yield lambda cb : r.get(key, cb)) or str(False)
         self.write(value)
 
 class SetHandler(tornado.web.RequestHandler):
     @swirl.asynchronous
-    def get(self):
-        value = yield lambda cb: r.set('foo', 'bar', cb)
-        self.write(str(value))
+    def put(self, key):
+        value = self.get_argument('value')
+        response = yield lambda cb: r.set(key, value, cb)
+        self.write(str(response))
 
 
 def main():
     tornado.options.parse_command_line()
     application = tornado.web.Application([
-        (r'/get',   GetHandler),
-        (r'/set',   SetHandler),
+        (r'/get/(.+)',   GetHandler),
+        (r'/set/(.+)',   SetHandler),
     ], **settings)
     http_server = tornado.httpserver.HTTPServer(application)
     http_server.listen(options.port)