arkindex-client


Namearkindex-client JSON
Version 1.0.15 PyPI version JSON
download
home_pagehttps://gitlab.teklia.com/arkindex/api-client
SummaryAPI client for the Arkindex project
upload_time2024-03-12 11:46:41
maintainer
docs_urlNone
authorTeklia <contact@teklia.com>
requires_python>=3.7
licenseMIT
keywords api client arkindex
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Arkindex API Client
===================

``arkindex-client`` provides an API client to interact with Arkindex servers.

.. contents::
   :depth: 2
   :local:
   :backlinks: none

Setup
-----

Install the client using ``pip``::

   pip install arkindex-client

Usage
-----

To create a client and login using an email/password combo,
use the ``ArkindexClient.login`` helper method:

.. 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
^^^^^^^^^^^^^^^

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.

.. 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
deals with pagination for you: ``ArkindexClient.paginate``. This method
returns a ``ResponsePaginator`` instance, 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.

.. code:: python

   for element in cli.paginate('ListElements', corpus=corpus['id']):
       print(element['name'])

**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.

A call to ``paginate`` may 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, ``paginate`` supports a ``retries`` parameter 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 allow ``paginate`` to fail on some pages, for really big datasets
(errors happen). In this case, you should use the optional boolean parameter
``allow_missing_data`` (set to ``False`` by default).

Here is an example of pagination on a large dataset, allowing data loss, lowering
 retries and listing the missed pages:

.. code:: python

   elements = cli.paginate(
       'ListProcessElements',
       id='XXX',
       retries=3,
       allow_missing_data=True,
   )
   for element in elements:
       print(element['id'])

   print("Missing pages: {elements.missing}")



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

Handling errors
^^^^^^^^^^^^^^^

APIStar_, the underlying API client we use, does most of the error handling.
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.

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.

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.content)  # Any kind of response body the server might give

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.teklia.com/arkindex/apistar.git

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

Examples
--------

Print all folders
^^^^^^^^^^^^^^^^^

.. code:: python

   for folder in cli.paginate('ListElements', folder=True):
       print(folder['name'])

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

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

Linting
-------

We use `pre-commit <https://pre-commit.com/>`_ with `black <https://github.com/psf/black>`_ to 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 :

.. code:: shell

   pip install pre-commit
   pre-commit install

