Skip to content

Commit 799481e

Browse files
author
Bill Prin
committed
Add Async Sample Code
1 parent 37f55e0 commit 799481e

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

appengine/urlfetch/async/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
- url: .*
7+
script: rpc.app

appengine/urlfetch/async/rpc.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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)

appengine/urlfetch/async/rpc_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 mock
16+
import pytest
17+
import rpc
18+
import webtest
19+
20+
21+
@pytest.fixture
22+
def app():
23+
return webtest.TestApp(rpc.app)
24+
25+
26+
@mock.patch("rpc.urlfetch")
27+
def test_url_fetch(urlfetch_mock, app):
28+
get_result_mock = mock.Mock(
29+
return_value=mock.Mock(status_code=200,
30+
content="I'm Feeling Lucky"))
31+
urlfetch_mock.create_rpc = mock.Mock(
32+
return_value=mock.Mock(get_result=get_result_mock))
33+
response = app.get('/')
34+
assert response.status_int == 200
35+
assert "I'm Feeling Lucky" in response.body
36+
37+
38+
@mock.patch("rpc.urlfetch")
39+
def test_url_post(urlfetch_mock, app):
40+
get_result_mock = mock.Mock(
41+
return_value=mock.Mock(status_code=200,
42+
content="I'm Feeling Lucky"))
43+
urlfetch_mock.create_rpc = mock.Mock(
44+
return_value=mock.Mock(get_result=get_result_mock))
45+
46+
rpc_mock = mock.Mock()
47+
urlfetch_mock.create_rpc.return_value = rpc_mock
48+
49+
app.get('/callback')
50+
rpc_mock.wait.assert_called_with()

0 commit comments

Comments
 (0)