简体   繁体   English

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

[英]Google Cloud Endpoints V2 multi-class API error App Engine Standard

Error: ApiConfigurationError: Attempting to implement service echo, version v2, with multiple classes that are not compatible. 错误: ApiConfigurationError:尝试使用多个不兼容的类来实现版本2的服务回显。 See docstring for api() for examples how to implement a multi-class API. 有关如何实现多类API的示例,请参见api()的docstring。

Code: 码:

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 if only: version='v1' (EchoApi) 如果只有,则表示良好:version ='v1'(EchoApi)

ERROR if: version='v2' is added (EchoApi2) 如果出现以下错误:添加版本='v2'(EchoApi2)

ERROR CODE: ApiConfigurationError: Attempting to implement service echo, version v2, with multiple classes that are not compatible. 错误代码:ApiConfigurationError:尝试使用多个不兼容的类来实现版本2的服务回显。 See docstring for api() for examples how to implement a multi-class API. 有关如何实现多类API的示例,请参见api()的docstring。

Thank you. 谢谢。

This is the correct format for creating an API implemented with multiple classes: 这是用于创建由多个类实现的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])

The documentation explains it: https://cloud.google.com/endpoints/docs/frameworks/python/create_api#creating_an_api_implemented_with_multiple_classes 该文档对此进行了解释: https : //cloud.google.com/endpoints/docs/frameworks/python/create_api#creating_an_api_implemented_with_multiple_classes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM