【问题标题】:Equivalent of @Named API parameter in PythonPython中@Named API参数的等价物
【发布时间】:2013-03-17 21:36:32
【问题描述】:

有什么方法可以在 Python 中使用命名方法参数 - 对应于这个 Java 示例:

@ApiMethod(
    name = "foos.remove",
    path = "foos/{id}",
    httpMethod = HttpMethod.DELETE,
)
public void removeFoo(@Named("id") String id) {
}

在我的 Python 版本中,如果我将 @endpoints.method 路径设置为 foos/{id},则 URL 会正确匹配,但如何访问参数?

【问题讨论】:

  • 它应该只是你方法的第一个参数。
  • @Linuxios 不适用于 Python 实现。

标签: python google-app-engine rest google-cloud-endpoints


【解决方案1】:

没有严格的等价物,但如果{id}在您的路径中,那么您在方法中用于请求类的protorpc消息类中必须有一个名为id的字段。

例如:

from google.appengine.ext import endpoints
from protorpc import messages
from protorpc import remote

class MyMessageClass(messages.Message):
  id = messages.StringField(1)  # Or any other field type

@endpoints.api(...)
class MyApi(remote.Service):
  @endpoints.method(MyMessageClass, SomeResponseClass,
                    ..., path='foos/{id}')
  def my_method(self, request):
    ...

【讨论】:

  • 这行得通,谢谢!如果在文档中提到它会很好。
猜你喜欢
  • 2012-03-09
  • 1970-01-01
  • 2018-09-06
  • 2013-02-06
  • 2012-03-22
  • 2016-12-27
  • 2013-12-13
  • 2021-06-04
  • 1970-01-01
相关资源
最近更新 更多