Skip to content
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

Add Endpoints 1.1 sample #443

Merged
merged 1 commit into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions appengine/standard/endpoints-v1.1/backend/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
runtime: python27
threadsafe: true
api_version: 1

handlers:
# The endpoints handler must be mapped to /_ah/api.
- url: /_ah/api/.*
script: main.api

libraries:
- name: pycrypto
version: 2.6

# Endpoints 1.1 uses background threads for caching and reporting to service
# management and service control.
manual_scaling:
instances: 1

beta_settings:
use_endpoints_api_management: true
endpoints_swagger_spec_file: echo-v1_swagger.json

env_variables:
# Replace with your endpoints service name.
ENDPOINTS_SERVICE_NAME: your-service.appspot.com
# Replace with the version Id of your uploaded Endpoints service.
ENDPOINTS_SERVICE_VERSION: 2016-08-01r01
4 changes: 4 additions & 0 deletions appengine/standard/endpoints-v1.1/backend/appengine_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from google.appengine.ext import vendor

# Add any libraries installed in the `lib` folder.
vendor.add('lib')
58 changes: 58 additions & 0 deletions appengine/standard/endpoints-v1.1/backend/echo-v1_swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"basePath": "/_ah/api",
"consumes": [
"application/json"
],
"definitions": {
"MainEcho": {
"properties": {
"content": {
"type": "string"
}
},
"type": "object"
}
},
"host": null,
"info": {
"title": "echo",
"version": "v1"
},
"paths": {
"/echo/v1/echo": {
"post": {
"operationId": "EchoApi_echo",
"parameters": [],
"responses": {
"200": {
"description": "200_response",
"schema": {
"$ref": "#/definitions/MainEcho"
}
}
}
}
},
"/echo/v1/echo/getUserEmail": {
"get": {
"operationId": "EchoApi_getUserEmail",
"parameters": [],
"responses": {
"200": {
"description": "200_response",
"schema": {
"$ref": "#/definitions/MainEcho"
}
}
}
}
}
},
"produces": [
"application/json"
],
"schemes": [
"https"
],
"swagger": "2.0"
}
69 changes: 69 additions & 0 deletions appengine/standard/endpoints-v1.1/backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.

"""This is a sample Hello World API implemented using Google Cloud
Endpoints."""

# [START imports]
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote
# [END imports]


# [START messages]
class Echo(messages.Message):
"""A proto Message that contains a simple string field."""
content = messages.StringField(1)
# [END messages]


# [START echo_api]
@endpoints.api(name='echo', version='v1')
class EchoApi(remote.Service):

@endpoints.method(
# This method takes an Echo message.
Echo,
# This method returns an Echo message.
Echo,
path='echo',
http_method='POST',
name='echo')
def echo(self, request):
return Echo(content=request.content)

@endpoints.method(
# This method takes an empty request body.
message_types.VoidMessage,
# This method returns an Echo message.
Echo,
path='echo/getUserEmail',
http_method='GET',
# Require auth tokens to have the following scopes to access this API.
scopes=[endpoints.EMAIL_SCOPE])
def get_user_email(self, request):
user = endpoints.get_current_user()
# If there's no user defined, the request was unauthenticated, so we
# raise 401 Unauthorized.
if not user:
raise endpoints.UnauthorizedException
return Echo(content=user.email())
# [END echo_api]


# [START api_server]
api = endpoints.api_server([EchoApi])
# [END api_server]
39 changes: 39 additions & 0 deletions appengine/standard/endpoints-v1.1/backend/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 endpoints
import main
import mock
from protorpc import message_types
import pytest


def test_echo():
api = main.EchoApi()
response = api.echo(main.Echo(content='Hello world!'))
assert 'Hello world!' == response.content


def test_get_user_email():
api = main.EchoApi()

with mock.patch('main.endpoints.get_current_user') as user_mock:
user_mock.return_value = None
with pytest.raises(endpoints.UnauthorizedException):
api.get_user_email(message_types.VoidMessage())

user_mock.return_value = mock.Mock()
user_mock.return_value.email.return_value = 'user@example.com'
response = api.get_user_email(message_types.VoidMessage())
assert 'user@example.com' == response.content