Skip to content
Snippets Groups Projects
README.rst 3.54 KiB
Newer Older
Arkindex API Client
===================

Erwan Rouchet's avatar
Erwan Rouchet committed
**API documentation available at https://arkindex.gitlab.io/api-client/**
Erwan Rouchet's avatar
Erwan Rouchet committed
``arkindex-client`` provides an API client to interact with Arkindex servers.
Erwan Rouchet's avatar
Erwan Rouchet committed
.. contents::
   :depth: 2
   :local:
   :backlinks: none
Erwan Rouchet's avatar
Erwan Rouchet committed
Setup
-----

Install the client using ``pip``::

   pip install arkindex-client

Erwan Rouchet's avatar
Erwan Rouchet committed
Usage
-----

To create a client and login using an email/password combo,
Erwan Rouchet's avatar
Erwan Rouchet committed
use the ``ArkindexClient.login`` helper method:
Erwan Rouchet's avatar
Erwan Rouchet committed

.. code:: python

   from arkindex import ArkindexClient
   cli = 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:

.. code:: python

   from arkindex import ArkindexClient
   cli = ArkindexClient('YOUR_TOKEN')

To perform a simple API request, you can use the ``request()`` method. The method
takes an operation ID as a name and the operation's parameters as keyword arguments:

Making requests
^^^^^^^^^^^^^^^

.. code:: python

   corpus = cli.request('RetrieveCorpus', id='...')

The result will be a Python ``dict`` containing the result of the API request.
If the request returns an error, an ``apistar.exceptions.ErrorResponse`` will
be raised.

Dealing with pagination
^^^^^^^^^^^^^^^^^^^^^^^

The Arkindex client adds another helper method for paginated endpoints that
Erwan Rouchet's avatar
Erwan Rouchet committed
deals with pagination for you: ``ArkindexClient.paginate``. This method
returns a ``ResponsePaginator`` instance, which is a classic Python
Erwan Rouchet's avatar
Erwan Rouchet committed
iterator that does not perform any actual requests until absolutely needed:
that is, until the next page must be loaded.

.. code:: python

   for page in cli.paginate('ListCorpusPages', id=corpus['id']):
       print(page['display_name'])

Erwan Rouchet's avatar
Erwan Rouchet committed
**Warning:** Using ``list`` on a ``ResponsePaginator`` may load dozens
of pages at once and cause a big load on the server. You can use ``len`` to
get the total item count before spamming a server.
Erwan Rouchet's avatar
Erwan Rouchet committed

Using another server
^^^^^^^^^^^^^^^^^^^^

By default, the API client is set to point to the main Arkindex server at
https://arkindex.teklia.com. If you need or want to use this API client on
another server, you can use the ``base_url`` keyword argument when setting up
your API client:

.. code:: python

   cli = ArkindexClient(base_url='https://somewhere')

Examples
--------

Print all volumes
^^^^^^^^^^^^^^^^^

.. code:: python

   for volume in cli.paginate('ListElements'):
       print(volume['name'])

Create transcriptions in bulk
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: python

   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)

Download full logs for each Ponos task in a workflow
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: python

   workflow = cli.request('RetrieveWorkflow', id='...')
   for task in workflow['tasks']:
       with open(task['id'] + '.txt', 'w') as f:
           f.write(cli.request('RetrieveTaskLog', id=task['id']))