Python 具有多个服务类的云端点

Python 具有多个服务类的云端点,python,google-app-engine,google-cloud-endpoints,Python,Google App Engine,Google Cloud Endpoints,我开始使用Google云端点,但在指定多个服务类时遇到了问题。你知道怎么让它工作吗 ApiConfigurationError: Attempting to implement service myservice, version v1, with multiple classes that aren't compatible. See docstring for api() for examples how to implement a multi-class API. 这就是我创建端点服务

我开始使用Google云端点,但在指定多个服务类时遇到了问题。你知道怎么让它工作吗

ApiConfigurationError: Attempting to implement service myservice, version v1, with multiple classes that aren't compatible. See docstring for api() for examples how to implement a multi-class API.
这就是我创建端点服务器的方式

AVAILABLE_SERVICES = [
  FirstService,
  SecondService
]

app = endpoints.api_server(AVAILABLE_SERVICES)
对于每一个服务级别,我都会这样做:

@endpoints.api(name='myservice', version='v1', description='MyService API')
class FirstService(remote.Service):
...

@endpoints.api(name='myservice', version='v1', description='MyService API')
class SecondService(remote.Service):
...
每一个都能完美地分开工作,但我不知道如何在组合它们时让它们工作


非常感谢。

如果我没弄错的话,您应该为每个服务指定不同的名称,这样您就可以访问这两个服务,每个服务都有特定的“地址”


正确的方法是创建
api
对象并使用
集合

api_root = endpoints.api(name='myservice', version='v1', description='MyService API')

@api_root.collection(resource_name='first')
class FirstService(remote.Service):
  ...


@api_root.collection(resource_name='second')
class SecondService(remote.Service):
  ...
其中,将在方法名称前面插入资源名称,以便您可以使用

  @endpoints.method(name='method', ...)
  def MyMethod(self, request):
    ...
而不是

  @endpoints.method(name='first.method', ...)
  def MyMethod(self, request):
    ...
将其放入API服务器:
api_root
对象相当于一个用
endpoints.api
修饰的
remote.Service
类,因此您只需将其包含在
endpoints.api_server
列表中即可。例如:

application = endpoints.api_server([api_root, ...])

对于本地开发,我使用了一个临时解决方案,即禁用异常(我知道我知道…)

在我的sdk中的
google\u appengine/google/appengine/ext/endpoints/api\u backend\u service.py
第97行附近:

        elif service_class != method_class:
          pass
#          raise api_config.ApiConfigurationError(
#              'SPI registered with multiple classes within one '
#              'configuration (%s and %s).  Each call to register_spi should '
#              'only contain the methods from a single class.  Call '
#              'repeatedly for multiple classes.' % (service_class,
#                                                    method_class))
    if service_class is not None:
与此相结合,我使用的构造:

application = endpoints.api_server([FirstService, SecondService, ...])
同样,这在生产中不起作用,您会得到同样的异常。希望这个答案在将来的修复中会过时

确认它现在已经过时(根据1.8.2进行测试)。

如果它是Java

https://developers.google.com/appengine/docs/java/endpoints/multiclass

cloudn再简单不过了。

我成功地部署了两个类中实现的单个api。您可以尝试使用以下代码段(几乎直接从google获得):


我以为它应该在一个api下,但你可能是对的。谢谢:-)不客气!我所拥有的是这样的,每个服务一个类,其中包含此服务的所有方法。:)我做到了这一点,现在唯一的问题是我必须在javascript客户端中分别加载每个API。我想这没什么大不了的,但我宁愿装一次,就这样。bossylobster的答案似乎是我想要的,但似乎并不完全有效。我从来没有像他说的那样使用过“收藏”。我刚刚检查了他的答案,我会尝试一下,并返回任何反馈!:)@bossylobster的答案实际上是正确的,尽管由于mkhatib提到的问题,它目前不起作用(我认为这是一个bug,或者我们都在做完全错误的事情)。这个不应该被标记为已接受…在这种情况下,您将如何创建API服务器?将[api_root]传递到endpoints.api_服务器或传递与上述相同的数组(所有服务)不起作用。我得到的
AttributeError:“\u ApiDecorator”对象对于第一个对象没有属性“api\u info”
。和
ApiConfigurationError:SPI在一个配置中注册了多个类(UsersService和SongsService)。每个对register_spi的调用应该只包含单个类中的方法。重复调用多个类。
第二个类。有什么想法吗?我试过
endpoints.api_服务器([FirstService,SecondService],restricted=False
,我一开始就认为它可以工作,但实际上没有。尝试注册api_根目录时,我也有同样的错误。我确实相信有一个解决方案不是最好的,我创建了一个继承所有服务类的类,我只注册了这个类。我将方法的名称命名为“first.method”.集合不推荐使用api_类instead@bossylobsterDanny只想问你一个问题,如果我的
API类
驻留在不同的文件中,那么如何将
API_root
变量发送给所有这些类,以便它们可以使用相同的
API_root
变量进行注释
https://developers.google.com/appengine/docs/java/endpoints/multiclass
an_api = endpoints.api(name='library', version='v1.0')

@an_api.api_class(resource_name='shelves')
class Shelves(remote.Service):
...

@an_api.api_class(resource_name='books', path='books')
class Books(remote.Service):
...

APPLICATION = endpoints.api_server([an_api],
                               restricted=False)