【问题标题】:GAE: Writing the API: a simple PATCH method (Python)GAE:编写 API:一个简单的 PATCH 方法(Python)
【发布时间】:2014-10-22 09:48:12
【问题描述】:

我有一个 google-cloud-endpoints,在文档中,我没有找到如何编写 PATCH 方法。 我的要求:

curl -XPATCH localhost:8080/_ah/api/hellogreeting/1 -d '{"message": "Hi"}'

我的方法处理程序如下所示:

from models import Greeting
from messages import GreetingMessage


@endpoints.method(ID_RESOURCE, Greeting,`
              path='hellogreeting/{id}', http_method='PATCH',
              name='greetings.patch')
def greetings_patch(self, request):
    request.message, request.username
    greeting = Greeting.get_by_id(request.id)
    greeting.message = request.message   # It's ok, cuz message exists in request
    greeting.username = request.username # request.username is None. Writing the IF conditions in each string(checking on empty), I think it not beatifully. 
    greeting.put()
    return GreetingMessage(message=greeting.message, username=greeting.username)

所以,现在Greeting.username 中的字段将为无。这是错误的。 在每个字符串中写入 IF 条件(检查是否为空),我认为它并不完美。 那么,模型部分更新的最佳方式是什么?

【问题讨论】:

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


    【解决方案1】:

    我认为 Cloud Endpoints 中没有,但您可以像下面的示例一样轻松编写自己的代码。

    你需要决定你希望你的补丁如何表现,特别是当涉及到作为对象的属性时:你应该也将补丁应用于对象属性(在这种情况下使用递归)还是应该替换原始对象属性与我的示例中的新对象属性相同。

    def apply_patch(origin, patch):
        for name in dir( patch ):
                if not name.startswith( '__' ):
                    setattr(origin,name,getattr(patch,name))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      相关资源
      最近更新 更多