djangoclient.py 文件源码

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

项目:maas 作者: maas 项目源码 文件源码
def transparent_encode_multipart(func):
    """Wrap an HTTP client method, transparently encoding multipart data.

    This wraps some of Django's `Client` HTTP verb methods -- delete, options,
    patch, put -- so they accept a dict of data to be sent as part of the
    request body, in MIME multipart encoding.

    This also accepts an optional dict of query parameters (as `query`) to be
    encoded as a query string and appended to the given path.

    Since Django 1.5, these HTTP verb methods require data in the form of a
    byte string. The application (that's us) needs to take care of MIME
    encoding.
    """
    @wraps(func)
    def maybe_encode_multipart(
            self, path, data=b"", content_type=None, secure=False, query=None,
            **extra):

        if isinstance(data, bytes):
            if content_type is None:
                content_type = 'application/octet-stream'
        elif content_type is None:
            content_type = client.MULTIPART_CONTENT
            data = client.encode_multipart(client.BOUNDARY, data)
        else:
            raise TypeError(
                "Cannot combine data (%r) with content-type (%r)."
                % (data, content_type))

        if query is not None:
            query = urlencode(query, doseq=True)
            path = path + ("&" if "?" in path else "?") + query

        return func(self, path, data, content_type, secure, **extra)

    return maybe_encode_multipart
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号