跳转到主要内容

类包装器,将属性调用映射到HTTP API。

项目描述

AttributeWrapper

https://badge.fury.io/py/attribute_wrapper.png https://pypip.in/d/attribute_wrapper/badge.png

此包装器将属性调用映射到HTTP API。该软件包提供类似于Tortilla的功能,但代码更小、更简单。

这种简单性让您能够根据需要子类化和修改功能。

示例

基本访问

from attribute_wrapper import HTTPWrapper

r = HTTPWrapper("http://kitakitsune.org")
data = r.get()

这将下载http://kitakitsune.org的内容并返回字符串。.get()调用确定HTTP方法,用于执行请求。

属性路径组合

使用点属性访问表示法来组合所需HTTP资源的路径。例如

r.raw.get()

被转换为http://kitakitsune.org/raw的GET请求。

特殊字符

Python中的点表示法仅限于A-Z、a-z、0-9和_,这有时可能过于限制。这就是为什么GenericWrapper具有特殊属性,它包含特殊序列到URL字符的映射。

当前的.specials表采用以下结构实现

{
    "__dot__": ".",
    "__slash__": "/",
    "__dash__": "-",
}

这意味着包含此子字符串的URL将被转换为预期的URL

r.raw.doctene_knihy__dot__txt.get()

被转换为http://kitakitsune.org/raw/doctene_knihy.txt

可以通过替换.specials字典表或使用空字典来禁用它。

下划线方法

更复杂的路径应使用_方法处理

r._("compl?icated/$path/").get()

这被翻译为 http://kitakitsune.org/compl?icated/$path/

自动后缀

如果您使用API,该API期望每个文件以类似于 .json.html 的后缀结尾,您可以在实例化类时修改 .suffix 属性或添加 suffix 参数。

r = HTTPWrapper("http://kitakitsune.org", suffix=".txt")

# this will return content of the http://kitakitsune.org, because the suffix
# is ignored in root of the path
r.get()

# this will return http://kitakitsune.org/raw/doctene_knihy.txt
r.raw.doctene_knihy.get()

参数

处理最后属性调用的方法接受关键字参数,这些参数以表单数据的形式发送到服务器。

r.post(something="else")

这将发送 {"something": "else"} 字典作为POST数据到服务器。

JSONWrapper

作为示例子类,还有一个 JSONWrapper,它将所有参数转换为JSON,并将其作为HTTP body 发送到服务器。这对于一些REST API特别有用。

子类化

代码实际上非常简单(138行!)应该很容易理解。如果您需要一些新功能,只需简单地从 GenericWrapper 类派生并重写 .download_handler() 方法以反映您的需求。

例如 - JSONWrapper 只用几行代码实现

import json

class JSONWrapper(GenericWrapper):
    def download_handler(self, method, url, data):
        if data:
            data = json.dumps(data)

        headers = {
            'content-type': 'application/json'
        }

        resp = requests.request(method, url, headers=headers, data=data)

        # handle http errors
        resp.raise_for_status()

        return json.loads(resp.text)

您的代码

请随时发送包含您自己类的pull request(别忘了对其进行文档化)。我希望使这个包有用,我会很乐意合并您的代码,这样您就不需要创建自己的包。

安装

代码托管在 PYPI 上,您可以使用以下命令轻松安装它

sudo pip install attribute_wrapper

测试

本项目使用 py.test 进行测试。只需从项目的根目录运行 py.test

bystrousak:/h/D/c0d3z/python/libs/attribute_wrapper,0$ py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.0
collected 8 items

tests/test_wrapper.py ........

=========================== 8 passed in 0.12 seconds ===========================

变更日志

0.2.0

  • 添加了完整的测试覆盖率。

  • 当调用没有方法的根对象时,添加了 ValueError 异常。

  • 修复了注释中的某些错别字。

  • 在 README.rst 中添加了测试部分。

  • 添加了特殊的下划线方法 GenericWrapper._(path) 用于复杂的路径。

0.1.2

  • 添加了处理HTTP错误的功能。

0.1.1

  • 修复了包元数据问题。

0.1.0

  • 项目创建。

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定要选择哪一个,请了解有关 安装包 的更多信息。

源分布

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面