|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import functools |
| 16 | +import logging |
| 17 | + |
| 18 | +# [START urlfetch-import] |
| 19 | +from google.appengine.api import urlfetch |
| 20 | +# [END urlfetch-import] |
| 21 | +import webapp2 |
| 22 | + |
| 23 | + |
| 24 | +class UrlFetchRpcHandler(webapp2.RequestHandler): |
| 25 | + """ Demonstrates an asynchronous HTTP query using urlfetch""" |
| 26 | + |
| 27 | + def get(self): |
| 28 | + # [START urlfetch-rpc] |
| 29 | + rpc = urlfetch.create_rpc() |
| 30 | + urlfetch.make_fetch_call(rpc, "http://www.google.com/") |
| 31 | + |
| 32 | + # ... do other things ... |
| 33 | + try: |
| 34 | + result = rpc.get_result() |
| 35 | + if result.status_code == 200: |
| 36 | + text = result.content |
| 37 | + self.response.write(text) |
| 38 | + else: |
| 39 | + self.response.status_code = result.status_code |
| 40 | + logging.error("Error making RPC request") |
| 41 | + except urlfetch.DownloadError: |
| 42 | + logging.error("Error fetching URL0") |
| 43 | + # [END urlfetch-rpc] |
| 44 | + |
| 45 | + |
| 46 | +class UrlFetchRpcCallbackHandler(webapp2.RequestHandler): |
| 47 | + """ Demonstrates an asynchronous HTTP query with a callback using |
| 48 | + urlfetch""" |
| 49 | + |
| 50 | + def get(self): |
| 51 | + # [START urlfetch-rpc-callback] |
| 52 | + def handle_result(rpc): |
| 53 | + result = rpc.get_result() |
| 54 | + self.response.write(result.content) |
| 55 | + logging.info("Handling RPC in callback: result {}".format(result)) |
| 56 | + |
| 57 | + urls = ['http://www.google.com', |
| 58 | + 'http://www.github.com', |
| 59 | + 'http://www.travis-ci.org'] |
| 60 | + rpcs = [] |
| 61 | + for url in urls: |
| 62 | + rpc = urlfetch.create_rpc() |
| 63 | + rpc.callback = functools.partial(handle_result, rpc) |
| 64 | + urlfetch.make_fetch_call(rpc, url) |
| 65 | + rpcs.append(rpc) |
| 66 | + |
| 67 | + # ... do other things ... |
| 68 | + |
| 69 | + # Finish all RPCs, and let callbacks process the results. |
| 70 | + |
| 71 | + for rpc in rpcs: |
| 72 | + rpc.wait() |
| 73 | + |
| 74 | + logging.info("Done waiting for RPCs") |
| 75 | + # [END urlfetch-rpc-callback] |
| 76 | + |
| 77 | + |
| 78 | +app = webapp2.WSGIApplication([ |
| 79 | + ('/', UrlFetchRpcHandler), |
| 80 | + ('/callback', UrlFetchRpcCallbackHandler), |
| 81 | +], debug=True) |
0 commit comments