Skip to main content

API client for the Arkindex project

Project description

API documentation available at https://arkindex.gitlab.io/api-client/

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

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

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 page in cli.paginate('ListCorpusPages', id=corpus['id']):
    print(page['display_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 all 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.

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

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.

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

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.

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)

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.

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

Examples

Create transcriptions in bulk

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)

Import transcriptions for a page from files in PAGE XML format

cli.send_xml('ImportTranskribusTranscriptions', id='PAGE_ID', body=open('file.xml', 'rb'))

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

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-0.9.6.tar.gz (23.7 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-0.9.6-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: arkindex-client-0.9.6.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9

File hashes

Hashes for arkindex-client-0.9.6.tar.gz
Algorithm Hash digest
SHA256 62316902075f23e0755bdd0e6f64f6ddd18dfe0746d10a0e10324496ceccd0f9
MD5 4bf7bb9502278dcde636cfe1c0eedba1
BLAKE2b-256 36f671fd6d849578c98e4b5febb5fc77f6eb2e80e19c1f08325f0574a2f644c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: arkindex_client-0.9.6-py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9

File hashes

Hashes for arkindex_client-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d3f9da1386f3e4cc7a3eee904bc066561cf5806c822a4c77dc7859d86031386d
MD5 b6e9ab6b5d4fa1c6caf560b915c5bfd5
BLAKE2b-256 f4803a9b8041fd7f63c3553c0056b793f706460d66da2cd4fb89e57526970212

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