Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import calendar | 5 import calendar |
| 6 import datetime | 6 import datetime |
| 7 import json | 7 import json |
| 8 import webapp2 | 8 import webapp2 |
| 9 import zlib | |
| 9 | 10 |
| 10 from google.appengine.api import memcache | 11 from google.appengine.api import memcache |
| 11 | 12 |
| 12 | 13 |
| 13 class DateTimeEncoder(json.JSONEncoder): | 14 class DateTimeEncoder(json.JSONEncoder): |
| 14 def default(self, obj): | 15 def default(self, obj): |
| 15 if isinstance(obj, datetime.datetime): | 16 if isinstance(obj, datetime.datetime): |
| 16 return calendar.timegm(obj.timetuple()) | 17 return calendar.timegm(obj.timetuple()) |
| 17 # Let the base class default method raise the TypeError | 18 # Let the base class default method raise the TypeError |
| 18 return json.JSONEncoder.default(self, obj) | 19 return json.JSONEncoder.default(self, obj) |
| 19 | 20 |
| 20 | 21 |
| 21 class AlertsHandler(webapp2.RequestHandler): | 22 class AlertsHandler(webapp2.RequestHandler): |
| 22 MEMCACHE_ALERTS_KEY = 'alerts' | 23 MEMCACHE_ALERTS_KEY = 'alerts' |
| 23 | 24 |
| 24 def get(self): | 25 def get(self): |
| 25 self.response.headers.add_header('Access-Control-Allow-Origin', '*') | 26 self.response.headers.add_header('Access-Control-Allow-Origin', '*') |
| 26 self.response.headers['Content-Type'] = 'application/json' | 27 self.response.headers['Content-Type'] = 'application/json' |
| 27 alerts = memcache.get(AlertsHandler.MEMCACHE_ALERTS_KEY) | 28 compressed = memcache.get(AlertsHandler.MEMCACHE_ALERTS_KEY) |
| 28 if not alerts: | 29 if not compressed: |
| 29 return | 30 return |
| 30 self.response.write(json.dumps(alerts, cls=DateTimeEncoder, indent=1)) | 31 uncompressed = zlib.decompress(compressed) |
| 32 self.response.write(uncompressed) | |
| 31 | 33 |
| 32 def post(self): | 34 def post(self): |
| 33 try: | 35 try: |
| 34 alerts = json.loads(self.request.get('content')) | 36 alerts = json.loads(self.request.get('content')) |
| 35 except ValueError: | 37 except ValueError: |
| 36 self.response.set_status(400, 'content field was not JSON') | 38 self.response.set_status(400, 'content field was not JSON') |
| 37 return | 39 return |
| 38 alerts.update({ | 40 alerts.update({ |
| 39 'date': datetime.datetime.utcnow(), | 41 'date': datetime.datetime.utcnow(), |
| 40 'alerts': alerts['alerts'] | 42 'alerts': alerts['alerts'] |
| 41 }) | 43 }) |
| 42 memcache.set(AlertsHandler.MEMCACHE_ALERTS_KEY, alerts) | 44 uncompressed = json.dumps(alerts, cls=DateTimeEncoder, indent=1) |
| 45 compression_level = 1 | |
| 46 compressed = zlib.compress(uncompressed, compression_level) | |
|
ojan
2014/08/14 01:03:51
Nit: can you just do the following?
compressed = z
| |
| 47 memcache.set(AlertsHandler.MEMCACHE_ALERTS_KEY, compressed) | |
| 43 | 48 |
| 44 | 49 |
| 45 app = webapp2.WSGIApplication([ | 50 app = webapp2.WSGIApplication([ |
| 46 ('/alerts', AlertsHandler) | 51 ('/alerts', AlertsHandler) |
| 47 ]) | 52 ]) |
| OLD | NEW |