blob: 3695fb4e81dae5f26366bce6a493c933ad856073 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from appengine_wrappers import urlfetch
from future import Future
class _AsyncFetchDelegate(object):
def __init__(self, rpc):
self._rpc = rpc
def Get(self):
self._rpc.wait()
return self._rpc.get_result()
class AppEngineUrlFetcher(object):
"""A wrapper around the App Engine urlfetch module that allows for easy
async fetches.
"""
def __init__(self, base_path):
self._base_path = base_path
def Fetch(self, url):
"""Fetches a file synchronously.
"""
return urlfetch.fetch(self._base_path + '/' + url)
def FetchAsync(self, url):
"""Fetches a file asynchronously, and returns a Future with the result.
"""
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, self._base_path + '/' + url)
return Future(delegate=_AsyncFetchDelegate(rpc))
|