Google Cloud Endpoints V2多类API错误App Engine标准版

时间:2017-01-21 04:29:56

标签: python google-app-engine google-cloud-endpoints

错误: ApiConfigurationError:尝试使用多个不兼容的类实现服务echo v2。有关如何实现多类API的示例,请参阅api()的docstring。

代码:

import logging
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote

class EchoRequest(messages.Message):
   content = messages.StringField(1)

class EchoResponse(messages.Message):
    content = messages.StringField(1)

ECHO_RESOURCE = endpoints.ResourceContainer(
    EchoRequest, n=messages.IntegerField(2, default=1))

@endpoints.api(name='echo', version='v1',description='description')
class EchoApi(remote.Service):

  @endpoints.method(
    # This method takes a ResourceContainer defined above.
    ECHO_RESOURCE,
    # This method returns an Echo message.
    EchoResponse,
    path='echo',
    http_method='POST',
    name='echo')

  def echo(self, request):
    logging.info("echo1"+ str(request.content))
    output_content = ' '.join([request.content] * request.n)
    return EchoResponse(content=output_content)

@endpoints.api(name='echo', version='v2', description='description2')
class EchoApi2(remote.Service):

  @endpoints.method(
    # This method takes a ResourceContainer defined above.
    ECHO_RESOURCE,
    # This method returns an Echo message.
    EchoResponse,
    path='echo',
    http_method='POST',
    name='echo')

  def echo(self, request):
    logging.info("echo2" + str(request.content))
    output_content = ' '.join([request.content] * request.n)
    return EchoResponse(content=output_content)

api = endpoints.api_server([EchoApi, EchoApi2])

仅限GOOD:版本='v1'(EchoApi)

错误如果:添加版本='v2'(EchoApi2)

错误代码:ApiConfigurationError:尝试实现具有多个不兼容的类的服务回显版本v2。有关如何实现多类API的示例,请参阅api()的docstring。

谢谢。

1 个答案:

答案 0 :(得分:2)

这是创建使用多个类实现的API的正确格式:

import logging
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote

class EchoRequest(messages.Message):
   content = messages.StringField(1)

class EchoResponse(messages.Message):
    content = messages.StringField(1)

ECHO_RESOURCE = endpoints.ResourceContainer(
    EchoRequest, n=messages.IntegerField(2, default=1))

echo_collection = endpoints.api(name='echo', version='v1', description='description')

@echo_collection.api_class(resource_name='echo1')
class EchoApi1(remote.Service):

  @endpoints.method(
    # This method takes a ResourceContainer defined above.
    ECHO_RESOURCE,
    # This method returns an Echo message.
    EchoResponse,
    path='echo',
    http_method='POST',
    name='echo')

  def echo(self, request):
    logging.info("echo1"+ str(request.content))
    output_content = ' '.join([request.content] * request.n)
    return EchoResponse(content=output_content)

@echo_collection.api_class(resource_name='echo2')
class EchoApi2(remote.Service):

  @endpoints.method(
    # This method takes a ResourceContainer defined above.
    ECHO_RESOURCE,
    # This method returns an Echo message.
    EchoResponse,
    path='echo',
    http_method='POST',
    name='echo')

  def echo(self, request):
    logging.info("echo2" + str(request.content))
    output_content = ' '.join([request.content] * request.n)
    return EchoResponse(content=output_content)

api = endpoints.api_server([echo_collection])

文档解释了它:https://cloud.google.com/endpoints/docs/frameworks/python/create_api#creating_an_api_implemented_with_multiple_classes