像老板一样休息
hammock的Python项目详细描述
_ _ | | | | | |__ _____ ____ ____ ___ ____| | _ | _ \(____ | \| \ / _ \ / ___) |_/ ) | | | / ___ | | | | | | | |_| ( (___| _ ( |_| |_\_____|_|_|_|_|_|_|\___/ \____)_| \_)
hammock是一个有趣的模块,它允许您通过将restapi转换成死的简单编程api来处理restapi。 它在后院使用流行的requests模块来提供全面的休息体验。
证明
让我们玩github:
>>> from hammock import Hammock as Github >>> # Let's create the first chain of hammock using base api url >>> github = Github('https://api.github.com') >>> # Ok, let the magic happens, ask github for hammock watchers >>> resp = github.repos('kadirpekel', 'hammock').watchers.GET() >>> # now you're ready to take a rest for the rest the of code :) >>> for watcher in resp.json: print watcher.get('login') kadirpekel ... .. .
不相信?这也是您如何观看此项目以了解其未来功能:
>>> github.user.watched('kadirpekel', 'hammock').PUT(auth=('<user>', '<pass>'), headers={'content-length': '0'}) <Response [204]>
怎么做?
Hammock是requests模块上的薄包装,您仍然可以使用它。但它简化了你的生活 通过使用对象表示法,让您自然地将变量放入url中。你也可以包一些 url片段到对象中,以改进代码重用。例如;
拿着这些;
>>> base_url = 'https://api.github.com' >>> user = 'kadirpekel' >>> repo = 'hammock'
如果没有Hammock,使用纯requests模块,则必须使用字符串格式手动生成URL:
>>> requests.get("%s/repos/%s/%s/watchers" % (base_url, user, repo))
使用Hammock,您不必处理字符串格式。您可以包装base_url以供代码重用 很容易将变量映射到url。这更干净:
>>> github = hammock.Hammock(base_url) >>> github.repos(user, repo).watchers.GET() >>> github.user.watched(user, repo).PUT() # reuse!
安装
安装Hammock的最佳方法是通过easy_install或pip使用pypi存储库:
$ pip install hammock
文档
Hammock是一个神奇的、多态的(!),有趣而简单的类,帮助您生成restfulurl 并允许您使用requests模块以简单而巧妙的方式请求它们。
下面的所有短语都向相同的url“http://localhost:8000/users/foo/posts/bar/comments”发出请求。 请注意,所有这些都是有效的,但其中一些在其所属上下文中是无稽之谈:
>>> import hammock >>> api = hammock.Hammock('http://localhost:8000') >>> api.users('foo').posts('bar').comments.GET() <Response [200]> >>> api.users.foo.posts('bar').GET('comments') <Response [200]> >>> api.users.foo.posts.bar.comments.GET() <Response [200]> >>> api.users('foo', 'posts', 'comments').GET() <Response [200]> >>> api('users')('foo', 'posts').GET('bar', 'comments') <Response [200]> >>> # Any other combinations ...
Hammock类实例提供请求模块作为大写版本绑定在自身上的所有http方法 删除第一个参数url以替换*args,以便继续追加url组件。
还可以继续为requests模块的相应http动词方法提供任何关键字参数:
Hammock.[GET, HEAD, OPTIONS, POST, PUT, PATCH, DELETE](*args, **kwargs)
返回类型与模块提供的Responseobjectrequestsmodule相同。
下面是一些使用twitter api的更真实的应用示例:
>>> import hammock >>> twitter = hammock.Hammock('https://api.twitter.com/1') >>> tweets = twitter.statuses('user_timeline.json').GET(params={'screen_name':'kadirpekel', 'count':'10'}).json >>> for tweet in tweets: print tweet.get('text') my tweets ... .. .
您可能还想使用会话。让我们看看下面的jira示例,它维护了 通过几个http请求验证凭据:
>>> import hammock >>> # You can configure a session by providing keyword args to `Hammock` constructor to initiate builtin `requests` session >>> # This sample below shows the use of auth credentials through several requests by intitiating a embedded session >>> jira = hammock.Hammock('https://jira.atlassian.com/rest/api/latest', auth=('<user>', '<pass>')) >>> my_issue = 'JRA-9' >>> # Let's get a jira issue. No auth credentials provided explicitly since parent >>> # hammock already has a `requests` session configured. >>> issue = jira.issue(my_issue).GET() >>> # Now watch the issue again using with the same session >>> watched = jira.issue(my_issue).watchers.POST(params={'name': '<user>'}) >>> print(watched)
还要记住,如果您希望在由^{tt2}生成的url的末尾有一个斜杠$ 构造Hammock时,应将append_slashkewyword参数作为True传递。 例如:
>>> api = hammock.Hammock('http://localhost:8000', append_slash=True) >>> print (api.foo.bar) # Note that trailing slash 'http://localhost:8000/foo/bar/'
贡献者
- @Maraujop(Miguel Araujo)
- @魔方(Michele Lacchia)
许可证
版权所有(c)2012 Kadir Pekel。
特此免费授予获得本软件副本和相关文档文件(以下简称“软件”)的任何人使用本软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或出售本软件副本的权利,并在符合以下条件的情况下,允许向其提供软件的人员这样做:
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件是按原样提供的,无任何保证,明示或默示,包括但不限于适销性、适合特定用途和不侵权的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权诉讼或其他诉讼中,由软件或软件的使用或其他交易引起的、由软件引起的或与之相关的。