Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0982cc9

Browse files
author
Jon Wayne Parrott
committed
Adding sample of how to use requests with GAE standard (GoogleCloudPlatform#320)
Change-Id: I8aa3d95cf88634c1725e7756704aeafe9bdf8708
1 parent eb41f42 commit 0982cc9

File tree

9 files changed

+119
-24
lines changed

9 files changed

+119
-24
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib
File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2016 Google Inc.
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+
from google.appengine.ext import vendor
16+
17+
# Add any libraries installed in the "lib" folder.
18+
vendor.add('lib')

appengine/urlfetch/requests/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# [START app]
16+
import logging
17+
18+
from flask import Flask
19+
20+
# [start imports]
21+
import requests
22+
import requests_toolbelt.adapters.appengine
23+
24+
# Use the App Engine Requests adapter. This makes sure that Requests uses
25+
# URLFetch.
26+
requests_toolbelt.adapters.appengine.monkeypatch()
27+
# [END imports]
28+
29+
app = Flask(__name__)
30+
31+
32+
@app.route('/')
33+
def index():
34+
# [START requests_get]
35+
url = 'http://www.google.com/humans.txt'
36+
response = requests.get(url)
37+
response.raise_for_status()
38+
return response.text
39+
# [END requests_get]
40+
41+
42+
@app.errorhandler(500)
43+
def server_error(e):
44+
logging.exception('An error ocurred during a request.')
45+
return """
46+
An internal error occurred: <pre>{}</pre>
47+
See logs for full stacktrace.
48+
""".format(e), 500
49+
# [END app]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
def test_index(testbed):
17+
# Import main here so that the testbed is active, otherwise none of the
18+
# stubs will be available and it will error.
19+
import main
20+
21+
app = main.app.test_client()
22+
response = app.get('/')
23+
assert response.status_code == 200
24+
assert 'Google' in response.data.decode('utf-8')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask==0.10.1
2+
requests==2.10.0
3+
requests-toolbelt==0.6.0

appengine/urlfetch/snippets/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 renamed to appengine/urlfetch/snippets/main.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class UrlLibFetchHandler(webapp2.RequestHandler):
3535

3636
def get(self):
3737
# [START urllib-get]
38-
url = "http://www.google.com/"
38+
url = 'http://www.google.com/humans.txt'
3939
try:
4040
result = urllib2.urlopen(url)
4141
self.response.write(result.read())
42-
except urllib2.URLError, e:
43-
logging.error("Caught exception fetching url {}".format(e))
42+
except urllib2.URLError:
43+
logging.exception('Caught exception fetching url')
4444
# [END urllib-get]
4545

4646

@@ -49,24 +49,24 @@ class UrlFetchHandler(webapp2.RequestHandler):
4949

5050
def get(self):
5151
# [START urlfetch-get]
52-
url = "http://www.google.com/"
52+
url = 'http://www.google.com/humans.txt'
5353
try:
5454
result = urlfetch.fetch(url)
5555
if result.status_code == 200:
5656
self.response.write(result.content)
5757
else:
5858
self.response.status_code = result.status_code
59-
except urlfetch.Error, e:
60-
logging.error("Caught exception fetching url {}".format(e))
59+
except urlfetch.Error:
60+
logging.exception('Caught exception fetching url')
6161
# [END urlfetch-get]
6262

6363

6464
class UrlPostHandler(webapp2.RequestHandler):
6565
""" Demonstrates an HTTP POST form query using urlfetch"""
6666

6767
form_fields = {
68-
"first_name": "Albert",
69-
"last_name": "Johnson",
68+
'first_name': 'Albert',
69+
'last_name': 'Johnson',
7070
}
7171

7272
def get(self):
@@ -75,13 +75,13 @@ def get(self):
7575
form_data = urllib.urlencode(UrlPostHandler.form_fields)
7676
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
7777
result = urlfetch.fetch(
78-
url="http://localhost:8080/submit_form",
78+
url='http://localhost:8080/submit_form',
7979
payload=form_data,
8080
method=urlfetch.POST,
8181
headers=headers)
8282
self.response.write(result.content)
83-
except urlfetch.Error, e:
84-
logging.error("Caught exception fetching url {}".format(e))
83+
except urlfetch.Error:
84+
logging.exception('Caught exception fetching url')
8585
# [END urlfetch-post]
8686

8787

appengine/urlfetch/main_test.py renamed to appengine/urlfetch/snippets/main_test.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,24 @@
2020

2121

2222
@pytest.fixture
23-
def app():
23+
def app(testbed):
2424
return webtest.TestApp(main.app)
2525

2626

2727
def test_url_lib(app):
2828
response = app.get('/')
29-
assert response.status_int == 200
30-
assert "I'm Feeling Lucky" in response.body
29+
assert 'Google' in response.body
3130

3231

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))
32+
def test_url_fetch(app):
3833
response = app.get('/url_fetch')
39-
assert response.status_int == 200
40-
assert "I'm Feeling Lucky" in response.body
34+
assert 'Google' in response.body
4135

4236

4337
@mock.patch("main.urlfetch")
4438
def test_url_post(urlfetch_mock, app):
4539
urlfetch_mock.fetch = mock.Mock(
46-
return_value=mock.Mock(content="Albert",
40+
return_value=mock.Mock(content='Albert',
4741
status_code=200))
4842
response = app.get('/url_post')
49-
assert response.status_int == 200
50-
assert "Albert" in response.body
43+
assert 'Albert' in response.body

0 commit comments

Comments
 (0)