| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 from appengine_wrappers import urlfetch | 5 from appengine_wrappers import urlfetch |
| 6 from future import Future | 6 from future import Future |
| 7 | 7 |
| 8 class _AsyncFetchDelegate(object): | 8 class _AsyncFetchDelegate(object): |
| 9 def __init__(self, rpc): | 9 def __init__(self, rpc): |
| 10 self._rpc = rpc | 10 self._rpc = rpc |
| 11 | 11 |
| 12 def Get(self): | 12 def Get(self): |
| 13 self._rpc.wait() | |
| 14 return self._rpc.get_result() | 13 return self._rpc.get_result() |
| 15 | 14 |
| 16 class AppEngineUrlFetcher(object): | 15 class AppEngineUrlFetcher(object): |
| 17 """A wrapper around the App Engine urlfetch module that allows for easy | 16 """A wrapper around the App Engine urlfetch module that allows for easy |
| 18 async fetches. | 17 async fetches. |
| 19 """ | 18 """ |
| 20 def __init__(self, base_path): | 19 def __init__(self, base_path): |
| 21 self._base_path = base_path | 20 self._base_path = base_path |
| 22 | 21 |
| 23 def Fetch(self, url): | 22 def Fetch(self, url): |
| 24 """Fetches a file synchronously. | 23 """Fetches a file synchronously. |
| 25 """ | 24 """ |
| 26 if self._base_path is not None: | 25 if self._base_path is not None: |
| 27 return urlfetch.fetch(self._base_path + '/' + url) | 26 return urlfetch.fetch(self._base_path + '/' + url) |
| 28 else: | 27 else: |
| 29 return urlfetch.fetch(url) | 28 return urlfetch.fetch(url) |
| 30 | 29 |
| 31 def FetchAsync(self, url): | 30 def FetchAsync(self, url): |
| 32 """Fetches a file asynchronously, and returns a Future with the result. | 31 """Fetches a file asynchronously, and returns a Future with the result. |
| 33 """ | 32 """ |
| 34 rpc = urlfetch.create_rpc() | 33 rpc = urlfetch.create_rpc() |
| 35 if self._base_path is not None: | 34 if self._base_path is not None: |
| 36 urlfetch.make_fetch_call(rpc, self._base_path + '/' + url) | 35 urlfetch.make_fetch_call(rpc, self._base_path + '/' + url) |
| 37 else: | 36 else: |
| 38 urlfetch.make_fetch_call(rpc, url) | 37 urlfetch.make_fetch_call(rpc, url) |
| 39 return Future(delegate=_AsyncFetchDelegate(rpc)) | 38 return Future(delegate=_AsyncFetchDelegate(rpc)) |
| OLD | NEW |