Skip to content

Add Async Sample Code #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions appengine/urlfetch/async/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: .*
script: rpc.app
81 changes: 81 additions & 0 deletions appengine/urlfetch/async/rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import logging

# [START urlfetch-import]
from google.appengine.api import urlfetch
# [END urlfetch-import]
import webapp2


class UrlFetchRpcHandler(webapp2.RequestHandler):
""" Demonstrates an asynchronous HTTP query using urlfetch"""

def get(self):
# [START urlfetch-rpc]
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")

# ... do other things ...
try:
result = rpc.get_result()
if result.status_code == 200:
text = result.content
self.response.write(text)
else:
self.response.status_code = result.status_code
logging.error("Error making RPC request")
except urlfetch.DownloadError:
logging.error("Error fetching URL0")
# [END urlfetch-rpc]


class UrlFetchRpcCallbackHandler(webapp2.RequestHandler):
""" Demonstrates an asynchronous HTTP query with a callback using
urlfetch"""

def get(self):
# [START urlfetch-rpc-callback]
def handle_result(rpc):
result = rpc.get_result()
self.response.write(result.content)
logging.info("Handling RPC in callback: result {}".format(result))

urls = ['http://www.google.com',
'http://www.github.com',
'http://www.travis-ci.org']
rpcs = []
for url in urls:
rpc = urlfetch.create_rpc()
rpc.callback = functools.partial(handle_result, rpc)
urlfetch.make_fetch_call(rpc, url)
rpcs.append(rpc)

# ... do other things ...

# Finish all RPCs, and let callbacks process the results.

for rpc in rpcs:
rpc.wait()

logging.info("Done waiting for RPCs")
# [END urlfetch-rpc-callback]


app = webapp2.WSGIApplication([
('/', UrlFetchRpcHandler),
('/callback', UrlFetchRpcCallbackHandler),
], debug=True)
50 changes: 50 additions & 0 deletions appengine/urlfetch/async/rpc_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import pytest
import rpc
import webtest


@pytest.fixture
def app():
return webtest.TestApp(rpc.app)


@mock.patch("rpc.urlfetch")
def test_url_fetch(urlfetch_mock, app):
get_result_mock = mock.Mock(
return_value=mock.Mock(status_code=200,
content="I'm Feeling Lucky"))
urlfetch_mock.create_rpc = mock.Mock(
return_value=mock.Mock(get_result=get_result_mock))
response = app.get('/')
assert response.status_int == 200
assert "I'm Feeling Lucky" in response.body


@mock.patch("rpc.urlfetch")
def test_url_post(urlfetch_mock, app):
get_result_mock = mock.Mock(
return_value=mock.Mock(status_code=200,
content="I'm Feeling Lucky"))
urlfetch_mock.create_rpc = mock.Mock(
return_value=mock.Mock(get_result=get_result_mock))

rpc_mock = mock.Mock()
urlfetch_mock.create_rpc.return_value = rpc_mock

app.get('/callback')
rpc_mock.wait.assert_called_with()