[英]Google App Engine Api and Endpoints versioning
我在我的應用程序中找到正確的方法來處理多個版本的remote.Service api時遇到了一些麻煩。
class MyService(Service):
@endpoints.method(
endpoints.ResourceContainer(
something=protorpc.messages.StringField(1, required=True),
),
message_types.VoidMessage,
)
def do_stuff(self, request):
... implement do_stuff ...
class MyBetterService(MyService):
@endpoints.method(
endpoints.ResourceContainer(
some_other_name=protorpc.messages.StringField(1, required=True),
),
message_types.VoidMessage,
)
def do_stuff(self, request):
# ...other way of doing stuff
return message_types.VoidMessage()
在嘗試創建庫時出現此錯誤:
protorpc.remote.ServiceDefinitionError: Do not use method decorator when overloading remote method do_stuff on service MyBetterService.
在下一版API中是否有覆蓋方法的方法?
重寫方法可能需要其他請求參數?
是否有可能在現有API中只添加一個不同版本的端點?
編寫端點服務類,一旦定義了公共接口方法,就不能在子類中更改它們。 通常,您不應該將類的新API版本定義為子類,除非它精確地超級設置超類的接口。
在新版本是超集的情況下,您可以使用重新定義的接口方法,這將自動繼承父方法的屬性。 例如:
class MyService(Service):
@endpoints.method(
endpoints.ResourceContainer(
something=protorpc.messages.StringField(1, required=True),
),
message_types.VoidMessage,
)
def do_stuff(self, request):
... implement do_stuff ...
class MyBetterService(MyService):
def do_stuff(self, request):
# ...other way of doing stuff
return message_types.VoidMessage()
@endpoints.method(
endpoints.ResourceContainer(
something=protorpc.messages.IntegerField(1, required=True),
),
message_types.VoidMessage,
)
def do_more_stuff(self, request):
... implement do_more_stuff ...
無法更改do_stuff()
的輸入類型。
在實踐中,新的API版本應該被視為與新API相同並且具有獨立的Service類定義。 將API真正視為一個接口。 雖然兩個類不應該共享具有常見API方法定義的基類,但這並不意味着兩個類現在可以共享一組通用的函數類。
當我構建服務時,我已經將API版本實現為單獨的類,即使我必須復制許多方法簽名。 但是,在服務下面,我實現了一個對象系統,它完全獨立於接口和API消息類型執行相同的操作。 這允許兩個API版本共享實現的重要部分。
例如:
from mysystem import MyImplementation
class MyService(Service):
@endpoints.method(
endpoints.ResourceContainer(
something=protorpc.messages.StringField(1, required=True),
),
message_types.VoidMessage,
)
def do_stuff(self, request):
MyImplementation.do_stuff(request.something)
class MyBetterService(Service):
@endpoints.method(
endpoints.ResourceContainer(
something=protorpc.messages.IntegerField(1, required=True),
),
message_types.VoidMessage,
)
def do_stuff(self, request):
MyImplementation.do_stuff(self.lookup_string(request.something))
在這個模型中,我認為API負責在服務接口和實際底層系統之間編組信息,而不是實際的實現。
雖然為每個新實現明確地復制每個方法可能看起來很多工作,但實際上它通常只是整個服務應該做的一小部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.