| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from google.appengine.api import urlfetch | |
| 6 from google.appengine.api import memcache | |
| 7 | |
| 8 DEFAULT_CACHE_TIME = 300 | |
| 9 | |
| 10 class _FetchException(Exception): | |
| 11 """Thrown when status code is not 200. | |
| 12 """ | |
| 13 def __init__(self, url): | |
| 14 Exception.__init__(self, 'Fetch exception from ' + url) | |
| 15 | |
| 16 def fetch(url): | |
| 17 result = memcache.get(url, namespace=__name__) | |
| 18 if result is not None: | |
| 19 return result | |
| 20 | |
| 21 result = urlfetch.fetch(url) | |
| 22 if result.status_code != 200: | |
| 23 raise _FetchException(url) | |
| 24 | |
| 25 memcache.add(url, result, DEFAULT_CACHE_TIME, namespace=__name__) | |
| 26 return result | |
| OLD | NEW |