diff --git a/appengine/standard/endpoints-v1.1/backend/app.yaml b/appengine/standard/endpoints-v1.1/backend/app.yaml new file mode 100644 index 000000000000..0dd9abd9ce05 --- /dev/null +++ b/appengine/standard/endpoints-v1.1/backend/app.yaml @@ -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 diff --git a/appengine/standard/endpoints-v1.1/backend/appengine_config.py b/appengine/standard/endpoints-v1.1/backend/appengine_config.py new file mode 100644 index 000000000000..3bb4ea6e3f1a --- /dev/null +++ b/appengine/standard/endpoints-v1.1/backend/appengine_config.py @@ -0,0 +1,4 @@ +from google.appengine.ext import vendor + +# Add any libraries installed in the `lib` folder. +vendor.add('lib') diff --git a/appengine/standard/endpoints-v1.1/backend/echo-v1_swagger.json b/appengine/standard/endpoints-v1.1/backend/echo-v1_swagger.json new file mode 100644 index 000000000000..260fb33b06da --- /dev/null +++ b/appengine/standard/endpoints-v1.1/backend/echo-v1_swagger.json @@ -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" +} \ No newline at end of file diff --git a/appengine/standard/endpoints-v1.1/backend/main.py b/appengine/standard/endpoints-v1.1/backend/main.py new file mode 100644 index 000000000000..2348e1f6edc8 --- /dev/null +++ b/appengine/standard/endpoints-v1.1/backend/main.py @@ -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] diff --git a/appengine/standard/endpoints-v1.1/backend/main_test.py b/appengine/standard/endpoints-v1.1/backend/main_test.py new file mode 100644 index 000000000000..55a5b836a322 --- /dev/null +++ b/appengine/standard/endpoints-v1.1/backend/main_test.py @@ -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