Box(box.net)API v2.0的基于twisted的异步接口
txboxdotnet的Python项目详细描述
txboxdotnet
用于Box (box.net) API (version 2.0)的基于twisted的python异步接口。
用法示例
以下脚本将打印根框.net文件夹的列表,上载 “test.txt”文件,尝试在更新的文件夹列表中找到它,然后 把它取下来。
from twisted.internet import defer, reactor from txboxdotnet.api_v2 import txBoxAPI api = txBoxAPI( client_id='...', client_secret='...', ... ) if not api.auth_code: print '\n'.join([ 'Visit the following URL in any web browser (firefox, chrome, safari, etc),', ' authorize there, confirm access permissions, and paste URL of an empty page', ' (starting with "https://success.box.com/") you will get', ' redirected to into "auth_code" value in "config" dict above.\n', 'URL to visit:\n {}'.format(api.auth_user_get_url()) ]) exit() if re.search(r'^https?://', api.auth_code): api.auth_user_process_url(api.auth_code) @defer.inlineCallbacks def do_stuff(): # Print root directory listing print list(e['name'] for e in (yield api.listdir())) # Upload "test.txt" file from local current directory file_info = yield api.put('test.txt') # Find just-uploaded "test.txt" file by name file_id = yield api.resolve_path('test.txt') # Check that id matches uploaded file assert file_info['id'] == file_id # Remove the file yield api.delete(file_id) do_stuff().addBoth(lambda ignored: reactor.stop()) reactor.run()
注意,传递给上面txboxapi类init的参数应该包含 身份验证数据,可以/应该从“客户端ID”和 “客户端机密”,在应用程序注册后提供on box.net。
服务(至少现在)有一个相当烦人的要求 授权API访问请求后将提供的“身份验证代码” 仅在30秒内有效。有关进程的详细信息,请参见API auth docs。
有关更完整的示例(包括oauth2的内容),请参见 api_v2.py 在if __name__ == '__main__':之后的代码(需要更好的示例 未来,欢迎使用补丁程序!).
安装
它是Python2.7(而不是3.x)的常规包。
使用pip是最好的方法:
% pip install txboxdotnet
如果没有,请使用:
% easy_install pip % pip install txboxdotnet
或者(see also):
% curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python % pip install txboxdotnet
或者,如果您必须这样做:
% easy_install txboxdotnet
但是,你真的不应该这么做。
当前的git版本可以如下安装:
% pip install 'git+https://github.com/mk-fg/txboxdotnet.git#egg=txboxdotnet'
注意,要在系统范围的路径和站点包中安装内容, 通常需要提升权限。使用“install–user”, ~/.pydistutils.cfg 或者virtualenv要做 无权限安装到自定义路径中。
要求
- Python 2.7 (not 3.X)
- Twisted(核心,网络,至少12.2.0)
实现的方法
已实现的API方法包装器的矩阵(与 docs):
- 文件夹
- [X]列表:^{TT2}$
- [X]创建:^{TT3}$
- [X]获取信息:^{TT4}$
- []副本
- [X]删除:^{TT5}$
- []更新信息
- []创建共享链接
- []查看讨论
- []查看协作
- []垃圾清单
- []垃圾信息
- []从垃圾箱恢复
- []永久删除
- 文件
- [X]上传:^{TT6}$
- [X]下载:^{TT7}$
- [X]获取信息:^{TT8}$
- [X]上传覆盖:^{TT6}$
- []上传重命名
- []查看版本
- []更新信息
- [X]删除:^{TT10}$
- []副本
- []创建共享链接
- []查看评论
- []获取缩略图
- []从垃圾桶中获取
- []从垃圾箱恢复
- []永久删除
- 评论
- 讨论
- 合作
- 用户
- [X]当前用户信息:^{TT11}$
- []所有其他;)
- 搜索
- 共享项目
- 事件
注意,这些包装器往往很薄,只是采用python方法 参数并将其转换为适当的api键,通常 有点像这样:
def info_file(self, file_id): return self(join('files', file_id))
任何缺少的方法也可以通过调用 与上面一样,传递所有适当的(根据文档)参数。
我的目的很狭窄-我只将服务用于文件存储-所以 不太可能实现缺少的共享/协作包装器 这里没有从有兴趣拥有这些补丁。