Package: arkindex-client
arkindex-clientprovides an API client to interact with Arkindex servers.SetupUsageMaking requestsDealing with paginationUsing another serverHandling errorsExamplesPrint all foldersDownload full logs for each Ponos task in a workflowLintingSetupInstall the client usingpip:pip install arkindex-clientUsageTo create a client and login using an email/password combo,
use theArkindexClient.loginhelper method:fromarkindeximportArkindexClientcli=ArkindexClient()cli.login('EMAIL','PASSWORD')This helper method will save the authentication token in your API client, so
that it is reused in later API requests.If you already have an API token, you can create your client like so:fromarkindeximportArkindexClientcli=ArkindexClient('YOUR_TOKEN')Making requestsTo perform a simple API request, you can use therequest()method. The method
takes an operation ID as a name and the operation’s parameters as keyword arguments.You can openhttps://your.arkindex/api-docs/to access the API documentation,
which will describe the available API endpoints, including their operation ID and
parameters.corpus=cli.request('RetrieveCorpus',id='...')The result will be a Pythondictcontaining the result of the API request.
If the request returns an error, anapistar.exceptions.ErrorResponsewill
be raised.Dealing with paginationThe Arkindex client adds another helper method for paginated endpoints that
deals with pagination for you:ArkindexClient.paginate. This method
returns aResponsePaginatorinstance, which is a classic Python
iterator that does not perform any actual requests until absolutely needed:
that is, until the next page must be loaded.forelementincli.paginate('ListElements',corpus=corpus['id']):print(element['name'])Warning:Usingliston aResponsePaginatormay load dozens
of pages at once and cause a big load on the server. You can uselento
get the total item count before spamming a server.A call topaginatemay produce hundreds of sub-requests depending on the size
of the dataset you’re requesting. To accommodate with large datasets, and support
network or performance issues,paginatesupports aretriesparameter to
specify the number of sub-request it’s able to run for each page in the dataset.
By default, the method will retry 5 times per page.You may want to allowpaginateto fail on some pages, for really big datasets
(errors happen). In this case, you should use the optional boolean parameterallow_missing_data(set toFalseby default).Here is an example of pagination on a large dataset, allowing data loss, loweringretries and listing the missed pages:elements=cli.paginate('ListProcessElements',id='XXX',retries=3,allow_missing_data=True,)forelementinelements:print(element['id'])print("Missing pages:{elements.missing}")Using another serverBy default, the API client is set to point to the main Arkindex server athttps://arkindex.teklia.com. If you need or want to use this API client on
another server, you can use thebase_urlkeyword argument when setting up
your API client:cli=ArkindexClient(base_url='https://somewhere')Handling errorsAPIStar, the underlying API client we use, does most of the error handling.
It will raise two types of exceptions:apistar.exceptions.ErrorResponseThe request resulted in a HTTP 4xx or 5xx response from the server.apistar.exceptions.ClientErrorAny error that prevents the client from making the request or fetching
the response: invalid endpoint names or URLs, unsupported content types,
or unknown request parameters. See the exception messages for more info.Since this API client retrieves the endpoints description from the server
using the base URL, errors can occur during the retrieval and parsing of the
API schema. If this happens, anarkindex.exceptions.SchemaErrorexception
will be raised.You can handle HTTP errors and fetch more information about them using the
exception’s attributes: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 giveNote that by default, usingrepr()orstr()on APIStar exceptions will
not give any useful messages; a fix in APIStar is waiting to be merged. In
the meantime, you can use Teklia’sAPIStar fork:pip install git+https://gitlab.teklia.com/arkindex/apistar.gitThis will provide support forrepr()andstr(), which will also
enhance error messages on unhandled exceptions.ExamplesPrint all foldersforfolderincli.paginate('ListElements',folder=True):print(folder['name'])Download full logs for each Ponos task in a workflowworkflow=cli.request('RetrieveWorkflow',id='...')fortaskinworkflow['tasks']:withopen(task['id']+'.txt','w')asf:f.write(cli.request('RetrieveTaskLog',id=task['id']))LintingWe usepre-commitwithblackto automatically format the Python source code of this project.To be efficient, you should run pre-commit before committing (hence the name…).To do that, run once :pipinstallpre-commitpre-commitinstallThe linting workflow will now run on modified files before committing, and will fix issues for you.If you want to run the full workflow on all the files:pre-commit run -a.