Skip to main content

API client for the Arkindex project

Project description

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

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:

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:

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.

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.

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.

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:

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:

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

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.

Examples

Download full logs for each Ponos task in a workflow

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

Linting

We use pre-commit with 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 :

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arkindex-client-1.0.3.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arkindex_client-1.0.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file arkindex-client-1.0.3.tar.gz.

File metadata

  • Download URL: arkindex-client-1.0.3.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.12

File hashes

Hashes for arkindex-client-1.0.3.tar.gz
Algorithm Hash digest
SHA256 0d80efe50206a9022c101b53742104d7b11bde83804c0ab2fe123665e3f681a9
MD5 ed6a59e42f4462010116f1dc271f260d
BLAKE2b-256 ddb1340deb14ad2e41e318dee191ab69bc8a7a40c32d4f33918d66f3481abfbe

See more details on using hashes here.

File details

Details for the file arkindex_client-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: arkindex_client-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.12

File hashes

Hashes for arkindex_client-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a9f24b191b59e945cdc7d9cf50f233fee32679e7b3548c86690fb4bda73c9e4b
MD5 37171aee2c53d0b3ddd46ec7c418ba2d
BLAKE2b-256 f78ed9b5bdd8cbb70bda52f61d5011a56999c7445384e8000282ca74ffccfc1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page