Skip to content

Commit 176ec8c

Browse files
committed
Merge pull request #267 from GoogleCloudPlatform/urlfetch
Add UrlFetch Example
2 parents c998e4b + 7193c29 commit 176ec8c

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

appengine/urlfetch/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## App Engine UrlFetch Docs Snippets
2+
3+
This sample application demonstrates different ways to request a URL
4+
on App Engine
5+
6+
7+
<!-- auto-doc-link --><!-- end-auto-doc-link -->

appengine/urlfetch/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: main.app

appengine/urlfetch/main.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
"""
16+
Sample application that demonstrates different ways of fetching
17+
URLS on App Engine
18+
"""
19+
20+
import logging
21+
# [START url-imports]
22+
import urllib
23+
import urllib2
24+
25+
from google.appengine.api import urlfetch
26+
import webapp2
27+
# [END url-imports]
28+
29+
30+
class UrlLibFetchHandler(webapp2.RequestHandler):
31+
""" Demonstrates an HTTP query using urllib2"""
32+
33+
def get(self):
34+
# [START urllib-get]
35+
url = "http://www.google.com/"
36+
try:
37+
result = urllib2.urlopen(url)
38+
self.response.write(result.read())
39+
except urllib2.URLError, e:
40+
logging.error("Caught exception fetching url {}".format(e))
41+
# [END urllib-get]
42+
43+
44+
class UrlFetchHandler(webapp2.RequestHandler):
45+
""" Demonstrates an HTTP query using urlfetch"""
46+
47+
def get(self):
48+
# [START urlfetch-get]
49+
url = "http://www.googleadsfasdf.com/"
50+
try:
51+
result = urlfetch.fetch(url)
52+
if result.status_code == 200:
53+
self.response.write(result.content)
54+
else:
55+
self.response.status_code = result.status_code
56+
except urlfetch.Error, e:
57+
logging.error("Caught exception fetching url {}".format(e))
58+
# [END urlfetch-get]
59+
60+
61+
class UrlPostHandler(webapp2.RequestHandler):
62+
""" Demonstrates an HTTP POST form query using urlfetch"""
63+
64+
form_fields = {
65+
"first_name": "Albert",
66+
"last_name": "Johnson",
67+
}
68+
69+
def get(self):
70+
# [START urlfetch-post]
71+
try:
72+
form_data = urllib.urlencode(UrlPostHandler.form_fields)
73+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
74+
result = urlfetch.fetch(
75+
url="http://localhost:8080/submit_form",
76+
payload=form_data,
77+
method=urlfetch.POST,
78+
headers=headers)
79+
self.response.write(result.content)
80+
except urlfetch.Error, e:
81+
logging.error("Caught exception fetching url {}".format(e))
82+
# [END urlfetch-post]
83+
84+
85+
class SubmitHandler(webapp2.RequestHandler):
86+
""" Handler that receives UrlPostHandler POST request"""
87+
88+
def post(self):
89+
self.response.out.write((self.request.get('first_name')))
90+
91+
92+
app = webapp2.WSGIApplication([
93+
('/', UrlLibFetchHandler),
94+
('/url_fetch', UrlFetchHandler),
95+
('/url_post', UrlPostHandler),
96+
('/submit_form', SubmitHandler)
97+
], debug=True)

appengine/urlfetch/main_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 main
16+
17+
import mock
18+
import pytest
19+
import webtest
20+
21+
22+
@pytest.fixture
23+
def app():
24+
return webtest.TestApp(main.app)
25+
26+
27+
def test_url_lib(app):
28+
response = app.get('/')
29+
assert response.status_int == 200
30+
assert "I'm Feeling Lucky" in response.body
31+
32+
33+
@mock.patch("main.urlfetch")
34+
def test_url_fetch(urlfetch_mock, app):
35+
urlfetch_mock.fetch = mock.Mock(
36+
return_value=mock.Mock(content="I'm Feeling Lucky",
37+
status_code=200))
38+
response = app.get('/url_fetch')
39+
assert response.status_int == 200
40+
assert "I'm Feeling Lucky" in response.body
41+
42+
43+
@mock.patch("main.urlfetch")
44+
def test_url_post(urlfetch_mock, app):
45+
urlfetch_mock.fetch = mock.Mock(
46+
return_value=mock.Mock(content="Albert",
47+
status_code=200))
48+
response = app.get('/url_post')
49+
assert response.status_int == 200
50+
assert "Albert" in response.body

0 commit comments

Comments
 (0)