Skip to content
Snippets Groups Projects
README.rst 6.95 KiB
Newer Older
Arkindex 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')

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

Erwan Rouchet's avatar
Erwan Rouchet committed
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.

You can open ``https://your.arkindex/api-docs/`` to access the API documentation,
which will describe the available API endpoints, including their operation ID and
parameters.

Erwan Rouchet's avatar
Erwan Rouchet committed
.. 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

Erwan Rouchet's avatar
Erwan Rouchet committed
   for element in cli.paginate('ListElements', corpus=corpus['id']):
       print(element['name'])
Erwan Rouchet's avatar
Erwan Rouchet committed

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')

Erwan Rouchet's avatar
Erwan Rouchet committed
Handling errors
^^^^^^^^^^^^^^^

Erwan Rouchet's avatar
Erwan Rouchet committed
APIStar_, the underlying API client we use, does most of the error handling.
Erwan Rouchet's avatar
Erwan Rouchet committed
It will raise two types of exceptions:

``apistar.exceptions.ErrorResponse``
  The request resulted in a HTTP 4xx or 5xx response from the server.
``apistar.exceptions.ClientError``
  Any 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.  

Erwan Rouchet's avatar
Erwan Rouchet committed
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, an ``arkindex.exceptions.SchemaError`` exception
will be raised.

Erwan Rouchet's avatar
Erwan Rouchet committed
You can handle HTTP errors and fetch more information about them using the
exception's attributes:

.. code:: python

   from apistar.exceptions import ErrorResponse
   try:
       # cli.request ...
   except ErrorResponse as e:
       print(e.title)   # "400 Bad Request"
       print(e.status_code)  # 400
       print(e.result)  # Any kind of response body the server might give

Erwan Rouchet's avatar
Erwan Rouchet committed
Note that by default, using ``repr()`` or ``str()`` 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's `APIStar fork`_::

   pip install git+https://gitlab.com/teklia/apistar.git

This will provide support for ``repr()`` and ``str()``, which will also
enhance error messages on unhandled exceptions.

Erwan Rouchet's avatar
Erwan Rouchet committed
Uploading a file
^^^^^^^^^^^^^^^^

The underlying API client we use does not currently handle sending anything
other than JSON; therefore, the client adds a helper method to upload files
to a corpus using the ``UploadDataFile`` endpoint: ``ArkindexClient.upload``.

.. code:: python

   # Any readable file-like object is supported
   with open('cat.jpg', 'rb') as f:
       cli.upload('CORPUS_ID', f)

   # You can also use a path directly
   cli.upload('CORPUS_ID', 'cat.jpg')

Erwan Rouchet's avatar
Erwan Rouchet committed
Trying to upload an existing data file will result in a HTTP 400 error that
includes the existing data file's ID, so that you can try to delete it and
upload again or just reuse it.

.. code:: python

   from apistar.exceptions import ErrorResponse
   try:
       data = cli.upload('CORPUS_ID', 'cat.jpg')
       file_id = data['id']
       print('Success', file_id)
   except ErrorResponse as e:
       if e.status_code != 400 or 'id' not in e.content:
           raise
       file_id = e.content['id']
       print('Already exists', file_id)

Erwan Rouchet's avatar
Erwan Rouchet committed
Sending XML content
^^^^^^^^^^^^^^^^^^^

Some Arkindex endpoints expect XML to be sent as the request body; to do so,
use the ``ArkindexClient.send_xml`` method. This method expects a string,
bytestring, or a file-like object as the body. The method works just like
any other request.

.. code:: python

   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'))

Erwan Rouchet's avatar
Erwan Rouchet committed
Examples
--------

Erwan Rouchet's avatar
Erwan Rouchet committed
Print all folders
Erwan Rouchet's avatar
Erwan Rouchet committed
^^^^^^^^^^^^^^^^^

.. code:: python

Erwan Rouchet's avatar
Erwan Rouchet committed
   for folder in cli.paginate('ListElements', folder=True):
       print(folder['name'])
Erwan Rouchet's avatar
Erwan Rouchet committed

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)

Erwan Rouchet's avatar
Erwan Rouchet committed
Import transcriptions for a page from files in PAGE XML format
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code:: python

   cli.send_xml('ImportTranskribusTranscriptions', id='PAGE_ID', body=open('file.xml', 'rb'))
Erwan Rouchet's avatar
Erwan Rouchet committed
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']))
Erwan Rouchet's avatar
Erwan Rouchet committed

.. _APIStar: http://docs.apistar.com/
.. _APIStar fork: https://gitlab.com/teklia/apistar