2013-04-23 78 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) 
相關問題