具有多个响应消息的google云端点方法

时间:2022-02-23 18:37:03

I have a google could enpoint method that I need to be able to return either a MaleResponseMessage or a FemaleResponseMessage. Is there a way to specify that such as with

我有一个google can enpoint方法,我需要能够返回MaleResponseMessage或FemaleResponseMessage。有没有办法指定诸如with

@endpoints.method(message_types.VoidMessage, [MaleResponseMessage, FemaleResponseMessage])

There is of course the option of declaring a super message class, say, PersonResponseMessage to wrap either MaleResponseMessage or FemaleResponseMessage. But is there something similar to the snippet above?

当然可以选择声明一个超级消息类,比如PersonResponseMessage来包装MaleResponseMessage或FemaleResponseMessage。但有什么类似于上面的片段?

EDIT:

编辑:

Trying to implement my own proposal, I got stuck. The only thing the two message types have in common is the request: the exact same request fields (with an additional boolean female=true/false) for PersonRequest. The MaleResponseMessage and the FemaleResponseMessage have no field in common. So I am using one endpoint method, as @bossylobster shows, where I check

我试图实施自己的提案,但我遇到了困难。这两种消息类型的唯一共同点是请求:PersonRequest的完全相同的请求字段(带有额外的布尔值female = true / false)。 MaleResponseMessage和FemaleResponseMessage没有共同的字段。所以我使用一个端点方法,正如@bossylobster所示,我在哪里检查

if request.female : # request.female == True
    return get_female(etc, etc)
else: # request.female == False // implies male
    return get_male(etc,etc)

For the response, I need something like

对于回应,我需要类似的东西

class PersonResponse(messages.Message):
  if ??? :
     item = messages.MessageField(MaleResponseMessage,1)
  else:
      item = messages.MessageField(FemaleResponseMessage,1)

I am not sure what to check ??? for. First, I thought about isinstance or type. But how would I do that? Would the below work?

我不知道该检查什么???对于。首先,我想到了实例或类型。但是我该怎么做呢?以下工作吗?

class PersonResponse(messages.Message):
  if type(Message()) == MaleResponseMessage :
     item = messages.MessageField(MaleResponseMessage,1)
  else:
      item = messages.MessageField(FemaleResponseMessage,1)

2 个解决方案

#1


2  

You could use two different endpoint methods.

您可以使用两种不同的端点方法。

#2


3  

Unfortunately no. You can have only one response and one request schema; this is because they are registered with Google's API infrastructure and having a strict schema is what provides the speed and efficiency of requests.

很不幸的是,不行。您只能有一个响应和一个请求架构;这是因为它们已在Google的API基础架构中注册,并且具有严格的架构可以提供请求的速度和效率。

Your best bet would be to combine the fields needed for each male and female into a single model class and do your own validation.

您最好的选择是将每个男性和女性所需的字段组合到一个模型类中并进行自己的验证。

A possible solution could look like

可能的解决方案看起来像

from protorpc import messages

class Gender(messages.Enum):
    MALE = 0
    FEMALE = 1

class GenderRequest(messages.Enum):
    gender = messages.EnumField(Gender, 1, required=True)

class PersonResponse(messages.Message):
    gender = messages.EnumField(Gender, 1)
    # shared fields
    # female specific fields
    # male specific fields

and then in your actual method

然后在你的实际方法中

    @endpoints.method(GenderRequest, PersonMessage, ...)
    def my_method(self, request):
      if request.gender == Gender.MALE:
          return male_response(request)
      elif request.gender == Gender.FEMALE:
          return female_response(request)
      else:
          # This should never occur since gender is required
          raise endpoints.BadRequestException('Gender not set.')

where male_response and female_response are methods which create instances of PersonMessage corresponding to male and female.

其中male_response和female_response是创建对应于男性和女性的PersonMessage实例的方法。

#1


2  

You could use two different endpoint methods.

您可以使用两种不同的端点方法。

#2


3  

Unfortunately no. You can have only one response and one request schema; this is because they are registered with Google's API infrastructure and having a strict schema is what provides the speed and efficiency of requests.

很不幸的是,不行。您只能有一个响应和一个请求架构;这是因为它们已在Google的API基础架构中注册,并且具有严格的架构可以提供请求的速度和效率。

Your best bet would be to combine the fields needed for each male and female into a single model class and do your own validation.

您最好的选择是将每个男性和女性所需的字段组合到一个模型类中并进行自己的验证。

A possible solution could look like

可能的解决方案看起来像

from protorpc import messages

class Gender(messages.Enum):
    MALE = 0
    FEMALE = 1

class GenderRequest(messages.Enum):
    gender = messages.EnumField(Gender, 1, required=True)

class PersonResponse(messages.Message):
    gender = messages.EnumField(Gender, 1)
    # shared fields
    # female specific fields
    # male specific fields

and then in your actual method

然后在你的实际方法中

    @endpoints.method(GenderRequest, PersonMessage, ...)
    def my_method(self, request):
      if request.gender == Gender.MALE:
          return male_response(request)
      elif request.gender == Gender.FEMALE:
          return female_response(request)
      else:
          # This should never occur since gender is required
          raise endpoints.BadRequestException('Gender not set.')

where male_response and female_response are methods which create instances of PersonMessage corresponding to male and female.

其中male_response和female_response是创建对应于男性和女性的PersonMessage实例的方法。