Skip to content

Instantly share code, notes, and snippets.

@douglascamata
Created August 2, 2012 23:14
Show Gist options
  • Save douglascamata/3241776 to your computer and use it in GitHub Desktop.
Save douglascamata/3241776 to your computer and use it in GitHub Desktop.
My code
@defer.inlineCallbacks
@cyclone.web.asynchronous
def put(self):
self.set_header('Content-Type', 'application/json')
key = str(uuid4())
today = datetime.today().strftime(u'%d/%m/%y %H:%M')
user = self._get_current_user()[0]
value = self._load_request_as_json().get('value')
if not value:
log.msg("PUT failed!")
log.msg("Request didn't have a value to store.")
raise cyclone.web.HTTPError(400, 'Malformed request.')
data_dict = {u'data':value, u'date':today, u'from_user': user}
json_dict = dumps(data_dict)
del data_dict
# self.settings.db is created by txredisapi.lazyShardedConnectionPool(address_list)
# where address_list is something like ['192.168.0.154:6973', '192.168.0.105:6973', '127.0.0.1:6973']
result = yield self.settings.db.set(key, json_dict)
checksum = self._calculate_sha1_checksum(json_dict)
del json_dict
log.msg("Value stored at key %s." % key)
self.finish(cyclone.escape.json_encode({u'key':key, u'checksum':checksum}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment