-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bill Prin
committed
Apr 20, 2016
1 parent
c998e4b
commit 00b9204
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## App Engine UrlFetch Docs Snippets | ||
|
||
This sample application demonstrates different ways to request a URL | ||
on App Engine | ||
|
||
|
||
<!-- auto-doc-link --><!-- end-auto-doc-link --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: yes | ||
|
||
handlers: | ||
- url: .* | ||
script: main.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 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. | ||
|
||
""" | ||
Sample application that demonstrates different ways of fetching | ||
URLS on App Engine | ||
""" | ||
|
||
import urllib | ||
import urllib2 | ||
import logging | ||
import webapp2 | ||
|
||
from google.appengine.api import urlfetch | ||
from google.appengine.api import urlfetch_errors | ||
|
||
|
||
class UrlLibFetchHandler(webapp2.RequestHandler): | ||
""" Demonstrates an HTTP query using urllib2""" | ||
|
||
def get(self): | ||
url = "http://www.google.com/" | ||
try: | ||
result = urllib2.urlopen(url) | ||
self.response.write(result.read()) | ||
except urllib2.URLError, e: | ||
logging.error("Caught exception fetching url {}".format(e)) | ||
|
||
|
||
class UrlFetchHandler(webapp2.RequestHandler): | ||
""" Demonstrates an HTTP query using urlfetch""" | ||
|
||
def get(self): | ||
url = "http://www.googleadsfasdf.com/" | ||
try: | ||
result = urlfetch.fetch(url) | ||
if result.status_code == 200: | ||
self.response.write(result.content) | ||
else: | ||
self.response.status_code = result.status_code | ||
except urlfetch.Error, e: | ||
logging.error("Caught exception fetching url {}".format(e)) | ||
|
||
|
||
class UrlPostHandler(webapp2.RequestHandler): | ||
""" Demonstrates an HTTP POST form query using urlfetch""" | ||
|
||
form_fields = { | ||
"first_name": "Albert", | ||
"last_name": "Johnson", | ||
} | ||
|
||
def get(self): | ||
try: | ||
form_data = urllib.urlencode(UrlPostHandler.form_fields) | ||
headers = {'Content-Type': 'application/x-www-form-urlencoded'} | ||
result = urlfetch.fetch( | ||
url="http://localhost:8080/submit_form", | ||
payload=form_data, | ||
method=urlfetch.POST, | ||
headers=headers) | ||
self.response.write(result.content) | ||
except urlfetch.Error, e: | ||
logging.error("Caught exception fetching url {}".format(e)) | ||
|
||
|
||
class SubmitHandler(webapp2.RequestHandler): | ||
""" Handler that receives UrlPostHandler POST request""" | ||
|
||
def post(self): | ||
self.response.out.write((self.request.get('first_name'))) | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', UrlLibFetchHandler), | ||
('/url_fetch', UrlFetchHandler), | ||
('/url_post', UrlPostHandler), | ||
('/submit_form', SubmitHandler) | ||
], debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 main | ||
|
||
import mock | ||
import pytest | ||
import webtest | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
return webtest.TestApp(main.app) | ||
|
||
|
||
def test_url_lib(app): | ||
response = app.get('/') | ||
assert response.status_int == 200 | ||
assert "I'm Feeling Lucky" in response.body | ||
|
||
|
||
@mock.patch("main.urlfetch") | ||
def test_url_fetch(urlfetch_mock, app): | ||
urlfetch_mock.fetch = mock.Mock( | ||
return_value=mock.Mock(content="I'm Feeling Lucky", | ||
status_code=200)) | ||
response = app.get('/url_fetch') | ||
assert response.status_int == 200 | ||
assert "I'm Feeling Lucky" in response.body | ||
|
||
|
||
@mock.patch("main.urlfetch") | ||
def test_url_post(urlfetch_mock, app): | ||
urlfetch_mock.fetch = mock.Mock( | ||
return_value=mock.Mock(content="Albert", | ||
status_code=200)) | ||
response = app.get('/url_post') | ||
assert response.status_int == 200 | ||
assert "Albert" in response.body |