arkindex项目的api客户端
arkindex-client的Python项目详细描述
api文档可在https://arkindex.gitlab.io/api client/
arkindex-client提供了一个与arkindex服务器交互的api客户端。
设置
使用pip:
安装客户端pip install arkindex-client
用法
要创建客户端并使用电子邮件/密码组合登录, 使用ArkindexClient.loginhelper方法:
fromarkindeximportArkindexClientcli=ArkindexClient()cli.login('EMAIL','PASSWORD')
这个helper方法将在您的api客户机中保存身份验证令牌,因此 在以后的api请求中重用它。
如果您已经有一个api令牌,那么您可以像这样创建您的客户机:
fromarkindeximportArkindexClientcli=ArkindexClient('YOUR_TOKEN')
要执行简单的api请求,可以使用request()方法。方法 以操作id作为名称,操作的参数作为关键字参数:
提出请求
corpus=cli.request('RetrieveCorpus',id='...')
结果将是包含api请求结果的pythondict。 如果请求返回错误,apistar.exceptions.ErrorResponse将 被抚养成人。
处理分页
arkIndex客户端为分页端点添加了另一个helper方法 为您处理分页:ArkindexClient.paginate。这种方法 返回一个ResponsePaginator实例,这是一个典型的python 在绝对需要之前不执行任何实际请求的迭代器: 也就是说,直到下一页必须加载。
forpageincli.paginate('ListCorpusPages',id=corpus['id']):print(page['display_name'])
警告:在ResponsePaginator上使用list可能会加载几十个 同时导致服务器上的大量负载。您可以使用len来 在发送服务器垃圾邮件之前获取项目总数。
使用其他服务器
默认情况下,api客户端设置为指向位于 https://arkindex.teklia.com。如果需要或希望在上使用此API客户端 另一个服务器,您可以在设置时使用base_url关键字参数 您的api客户端:
cli=ArkindexClient(base_url='https://somewhere')
处理错误
APIStar,我们使用的底层api客户机,负责所有的错误处理。 它将引发两种类型的异常:
- apistar.exceptions.ErrorResponse
- 请求导致服务器的HTTP 4xx或5xx响应。
- apistar.exceptions.ClientError
- 阻止客户端发出请求或获取数据的任何错误 响应:无效的终结点名称或URL、不支持的内容类型, 或未知的请求参数。有关详细信息,请参阅异常消息。
您可以使用 异常属性:
fromapistar.exceptionsimportErrorResponsetry:# cli.request ...exceptErrorResponsease:print(e.title)# "400 Bad Request"print(e.status_code)# 400print(e.result)# Any kind of response body the server might give
上传文件
我们使用的底层api客户端当前不处理发送任何内容 而不是json;因此,客户端添加了一个helper方法来上传文件 到使用UploadDataFile端点的语料库:ArkindexClient.upload。
# Any readable file-like object is supportedwithopen('cat.jpg','rb')asf:cli.upload('CORPUS_ID',f)# You can also use a path directlycli.upload('CORPUS_ID','cat.jpg')
尝试上载现有的数据文件会导致HTTP 400错误 包含现有数据文件的ID,以便您可以尝试删除它。 再次上传或重新使用。
fromapistar.exceptionsimportErrorResponsetry:data=cli.upload('CORPUS_ID','cat.jpg')file_id=data['id']print('Success',file_id)exceptErrorResponsease:ife.status_code!=400or'id'notine.content:raisefile_id=e.content['id']print('Already exists',file_id)
发送XML内容
一些arkIndex端点希望XML作为请求体发送;为此, 使用ArkindexClient.send_xml方法。此方法需要一个字符串, bytestring,或者一个类似文件的对象作为主体。这种方法就像 任何其他要求。
cli.send_xml('SomeEndpoint',body='<xml></xml>')cli.send_xml('SomeEndpoint',id='...',arg='...',body=b'<xml></xml>')cli.send_xml('SomeEndpoint',body=open('file.xml'))
示例
打印所有卷
forvolumeincli.paginate('ListElements'):print(volume['name'])
批量创建转录
payload={"parent":"ELEMENT_ID","recognizer":"ML_TOOL_SLUG","transcriptions":[{# A polygon, as a list of at least 3 [x, y] points"polygon":[[100,100],[100,300],[200,300],[200,100],],# The confidence score"score":0.8,# Recognized text"text":"Blah",# Transcription type: page, paragraph, line, word, character"type":"word",},# ...]}cli.request('CreateTranscriptions',body=payload)
从页面XML格式的文件导入页面的转录
cli.send_xml('ImportTranskribusTranscriptions',id='PAGE_ID',body=open('file.xml','rb'))
下载工作流中每个ponos任务的完整日志
workflow=cli.request('RetrieveWorkflow',id='...')fortaskinworkflow['tasks']:withopen(task['id']+'.txt','w')asf:f.write(cli.request('RetrieveTaskLog',id=task['id']))