| 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 import base64 | 5 import base64 |
| 6 import posixpath |
| 6 | 7 |
| 7 from appengine_wrappers import urlfetch | 8 from appengine_wrappers import urlfetch |
| 8 from future import Future | 9 from future import Future |
| 9 | 10 |
| 11 |
| 10 class _AsyncFetchDelegate(object): | 12 class _AsyncFetchDelegate(object): |
| 11 def __init__(self, rpc): | 13 def __init__(self, rpc): |
| 12 self._rpc = rpc | 14 self._rpc = rpc |
| 13 | 15 |
| 14 def Get(self): | 16 def Get(self): |
| 15 return self._rpc.get_result() | 17 return self._rpc.get_result() |
| 16 | 18 |
| 19 |
| 17 def _MakeHeaders(username, password): | 20 def _MakeHeaders(username, password): |
| 18 headers = { 'Cache-Control': 'max-age=0' } | 21 headers = { 'Cache-Control': 'max-age=0' } |
| 19 if username is not None and password is not None: | 22 if username is not None and password is not None: |
| 20 headers['Authorization'] = 'Basic %s' % base64.encodestring( | 23 headers['Authorization'] = 'Basic %s' % base64.encodestring( |
| 21 '%s:%s' % (username, password)) | 24 '%s:%s' % (username, password)) |
| 22 return headers | 25 return headers |
| 23 | 26 |
| 27 |
| 24 class AppEngineUrlFetcher(object): | 28 class AppEngineUrlFetcher(object): |
| 25 """A wrapper around the App Engine urlfetch module that allows for easy | 29 """A wrapper around the App Engine urlfetch module that allows for easy |
| 26 async fetches. | 30 async fetches. |
| 27 """ | 31 """ |
| 28 def __init__(self, base_path=None): | 32 def __init__(self, base_path=None): |
| 33 assert base_path is None or not base_path.endswith('/') |
| 29 self._base_path = base_path | 34 self._base_path = base_path |
| 30 | 35 |
| 31 def Fetch(self, url, username=None, password=None): | 36 def Fetch(self, url, username=None, password=None): |
| 32 """Fetches a file synchronously. | 37 """Fetches a file synchronously. |
| 33 """ | 38 """ |
| 34 if self._base_path is not None: | 39 return urlfetch.fetch(self._FromBasePath(url), |
| 35 url = '%s/%s' % (self._base_path, url) | 40 headers=_MakeHeaders(username, password)) |
| 36 return urlfetch.fetch(url, headers=_MakeHeaders(username, password)) | |
| 37 | 41 |
| 38 def FetchAsync(self, url, username=None, password=None): | 42 def FetchAsync(self, url, username=None, password=None): |
| 39 """Fetches a file asynchronously, and returns a Future with the result. | 43 """Fetches a file asynchronously, and returns a Future with the result. |
| 40 """ | 44 """ |
| 45 rpc = urlfetch.create_rpc() |
| 46 urlfetch.make_fetch_call(rpc, |
| 47 self._FromBasePath(url), |
| 48 headers=_MakeHeaders(username, password)) |
| 49 return Future(delegate=_AsyncFetchDelegate(rpc)) |
| 50 |
| 51 def _FromBasePath(self, url): |
| 41 if self._base_path is not None: | 52 if self._base_path is not None: |
| 42 url = '%s/%s' % (self._base_path, url) | 53 url = posixpath.join(self._base_path, url) if url else self._base_path |
| 43 rpc = urlfetch.create_rpc() | 54 return url |
| 44 urlfetch.make_fetch_call(rpc, url, headers=_MakeHeaders(username, password)) | |
| 45 return Future(delegate=_AsyncFetchDelegate(rpc)) | |
| OLD | NEW |