The 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`.




            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.teklia.com/arkindex/api-client",
    "name": "arkindex-client",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "api client arkindex",
    "author": "Teklia <contact@teklia.com>",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/18/66/e5a5f6cfb72a90ee81079311be48d4d1c0cf6bdad3fb81581faf46431724/arkindex-client-1.0.15.tar.gz",
    "platform": null,
    "description": "Arkindex API Client\n===================\n\n``arkindex-client`` provides an API client to interact with Arkindex servers.\n\n.. contents::\n   :depth: 2\n   :local:\n   :backlinks: none\n\nSetup\n-----\n\nInstall the client using ``pip``::\n\n   pip install arkindex-client\n\nUsage\n-----\n\nTo create a client and login using an email/password combo,\nuse the ``ArkindexClient.login`` helper method:\n\n.. code:: python\n\n   from arkindex import ArkindexClient\n   cli = ArkindexClient()\n   cli.login('EMAIL', 'PASSWORD')\n\nThis helper method will save the authentication token in your API client, so\nthat it is reused in later API requests.\n\nIf you already have an API token, you can create your client like so:\n\n.. code:: python\n\n   from arkindex import ArkindexClient\n   cli = ArkindexClient('YOUR_TOKEN')\n\nMaking requests\n^^^^^^^^^^^^^^^\n\nTo perform a simple API request, you can use the ``request()`` method. The method\ntakes an operation ID as a name and the operation's parameters as keyword arguments.\n\nYou can open ``https://your.arkindex/api-docs/`` to access the API documentation,\nwhich will describe the available API endpoints, including their operation ID and\nparameters.\n\n.. code:: python\n\n   corpus = cli.request('RetrieveCorpus', id='...')\n\nThe result will be a Python ``dict`` containing the result of the API request.\nIf the request returns an error, an ``apistar.exceptions.ErrorResponse`` will\nbe raised.\n\nDealing with pagination\n^^^^^^^^^^^^^^^^^^^^^^^\n\nThe Arkindex client adds another helper method for paginated endpoints that\ndeals with pagination for you: ``ArkindexClient.paginate``. This method\nreturns a ``ResponsePaginator`` instance, which is a classic Python\niterator that does not perform any actual requests until absolutely needed:\nthat is, until the next page must be loaded.\n\n.. code:: python\n\n   for element in cli.paginate('ListElements', corpus=corpus['id']):\n       print(element['name'])\n\n**Warning:** Using ``list`` on a ``ResponsePaginator`` may load dozens\nof pages at once and cause a big load on the server. You can use ``len`` to\nget the total item count before spamming a server.\n\nA call to ``paginate`` may produce hundreds of sub-requests depending on the size\nof the dataset you're requesting. To accommodate with large datasets, and support\nnetwork or performance issues, ``paginate`` supports a ``retries`` parameter to\nspecify the number of sub-request it's able to run for each page in the dataset.\nBy default, the method will retry 5 times per page.\n\nYou may want to allow ``paginate`` to fail on some pages, for really big datasets\n(errors happen). In this case, you should use the optional boolean parameter\n``allow_missing_data`` (set to ``False`` by default).\n\nHere is an example of pagination on a large dataset, allowing data loss, lowering\n retries and listing the missed pages:\n\n.. code:: python\n\n   elements = cli.paginate(\n       'ListProcessElements',\n       id='XXX',\n       retries=3,\n       allow_missing_data=True,\n   )\n   for element in elements:\n       print(element['id'])\n\n   print(\"Missing pages: {elements.missing}\")\n\n\n\nUsing another server\n^^^^^^^^^^^^^^^^^^^^\n\nBy default, the API client is set to point to the main Arkindex server at\nhttps://arkindex.teklia.com. If you need or want to use this API client on\nanother server, you can use the ``base_url`` keyword argument when setting up\nyour API client:\n\n.. code:: python\n\n   cli = ArkindexClient(base_url='https://somewhere')\n\nHandling errors\n^^^^^^^^^^^^^^^\n\nAPIStar_, the underlying API client we use, does most of the error handling.\nIt will raise two types of exceptions:\n\n``apistar.exceptions.ErrorResponse``\n  The request resulted in a HTTP 4xx or 5xx response from the server.\n``apistar.exceptions.ClientError``\n  Any error that prevents the client from making the request or fetching\n  the response: invalid endpoint names or URLs, unsupported content types,\n  or unknown request parameters. See the exception messages for more info.\n\nSince this API client retrieves the endpoints description from the server\nusing the base URL, errors can occur during the retrieval and parsing of the\nAPI schema. If this happens, an ``arkindex.exceptions.SchemaError`` exception\nwill be raised.\n\nYou can handle HTTP errors and fetch more information about them using the\nexception's attributes:\n\n.. code:: python\n\n   from apistar.exceptions import ErrorResponse\n   try:\n       # cli.request ...\n   except ErrorResponse as e:\n       print(e.title)   # \"400 Bad Request\"\n       print(e.status_code)  # 400\n       print(e.content)  # Any kind of response body the server might give\n\nNote that by default, using ``repr()`` or ``str()`` on APIStar exceptions will\nnot give any useful messages; a fix in APIStar is waiting to be merged. In\nthe meantime, you can use Teklia's `APIStar fork`_::\n\n   pip install git+https://gitlab.teklia.com/arkindex/apistar.git\n\nThis will provide support for ``repr()`` and ``str()``, which will also\nenhance error messages on unhandled exceptions.\n\nExamples\n--------\n\nPrint all folders\n^^^^^^^^^^^^^^^^^\n\n.. code:: python\n\n   for folder in cli.paginate('ListElements', folder=True):\n       print(folder['name'])\n\nDownload full logs for each Ponos task in a workflow\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n.. code:: python\n\n   workflow = cli.request('RetrieveWorkflow', id='...')\n   for task in workflow['tasks']:\n       with open(task['id'] + '.txt', 'w') as f:\n           f.write(cli.request('RetrieveTaskLog', id=task['id']))\n\n.. _APIStar: http://docs.apistar.com/\n.. _APIStar fork: https://gitlab.teklia.com/arkindex/apistar\n\nLinting\n-------\n\nWe use `pre-commit <https://pre-commit.com/>`_ with `black <https://github.com/psf/black>`_ to automatically format the Python source code of this project.\n\nTo be efficient, you should run pre-commit before committing (hence the name...).\n\nTo do that, run once :\n\n.. code:: shell\n\n   pip install pre-commit\n   pre-commit install\n\nThe linting workflow will now run on modified files before committing, and will fix issues for you.\n\nIf you want to run the full workflow on all the files: `pre-commit run -a`.\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "API client for the Arkindex project",
    "version": "1.0.15",
    "project_urls": {
        "Homepage": "https://gitlab.teklia.com/arkindex/api-client"
    },
    "split_keywords": [
        "api",
        "client",
        "arkindex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7a59cbff90b16e842a8c91bc88698b5c5130a6a7ce1de5d989793d65334c21a2",
                "md5": "1e78f4b03e75b2a8bbceb582fd8be270",
                "sha256": "47e0d8ad3b63c8cffbec879b5a28b4c2b9bb4b9f9ca24e49fbdca5751a66164c"
            },
            "downloads": -1,
            "filename": "arkindex_client-1.0.15-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1e78f4b03e75b2a8bbceb582fd8be270",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 12348,
            "upload_time": "2024-03-12T11:46:39",
            "upload_time_iso_8601": "2024-03-12T11:46:39.264965Z",
            "url": "https://files.pythonhosted.org/packages/7a/59/cbff90b16e842a8c91bc88698b5c5130a6a7ce1de5d989793d65334c21a2/arkindex_client-1.0.15-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1866e5a5f6cfb72a90ee81079311be48d4d1c0cf6bdad3fb81581faf46431724",
                "md5": "e3ce676732b89c429a9004e6f0375230",
                "sha256": "a56dbf250b515349f95d009215e665bf34c52eafe110a4d6304083341dc7dc4f"
            },
            "downloads": -1,
            "filename": "arkindex-client-1.0.15.tar.gz",
            "has_sig": false,
            "md5_digest": "e3ce676732b89c429a9004e6f0375230",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 12902,
            "upload_time": "2024-03-12T11:46:41",
            "upload_time_iso_8601": "2024-03-12T11:46:41.197024Z",
            "url": "https://files.pythonhosted.org/packages/18/66/e5a5f6cfb72a90ee81079311be48d4d1c0cf6bdad3fb81581faf46431724/arkindex-client-1.0.15.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-12 11:46:41",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "arkindex-client"
}
        
Elapsed time: 3.58018s