Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 72f726b

Browse filesBrowse files
saicheemsJon Wayne Parrott
authored andcommitted
Add Endpoints 1.1 sample (GoogleCloudPlatform#443)
Uses the Endpoints 1.1 Python client with configuration options for service management and service control.
1 parent bc6000f commit 72f726b
Copy full SHA for 72f726b

File tree

Expand file treeCollapse file tree

5 files changed

+197
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+197
-0
lines changed
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
runtime: python27
2+
threadsafe: true
3+
api_version: 1
4+
5+
handlers:
6+
# The endpoints handler must be mapped to /_ah/api.
7+
- url: /_ah/api/.*
8+
script: main.api
9+
10+
libraries:
11+
- name: pycrypto
12+
version: 2.6
13+
14+
# Endpoints 1.1 uses background threads for caching and reporting to service
15+
# management and service control.
16+
manual_scaling:
17+
instances: 1
18+
19+
beta_settings:
20+
use_endpoints_api_management: true
21+
endpoints_swagger_spec_file: echo-v1_swagger.json
22+
23+
env_variables:
24+
# Replace with your endpoints service name.
25+
ENDPOINTS_SERVICE_NAME: your-service.appspot.com
26+
# Replace with the version Id of your uploaded Endpoints service.
27+
ENDPOINTS_SERVICE_VERSION: 2016-08-01r01
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from google.appengine.ext import vendor
2+
3+
# Add any libraries installed in the `lib` folder.
4+
vendor.add('lib')
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"basePath": "/_ah/api",
3+
"consumes": [
4+
"application/json"
5+
],
6+
"definitions": {
7+
"MainEcho": {
8+
"properties": {
9+
"content": {
10+
"type": "string"
11+
}
12+
},
13+
"type": "object"
14+
}
15+
},
16+
"host": null,
17+
"info": {
18+
"title": "echo",
19+
"version": "v1"
20+
},
21+
"paths": {
22+
"/echo/v1/echo": {
23+
"post": {
24+
"operationId": "EchoApi_echo",
25+
"parameters": [],
26+
"responses": {
27+
"200": {
28+
"description": "200_response",
29+
"schema": {
30+
"$ref": "#/definitions/MainEcho"
31+
}
32+
}
33+
}
34+
}
35+
},
36+
"/echo/v1/echo/getUserEmail": {
37+
"get": {
38+
"operationId": "EchoApi_getUserEmail",
39+
"parameters": [],
40+
"responses": {
41+
"200": {
42+
"description": "200_response",
43+
"schema": {
44+
"$ref": "#/definitions/MainEcho"
45+
}
46+
}
47+
}
48+
}
49+
}
50+
},
51+
"produces": [
52+
"application/json"
53+
],
54+
"schemes": [
55+
"https"
56+
],
57+
"swagger": "2.0"
58+
}
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
"""This is a sample Hello World API implemented using Google Cloud
16+
Endpoints."""
17+
18+
# [START imports]
19+
import endpoints
20+
from protorpc import message_types
21+
from protorpc import messages
22+
from protorpc import remote
23+
# [END imports]
24+
25+
26+
# [START messages]
27+
class Echo(messages.Message):
28+
"""A proto Message that contains a simple string field."""
29+
content = messages.StringField(1)
30+
# [END messages]
31+
32+
33+
# [START echo_api]
34+
@endpoints.api(name='echo', version='v1')
35+
class EchoApi(remote.Service):
36+
37+
@endpoints.method(
38+
# This method takes an Echo message.
39+
Echo,
40+
# This method returns an Echo message.
41+
Echo,
42+
path='echo',
43+
http_method='POST',
44+
name='echo')
45+
def echo(self, request):
46+
return Echo(content=request.content)
47+
48+
@endpoints.method(
49+
# This method takes an empty request body.
50+
message_types.VoidMessage,
51+
# This method returns an Echo message.
52+
Echo,
53+
path='echo/getUserEmail',
54+
http_method='GET',
55+
# Require auth tokens to have the following scopes to access this API.
56+
scopes=[endpoints.EMAIL_SCOPE])
57+
def get_user_email(self, request):
58+
user = endpoints.get_current_user()
59+
# If there's no user defined, the request was unauthenticated, so we
60+
# raise 401 Unauthorized.
61+
if not user:
62+
raise endpoints.UnauthorizedException
63+
return Echo(content=user.email())
64+
# [END echo_api]
65+
66+
67+
# [START api_server]
68+
api = endpoints.api_server([EchoApi])
69+
# [END api_server]
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 endpoints
16+
import main
17+
import mock
18+
from protorpc import message_types
19+
import pytest
20+
21+
22+
def test_echo():
23+
api = main.EchoApi()
24+
response = api.echo(main.Echo(content='Hello world!'))
25+
assert 'Hello world!' == response.content
26+
27+
28+
def test_get_user_email():
29+
api = main.EchoApi()
30+
31+
with mock.patch('main.endpoints.get_current_user') as user_mock:
32+
user_mock.return_value = None
33+
with pytest.raises(endpoints.UnauthorizedException):
34+
api.get_user_email(message_types.VoidMessage())
35+
36+
user_mock.return_value = mock.Mock()
37+
user_mock.return_value.email.return_value = 'user@example.com'
38+
response = api.get_user_email(message_types.VoidMessage())
39+
assert 'user@example.com' == response.content

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.