2013-04-23 81 views
5

我开始使用Google Cloud Endpoints,并且在指定多个服务类时遇到问题。任何想法如何得到这个工作?具有多个服务类的云端点

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): 
... 

完全分开这些工作中的每一个,但我不知道如何让他们的工作时,将它们结合起来。

非常感谢。

+0

看这个https://developers.google.com/appengine/docs/python/endpoints/create_api#creating_an_api_implemented_with_multiple_classes] – 2013-12-23 05:23:33

回答

6

正确的方法是创建一个api对象,并使用collection

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对象相当于remote.Service类装饰有endpoints.api,所以你可以简单地将其包含在endpoints.api_server列表。例如:

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

在这种情况下你将如何创建API服务器?将[api_root]传递给endpoints.api_server或传递与上面相同的数组(所有服务)不起作用。我得到'AttributeError:'_ApiDecorator'对象没有第一个属性'api_info'。并且'ApiConfigurationError:SPI在一个配置中注册了多个类(UsersService和SongsService)。每个对register_spi的调用应该只包含来自单个类的方法。第二个是多次打电话给多个班。任何想法? – mkhatib 2013-05-02 05:05:14

+0

我试过'endpoints.api_server([FirstService,SecondService],restricted = False',在第一时间我以为它正在工作,但它没有试图注册api_root我有同样的错误 – 2013-05-02 10:51:39

+0

一个解决方案,我真的相信不是最好的,我创建了一个继承所有服务类的类,我只注册了这个,我把这些方法的名字命名为“first.method”。 – 2013-05-02 11:25:17

2

如果我没有弄错,你应该给每个服务分配不同的名称,这样你就可以同时访问两个服务,每个服务都有特定的“地址”。

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

@endpoints.api(name='myservice_two', version='v1', description='MyService Two API') 
class SecondService(remote.Service): 
... 
+0

我认为这是应该是一个API,但你可能是对的。谢谢:-) – mkhatib 2013-04-23 18:36:57

+0

不客气!我拥有的就是这样,每个服务都有一个类,这个服务的所有方法都在其上。 :) – 2013-04-24 03:41:45

+0

我去了这个机智,我现在唯一的问题是,我将不得不单独加载我的JavaScript客户端中的每个API。我猜想这并不是什么大不了的事,但我宁愿将它们加载一次,就是这样。 bossylobster答案似乎是我想要的,但似乎并不完全正确。 – mkhatib 2013-05-02 05:08:04

0

地方发展我使用一个临时的解决方法,这是禁用异常(我知道我知道...)

在我的SDK中google_appengine/google/appengine/ext/endpoints/api_backend_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中测试)。

0

如果它的Java ...

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

cloudn't容易。

1

我已经设法成功地部署在两个类中实现的单个API。您可以尝试使用下面的代码片段(几乎是直接从谷歌documentation):

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) 
相关问题