-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Adding app engine request handling samples. #257
Merged
+127
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 @@ | ||
## App Engine Requests Docs Snippets | ||
|
||
These snippets demonstrate various aspects of App Engine Python request handling. | ||
|
||
<!-- auto-doc-link --> | ||
These samples are used on the following documentation page: | ||
|
||
> https://cloud.google.com/appengine/docs/python/requests | ||
|
||
<!-- 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: .* # This regex directs all routes to main.app | ||
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,66 @@ | ||
# Copyright 2015 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 various aspects of App Engine's request | ||
handling. | ||
""" | ||
|
||
import os | ||
import time | ||
|
||
import webapp2 | ||
|
||
|
||
# [START request_timer] | ||
class TimerHandler(webapp2.RequestHandler): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI you can do indented_block="class TimerHandler" to pull in the entire class |
||
def get(self): | ||
from google.appengine.runtime import DeadlineExceededError | ||
|
||
try: | ||
time.sleep(70) | ||
self.response.write('Completed.') | ||
except DeadlineExceededError: | ||
self.response.clear() | ||
self.response.set_status(500) | ||
self.response.out.write( | ||
'The request did not complete in time.') | ||
# [END request_timer] | ||
|
||
|
||
# [START environment] | ||
class PrintEnvironmentHandler(webapp2.RequestHandler): | ||
def get(self): | ||
self.response.headers['Content-Type'] = 'text/plain' | ||
for key, value in os.environ.iteritems(): | ||
self.response.out.write( | ||
"{} = {}\n".format(key, value)) | ||
# [END environment] | ||
|
||
|
||
# [START request_ids] | ||
class RequestIdHandler(webapp2.RequestHandler): | ||
def get(self): | ||
self.response.headers['Content-Type'] = 'text/plain' | ||
request_id = os.environ.get('REQUEST_LOG_ID') | ||
self.response.write( | ||
'REQUEST_LOG_ID={}'.format(request_id)) | ||
# [END request_ids] | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/timer', TimerHandler), | ||
('/environment', PrintEnvironmentHandler), | ||
('/requestid', RequestIdHandler) | ||
], 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,44 @@ | ||
# 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 os | ||
|
||
from google.appengine.runtime import DeadlineExceededError | ||
import main | ||
import mock | ||
import webtest | ||
|
||
|
||
def test_timer(testbed): | ||
app = webtest.TestApp(main.app) | ||
|
||
with mock.patch('main.time.sleep') as sleep_mock: | ||
sleep_mock.side_effect = DeadlineExceededError() | ||
app.get('/timer', status=500) | ||
assert sleep_mock.called | ||
|
||
|
||
def test_environment(testbed): | ||
app = webtest.TestApp(main.app) | ||
response = app.get('/environment') | ||
assert response.headers['Content-Type'] == 'text/plain' | ||
assert response.body | ||
|
||
|
||
def test_request_id(testbed): | ||
app = webtest.TestApp(main.app) | ||
os.environ['REQUEST_LOG_ID'] = '1234' | ||
response = app.get('/requestid') | ||
assert response.headers['Content-Type'] == 'text/plain' | ||
assert '1234' in response.body |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are these auto-doc-link comments I'm seeing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a script that can automatically populate these. I need to re-run it once we're done moving all samples.