google 端点 url 映射到 app.yaml

google endpoint url mapping in app.yaml

我正在关注 this example 但试图将不同的 url 映射到不同的脚本,此示例在 url 只是

的情况下有效
- url: /_ah/spi/.*
    script: test.api

我正在尝试将 url 映射到默认“/_ah/spi/”之后的不同路径。例如。测试,实用程序。

- url: /_ah/spi/test/.*
    script: test.api

- url: /_ah/spi/utils/.*
    script: utils.api

即使只有一个 url 处理程序,这也不起作用。我收到找不到路径错误。

我找不到任何这样的例子,想知道是否可以将 url 映射与 google 端点一起使用。

我正在我的本地开发环境中尝试这个。

我得到的错误是

INFO     2016-06-21 10:48:56,273 module.py:788] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 404 -
INFO     2016-06-21 10:48:56,274 module.py:788] default: "GET /_ah/api/test/test/v1/simple-test HTTP/1.1" 500 60

test.py 中的端点代码是

class Message(messages.Message):
    name = messages.StringField(1)

@endpoints.api(name='test', version='v1')
class TestApi(remote.Service):

    """ Simple api test """
    @endpoints.method(
        message_types.VoidMessage,
        Message,
        path='simple-test',
        http_method='GET',
        name='simple-test')
    def simple_test(self, unused_request):
        print("simple-test")
        return Message(name='test simple-test')

#Start the api server
api = endpoints.api_server([TestApi])

app.yaml 中的配置是

handlers:
- url: /_ah/spi/test/.*
    script: test.api

我正在使用邮递员,url 是

http://localhost:58080/_ah/api/test/test/v1/simple-test

免责声明-我还没有使用后端服务,答案主要基于文档,即Creating an API Implemented with Multiple Classes

貌似multi-class API还是在app.yaml中使用了单一的endpoints mapping,对应所有API class的集合]是的。来自 app.yaml file in the Github example:

handlers:
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: main.api

您收到的第一个错误中的 /_ah/spi/BackendService.getApiConfigs 路径表明后端服务无法获取后端配置(每个特定的 class 映射将从中获取),这将符合缺少这个单一的映射。

可能能够将您的后端class拆分为多个文件,您可以使用以下方法。

single/central 后端指定入口文件在 app.yaml:

中指定
# The endpoints handler must be mapped to /_ah/spi.
# Apps send requests to /_ah/api, but the endpoints service handles mapping
# those requests to /_ah/spi.
- url: /_ah/spi/.*
  script: backend.api

相应的 backend.py 文件只是从其他文件中收集实际的 APIs:

import endpoints

from backend1 import api_1
from backend2 import api_2

api = endpoints.api_server([api_1, api_2])

并且这些文件包含相应的 API 实现(copy/paste 来自 github 示例并为快速测试稍作修改):

backend1.py 文件:

import endpoints
from protorpc import messages
from protorpc import remote


class Request(messages.Message):
    message = messages.StringField(1)


class Response(messages.Message):
    message = messages.StringField(1)


api_1 = endpoints.api(name='library1', version='v1.0')


@api_1.api_class(resource_name='shelves')
class Shelves1(remote.Service):

    @endpoints.method(Request, Response, path='list')
    def list(self, request):
        return Response()


# [START books]
@api_1.api_class(resource_name='books', path='books')
class Books1(remote.Service):

    @endpoints.method(Request, Response, path='bookmark')
    def bookmark(self, request):
        return Response()

backend2.py 文件:

import endpoints
from protorpc import messages
from protorpc import remote


class Request(messages.Message):
    message = messages.StringField(1)


class Response(messages.Message):
    message = messages.StringField(1)


api_2 = endpoints.api(name='library2', version='v2.0')


@api_2.api_class(resource_name='shelves')
class Shelves2(remote.Service):

    @endpoints.method(Request, Response, path='list')
    def list(self, request):
        return Response()


# [START books]
@api_2.api_class(resource_name='books', path='books')
class Books2(remote.Service):

    @endpoints.method(Request, Response, path='bookmark')
    def bookmark(self, request):
        return Response()

通过上面的代码,我能够在开发服务器 API 资源管理器中看到 2 APIs:

产生了以下日志:

INFO     2016-06-21 15:52:24,945 module.py:812] default: "GET /_ah/api/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.pEvOgGtQ_zc.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCNv5bf7XiPy_xHPlP3A3K73m3QR2A HTTP/1.1" 200 8015
INFO     2016-06-21 15:52:25,058 module.py:812] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 5459
INFO     2016-06-21 15:52:25,070 module.py:812] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 5459
INFO     2016-06-21 15:52:25,470 module.py:812] default: "GET /_ah/api/discovery/v1/apis HTTP/1.1" 200 1516
INFO     2016-06-21 15:52:25,508 module.py:812] default: "GET /_ah/api/discovery/v1/apis HTTP/1.1" 200 1516
INFO     2016-06-21 15:52:25,813 module.py:812] default: "GET /_ah/api/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.pEvOgGtQ_zc.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCNv5bf7XiPy_xHPlP3A3K73m3QR2A HTTP/1.1" 200 8015
INFO     2016-06-21 15:52:25,937 module.py:812] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 5459
INFO     2016-06-21 15:52:25,941 module.py:812] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 5459
INFO     2016-06-21 15:52:25,942 module.py:812] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 5459
INFO     2016-06-21 15:52:26,183 module.py:812] default: "GET /_ah/api/discovery/v1/apis/library2/v2.0/rest HTTP/1.1" 200 3277
INFO     2016-06-21 15:52:26,203 module.py:812] default: "GET /_ah/api/discovery/v1/apis/library1/v1.0/rest HTTP/1.1" 200 3277
INFO     2016-06-21 15:52:26,210 module.py:812] default: "GET /_ah/api/discovery/v1/apis/library2/v1.0/rest HTTP/1.1" 200 3277