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