app.yaml中的google端点url映射

时间:2016-06-20 06:46:39

标签: google-app-engine endpoints app.yaml

我正在关注this example但是尝试将不同的网址映射到不同的脚本,此示例适用于网址只是

的地方
- url: /_ah/spi/.*
    script: test.api

我试图在默认" / _ ah / spi /"之后用不同的路径映射网址。例如。 test,utils。

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

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

即使只有一个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

我正在使用邮递员,网址是

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

1 个答案:

答案 0 :(得分:0)

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

似乎多类API仍在app.yaml中使用单个端点映射,对应于所有API类的集合。来自app.yaml中的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路径表明后端服务无法获取后端配置(从中获取每个特定的类映射),这将符合缺乏这种单一的映射。

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

单个/中央后端enpoints条目文件在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文件只是从其他文件中收集实际的API:

import endpoints

from backend1 import api_1
from backend2 import api_2

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

这些文件包含各自的API实现(从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个API:

enter image description here

其中产生了以下日志:

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