factory.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:stethoscope 作者: Netflix 项目源码 文件源码
def serialized_endpoint(*_callbacks):
  """Decorator which wraps an endpoint by applying callbacks to the results then serializing to JSON.

  First, the decorated function is called and must return a `defer.Deferred`. The callbacks supplied
  to the decorator are then applied followed by any callbacks supplied as keyword arguments to the
  decorated function.  The result is then serialized and returned in the response (with
  ``Content-Type`` set to ``application/json``).
  """
  def decorator(func):
    @six.wraps(func)
    def wrapped(request, *args, **kwargs):
      callbacks = kwargs.pop('callbacks', [])
      # logger.debug("in wrapped:\nargs: {!r}\nkwargs: {!r}", args, kwargs)

      deferred_list = func(*args, **kwargs)

      for callback in list(_callbacks) + callbacks:
        deferred_list.addCallback(callback)

      deferred_list.addCallback(json.dumps, default=stethoscope.utils.json_serialize_datetime)
      request.setHeader('Content-Type', 'application/json')
      return deferred_list
    return wrapped
  return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号