Python,在 API 中使用重复字段
Python, Using repeated field in API
我有一个 API 如下所示。如果 API 在 params 中只有一个值(这是一个重复的字段)被调用,一切都会按预期工作。但是,如果 params 包含多个值,则会出现错误:找不到路径的端点。
1 INPUT = endpoints.ResourceContainer(
2 params = messages.IntegerField(1, repeated = True, variant = messages.Variant.INT32))
3
4 @endpoints.method(INPUT,
5 response_type.CustomResponse,
6 path = 'foo/{params}',
7 http_method = 'POST',
8 name = 'foo')
9 def foo(self, request):
10 #foo body is irrelevent
11 return response
我该如何解决这个问题。类似于:path = 'foo/{params[]}', ?
感谢您的帮助
如果 'params' 是查询字符串的一部分而不是路径,您可以从路径中省略它,例如:
path = 'foo'
或
path = 'myApi/foo'
example given in the docs 将 ResourceContainer 用于单个 non-repeated 路径参数。鉴于重复属性的性质,您似乎不能将它们用作路径参数,只能查询字符串参数。查询字符串中的重复字段看起来像这样(很容易处理):
POST http://app.appspot.com/_ah/api/myApi/v1/foo?param=bar¶m=baz ...
但是路径参数中的重复字段看起来像这样(不是那么多):
POST http://app.appspot.com/_ah/api/myApi/v1/foo/bar/baz....
我有一个 API 如下所示。如果 API 在 params 中只有一个值(这是一个重复的字段)被调用,一切都会按预期工作。但是,如果 params 包含多个值,则会出现错误:找不到路径的端点。
1 INPUT = endpoints.ResourceContainer(
2 params = messages.IntegerField(1, repeated = True, variant = messages.Variant.INT32))
3
4 @endpoints.method(INPUT,
5 response_type.CustomResponse,
6 path = 'foo/{params}',
7 http_method = 'POST',
8 name = 'foo')
9 def foo(self, request):
10 #foo body is irrelevent
11 return response
我该如何解决这个问题。类似于:path = 'foo/{params[]}', ?
感谢您的帮助
如果 'params' 是查询字符串的一部分而不是路径,您可以从路径中省略它,例如:
path = 'foo'
或
path = 'myApi/foo'
example given in the docs 将 ResourceContainer 用于单个 non-repeated 路径参数。鉴于重复属性的性质,您似乎不能将它们用作路径参数,只能查询字符串参数。查询字符串中的重复字段看起来像这样(很容易处理):
POST http://app.appspot.com/_ah/api/myApi/v1/foo?param=bar¶m=baz ...
但是路径参数中的重复字段看起来像这样(不是那么多):
POST http://app.appspot.com/_ah/api/myApi/v1/foo/bar/baz....