用于box-view api的python客户端库
python-boxview的Python项目详细描述
的python客户端库
安装
安装最新版本的最简单方法是使用 pip/easy_install将其从pypi中拉出来:
pip install python-boxview
您还可以使用git从github克隆存储库并安装它 手动:
git clone https://github.com/caxap/python-boxview.git python setup.py install
身份验证
Box View API使用基于令牌的身份验证。你需要创造 应用程序并在Box Developes Portal生成api令牌。然后使用 创建实例的标记
fromboxviewimportboxviewapi=boxview.BoxView('<your box view api key>')
或者,令牌可以由环境变量设置 BOX_VIEW_API_KEY。
用法
python box view支持来自box-view api的所有方法。方法列表 参数描述可以找到here
importosfromboxviewimportboxviewapi=boxview.BoxView('<your box view api key>')# upload file to create new documentdoc=api.create_document(file='python-boxview.pdf',name='python-boxview')# create new document from public urldoc=api.create_document(url='https://cloud.box.com/shared/static/4qhegqxubg8ox0uj5ys8.pdf')doc_id=doc['id']# retrieve existings documentdoc1=api.get_document(doc_id)# list all uploaded documents for your api keyall_docs=api.get_documents(limit=10)# update name of existing documentdoc1=api.update_document(doc_id,name='python-boxview')# check that document ready to viewbool(api.ready_to_view(doc_id))# start view session for documentsession=api.create_session(doc_id,duration=300)ses_id=session['id']# get link to box viewerapi.get_session_url(ses_id)# retrieve original document content to stringcontent,mimetype=api.get_document_content_to_string(doc_id)len(content)# retrieve pdf version of document to fileapi.get_document_content_to_file('python-boxview.pdf',doc_id,extension='.pdf')os.path.exists('python-boxview.pdf')# retrieve mimetype of original document contentmimetype=api.get_document_content_mimetype(doc_id)# create webhookapi.create_webhook('http://example.com/my-webhook')# create S3 storage profileapi.create_storage_profile('S3','super-awesome-bucket','AKIAIOSFODNN7EXAMPLE','wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY')# and delete documentapi.delete_document(doc_id)
处理速率限制
importtimefromboxviewimportboxviewapi=boxview.BoxView('<your box view api key>')document_id='2da6cf9261824fb0a4fe532f94d14625'retry,max_retry=0,3whileTrue:try:api.get_thumbnail_to_file('thumbnail_100x100.png',document_id,100,100)break# ok, thumbnail savedexceptboxview.RetryAfterase:retry+=1ifretry<=max_retry:time.sleep(e.seconds)# waiting for next callelse:raise# failed after `max_retry` attempts, exit with exception