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

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

错误: ApiConfigurationError:尝试使用多个不兼容的类来实现版本 v2 的服务 echo。有关如何实现多类 API 的示例,请参阅 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))

@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])

只要版本='v1'(EchoApi)就很好

如果添加了 version='v2' (EchoApi2),则会出现错误

错误代码:ApiConfigurationError:尝试使用多个不兼容的类来实现版本 v2 的服务 echo。有关如何实现多类 API 的示例,请参阅 api() 的文档字符串。

谢谢。

最佳答案

这是创建使用多个类实现的 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

关于python - Google Cloud Endpoints V2 多类 API 错误 App Engine 标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41775746/

相关文章:

java - 使用 Java 的 App Engine 数据存储区中的特定条目正在破坏我的应用程序

用户之间的 Android 谷歌云消息传递

google-app-engine - 在 Google App Engine 中关闭区分大小写的 URL

android - 迁移到 Cloud Endpoints Framework v2,iOS 客户端

java - App Engine RPC 发现文档

google-app-engine - 我的 Google 云端点 API 在 api 资源管理器中不可见

python - 使用 tsfresh 中的 extract_(relevant_)features 时出现(类型转换)错误

python - 访问 Pandas DataFrame 元素中的列表并对列表求和

python - 从 pyuic5 转换的 Python 代码显示 PySide2 UI

python - 更改特定行中的特定值(例如行号 :57) and saving the file with same file name using python