| 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 import logging |
| 6 from google.appengine.api import urlfetch |
| 7 |
| 8 from future import Future |
| 9 |
| 10 class _AsyncFetchDelegate(object): |
| 11 def __init__(self, rpc): |
| 12 self._rpc = rpc |
| 13 |
| 14 def Get(self): |
| 15 self._rpc.wait() |
| 16 return self._rpc.get_result() |
| 17 |
| 18 class AppEngineUrlFetcher(object): |
| 19 """A wrapper around the App Engine urlfetch module that allows for easy |
| 20 async fetches. |
| 21 """ |
| 22 def __init__(self, base_path): |
| 23 self._base_path = base_path |
| 24 |
| 25 def Fetch(self, url): |
| 26 """Fetches a file synchronously. |
| 27 """ |
| 28 return urlfetch.fetch(self._base_path + '/' + url) |
| 29 |
| 30 def FetchAsync(self, url): |
| 31 """Fetches a file asynchronously, and returns a Future with the result. |
| 32 """ |
| 33 rpc = urlfetch.create_rpc() |
| 34 urlfetch.make_fetch_call(rpc, self._base_path + '/' + url) |
| 35 return Future(delegate=_AsyncFetchDelegate(rpc)) |
| OLD | NEW |