简化rest web服务客户端创建的库。

api_toolkit的Python项目详细描述


python api toolkit是一个以简单方式处理restapi的模块。api需要遵循一些简单的规则才能得到支持。



演示
----
例如,使用python api toolkit的[收费](https://github.com/myfreecom/charging)。

``python
>;>;从api工具包导入资源
>t;打印资源。url_attribute_name
"url"
>;>resource.url_attribute_name="uri"
>;>entrypoint=resource.load(
…)http://sandbox.charging.financeconnect.com.br/domain/',
…用户='',密码='1+oc7qhjqg6h9itrlq7cww='
…)
>;>;入口点。资源数据
{u'address':u'address',
u'city':u'niteroi/rj',
u'description':无,
u'etag':u'da39a3ee5e6b4b0d3255bfef95601890afd80709',
u'supplier':u'gerardo soares',
u'token':u'9kpjcag/qsui+g3s+0qcka==',
u'uri':u'http://sandbox.charging.financeconnect.com.br/account/domains/513e8442-0a4e-404c-b405-8681ca7e58b1/',
u'uuid':u'513e8442-0a4e-404c-b405-8681ca7e58b1',
u'zipcode':u'24020040'}
>;>然后,您可以作为一个t将资源作为对象进行分配和处理。
>;>;entrypoint.address=u'new address'
>;>;entrypoint.save()
>;>;列表(entrypoint.charge_accounts.all())
[<;api_toolkit.resource type="charge_accounts">;,<;api_toolkit.resource type="charge-accounts">;]
```


,所有其他资源对象的操作方式与第一个相同:
``python
>;>;对于entrypoint.charge-accounts.all():
…res.delete()
```

Requirements(for API)
——你的api应该是restful。
2.您应该实现头链接。
3.您的api应该接受json。



github使用头链接进行其[api的分页](http://developer.github.com/v3/分页)。我们所需要的只是把它扩大一点。例如:在[收费](https://github.com/myfreecomm/charging)上,[/domain/](http://sandbox.charging.financeconnect.com.br/domain/)具有以下链接头:

链接:<;http://sandbox.charging.financeconnect.com.br/charge accounts/>;;rel="charge_accounts"



存在分页的页,如[/charge accounts/](http://sandbox.charging.financeconnect.com.br/charge accounts/),链接头如下:

链接:<;http://sandbox.charging.financeconnect.com.br/charge accounts/banks/>;;rel="banks",
<;http://sandbox.charging.financeconnect.com.br/charge accounts/currences/>;;rel="currences",
<;http://sandbox.charging.financeconnect.com.br/charge-accounts/?limit=10&page=2>;;rel="next"



需求(对于客户端)
----
python api toolkit使用``请求```而不是常见的`` httplib2```.


install
----
您可以通过``pip``或``轻松安装``来安装python api toolkit。这个项目还没有在pypi上。

`$pip install-e git+git@github.com:myfreecom/python api toolkit egg=python api toolkit`


documentation
----
python api toolkit使用两个类以友好的方式公开api,即资源和集合。
是api的对象和入口点,它们是您将要操作的实体。
集合只是同一资源的集合。
python api toolkit将get和delete http动词定义为get和delete,put到资源,post到集合,现在所有动词都允许each资源或集合,即使api不需要。
将来动词将限制在api通过选项响应的那些动词。


但您始终可以"reload()",将它们与api服务器同步。
例如:
``python
>;>;从api_toolkit import resource
>;>;res=resource.load('http://example.com/api/user/',user='test',password='pass')
>;>;res.url
"http://example.com/api/user/"
>;>;>;资源最初加载。
>;>;res.url='nope!'
>;>;res.reload()
>;>;res.url
'http://example.com/api/user/'
>;>;>;资源与服务器同步。
````

但是如果您想在api服务器的资源上放置一些东西呢?您所要做的就是将资源视为一个对象,并在完成后调用"save()"。很像django orm的模型。
例如:
`` python
>;>我们正在重用以前加载的资源。
>;>re s.name='测试用户'
>;>res.save()
>;>服务器接受了我们的更改。
>;>res.reload()
>;>;res.name
"测试用户"
```
注意:如果尝试更改无法更改的属性,对象将很好地接受它,但"save()`"将引发异常。
``python
>;>;res.url='http://example.com/seuble/'
>;>;res.save()
httperror:400客户端错误:错误请求
```

,如果要删除资源,只需调用``delete()`.
``python
>;>;res.delete()
>;>;res.reload()
httperror:404客户端错误:未找到
```
注意:本地实例也不会被删除。如果api接受put as create,则在删除对象后只需调用"save()"。
``python
>;>res.url
'http://example.com/api/user/'
>;>res.save()
>;>res.reload()
```

集合实例化为属性。要获取与此用户相关的任务集合,只需执行以下操作:
``python
>;>res.tasks
<;api\u toolkit.collection type="tasks">;
```

collection
collection
collections是资源管理器。在集合中,您可以列出该类型的所有资源、从该类型获取特定资源、创建新资源或删除该类型中的所有资源。

例如:
`` python
>;>我们仍在利用前面定义的资源。
>;> res.tasks
<;api廑toolkit.collection type="tasks">;
>;> list(res.tasks.all())
[<;api廑toolkit.resource type="tasks">;,<;api廑toolkit.resource type="tasks">;]
>;>;注意,all()返回生成器。
>;>;对于res.tasks.all()中的资源
…打印resource.url
'http://example.com/api/users/tasks/1/'
'http://example.com/api/users/tasks/2/'
````

`` get()``将资源标识符作为参数(在"http://example.com/api/users/tasks/1/"中,标识符为"1"。
``python
>;>first_resource=res.tasks.get('1')
>;>first_resource.url
'http://example.com/api/users/tasks/1/'
```

to创建一个新资源,您所要做的就是为"create()"提供足够的参数。例如:
`` python
>;>new_resource=res.tasks.create(
…description='要创建的测试任务',
…双日='07/10',
…名称='任务01'
…)
``

现在,如果要从集合中删除所有内容,只需调用``delete()`。要实现的
`` python
>;>res.tasks.delete()
>;>list(res.tasks.all())
[]
>;>everything gone!让我们保存刚刚创建的资源…
>;>new_resource.save()
>;>list(res.tasks.all())
[<;api_toolkit.resource type="tasks">;]
```

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring启动启用HTTPS   actionscript 3 java中的这个[“var”+“name”]   java只匹配给定集合中一个字符的一个匹配项   java Hibernate:防止角色表中出现多个相同的条目   javajersey+Spring注入servlet请求   java HtmlEditor javafx失去焦点   java Apache Wicket AjaxRequestTarget ListView组件未刷新或更新   mysql java。无法将lang.String转换为java。sql。时间戳   java将巨大的整数文件(在一行中)拆分为具有内存限制的已排序块   安卓如何完全关闭proguard?   安装Eclipse和Android SDK后的java“无AVD可用”消息   java动态显示图像视图   java在Spring中还有哪些WebsocketClient实现?   java Glassfish需要很长时间才能重新启动   使用Java简单串行连接器将pc与arduino连接   java如何在camel文件组件配置中结合readLockCheckInterval和maxMessagesPerPoll?   单击Android时的java预览图像   java如何将字节数组转换为ByteArrayOutputStream