Skip to content

Commit 5f86065

Browse files
committed
Merge pull request #266 from GoogleCloudPlatform/modules
Add Modules Example
2 parents 5fd8683 + 6718a7e commit 5f86065

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed

appengine/modules/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## App Engine Modules Docs Snippets
2+
3+
This sample application demonstrates how to use Google App Engine's modules API.
4+
5+

appengine/modules/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/modules/backend.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 backend module deployed by backend.yaml and accessed in main.py
17+
"""
18+
19+
import webapp2
20+
21+
22+
class BackendHandler(webapp2.RequestHandler):
23+
def get(self):
24+
self.response.write("hello world")
25+
26+
27+
app = webapp2.WSGIApplication([
28+
('/', BackendHandler),
29+
], debug=True)

appengine/modules/backend.yaml

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

appengine/modules/backend_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 backend
16+
17+
import pytest
18+
import webtest
19+
20+
21+
@pytest.fixture
22+
def app():
23+
return webtest.TestApp(backend.app)
24+
25+
26+
def test_get_module_info(app):
27+
result = app.get('/')
28+
assert result.status_code == 200
29+
assert 'hello world' in result.body

appengine/modules/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 getting information about this
17+
AppEngine modules and accessing other modules in the same project.
18+
"""
19+
20+
import urllib2
21+
# [START modules_import]
22+
from google.appengine.api import modules
23+
# [END modules_import]
24+
import webapp2
25+
26+
27+
class GetModuleInfoHandler(webapp2.RequestHandler):
28+
def get(self):
29+
# [START module_info]
30+
module = modules.get_current_module_name()
31+
instance_id = modules.get_current_instance_id()
32+
self.response.write(
33+
'module_id={}&instance_id={}'.format(module, instance_id))
34+
# [END module_info]
35+
36+
37+
class GetBackendHandler(webapp2.RequestHandler):
38+
def get(self):
39+
# [START access_another_module]
40+
backend_hostname = modules.get_hostname(module='my-backend')
41+
url = "http://{}/".format(backend_hostname)
42+
try:
43+
result = urllib2.urlopen(url).read()
44+
self.response.write('Got response {}'.format(result))
45+
except urllib2.URLError:
46+
pass
47+
# [END access_another_module]
48+
49+
app = webapp2.WSGIApplication([
50+
('/', GetModuleInfoHandler),
51+
('/access_backend', GetBackendHandler),
52+
], debug=True)

appengine/modules/main_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
@mock.patch("main.modules")
28+
def test_get_module_info(modules_mock, app):
29+
modules_mock.get_current_module_name.return_value = "default"
30+
modules_mock.get_current_instance_id.return_value = 1
31+
response = app.get('/')
32+
assert response.status_int == 200
33+
results = response.body.split('&')
34+
assert results[0].split('=')[1] == 'default'
35+
assert results[1].split('=')[1] == '1'
36+
37+
38+
@mock.patch("main.modules")
39+
@mock.patch("urllib2.urlopen")
40+
def test_get_backend(url_open_mock, modules_mock, app):
41+
url_read_mock = mock.Mock(read=mock.Mock(return_value='hello world'))
42+
url_open_mock.return_value = url_read_mock
43+
response = app.get('/access_backend')
44+
45+
assert response.status_int == 200
46+
assert response.body == 'Got response hello world'

0 commit comments

Comments
 (0)