Python client for the Keboola Storage API
Client for using Keboola Connection Storage API. This API client provides client methods to get data from KBC and store data in KBC. The endpoints for working with buckets, tables and workspaces are covered.
Install
pip install kbcstorage
or
pip install git+https://github.com/keboola/sapi-python-client.git
Client Class Usage
from kbcstorage.client import Client, StorageApiToken
client = Client('https://connection.keboola.com', StorageApiToken('your-token'))
# get table data into local file
client.tables.export_to_file(table_id='in.c-demo.some-table', path_name='/data/')
# save data
client.tables.create(name='some-table-2', bucket_id='in.c-demo', file_path='/data/some-table')
# list buckets
client.buckets.list()
# list bucket tables
client.buckets.list_tables('in.c-demo')
# get table info
client.tables.detail('in.c-demo.some-table')
Authentication
The token argument takes an authentication strategy, either StorageApiToken or BearerToken:
from kbcstorage.client import Client, BearerToken, StorageApiToken
# Storage API token, sent as `X-StorageApi-Token`
client = Client('https://connection.keboola.com', StorageApiToken('your-token'))
# Programmatic token, sent as `Authorization: Bearer` with the project id in `X-KBC-ProjectId`
client = Client('https://connection.keboola.com', BearerToken('kbc_at_...', project_id=1234))
A bare string is also accepted and is equivalent to StorageApiToken('your-token'). It is fully
supported and not deprecated:
client = Client('https://connection.keboola.com', 'your-token')
Storage API tokens are bound to a project. Programmatic tokens (kbc_at_* session tokens,
kbc_pat_* personal access tokens) are not, which is why BearerToken requires the project id.
All three forms work for directly constructed endpoints too, and the token is used as given — the
client never refreshes or decodes it, so an expired token surfaces as a requests.HTTPError with a
401.
Breaking change in 0.11.0
A bare string starting with kbc_at_ or kbc_pat_ now raises ValueError at construction. Before
0.11.0 it was sent as X-StorageApi-Token, which the backend rejects with a 401 — the failure moved
from request time to construction time and the message names the fix. Wrap such tokens in
BearerToken(token, project_id).
Endpoint Classes Usage
from kbcstorage.auth import StorageApiToken
from kbcstorage.tables import Tables
from kbcstorage.buckets import Buckets
token = StorageApiToken('your-token')
tables = Tables('https://connection.keboola.com', token)
# get table data into local file
tables.export_to_file(table_id='in.c-demo.some-table', path_name='/data/')
# save data
tables.create(name='some-table-2', bucket_id='in.c-demo', file_path='/data/some-table')
# list buckets
buckets = Buckets('https://connection.keboola.com', token)
buckets.list()
# list bucket tables
buckets.list_tables('in.c-demo')
# get table info
tables.detail('in.c-demo.some-table')
Development
Local environment
The package requires Python 3.10 or newer (workspaces.load_tables uses int | str annotations); CI builds
and tests on 3.11.
Create a virtual environment and install the package into it in editable mode:
python3 -m venv .venv
.venv/bin/pip install -e ".[dev]"
pyproject.toml declares the test dependencies alongside the runtime ones, and the dev extra adds
the linter, so this single command covers everything. The -e flag keeps kbcstorage resolving to
the working tree, so edits take effect without reinstalling.
Prefixing commands with .venv/bin/ always uses the environment. Activating it instead makes the
plain python and pip commands resolve there for the rest of the shell session:
source .venv/bin/activate
On Windows the paths are .venv\Scripts\python and .venv\Scripts\activate.
Editor setup
Editors and IDEs resolve imports through a configured interpreter. Until yours is pointed at the virtual environment, every third-party import is reported as unresolved. The interpreter path is:
.venv/bin/python
| Editor | Where to set it |
|---|---|
| VS Code | Python: Select Interpreter |
| PyCharm | Settings → Project → Python Interpreter → Add → Existing environment |
| Zed | toolchain: select |
| Vim, Neovim, Emacs | the interpreter or venv option of your LSP client |
Most editors scan for a directory named .venv or venv in the project root, so keeping the default
name avoids manual configuration in the common case. A restart of the language server is often
needed before existing errors clear.
Checks
Lint. CI runs this before the tests, so a failure stops the build before anything is executed:
.venv/bin/python -m flake8
Unit tests, which mock all API responses and need no credentials:
.venv/bin/python -m unittest discover -s tests/mocks -t .
The functional tests run against a real project and are covered below.
Tests
Create .env file according to the .env.template file and run the tests with:
$ docker compose run --rm -e KBC_TEST_TOKEN -e KBC_TEST_API_URL ci -m unittest discover
Contribution Guide
The client is far from supporting the entire API, all contributions are very welcome. New API endpoints should
be implemented in their own class extending Endpoint. Naming conventions should follow existing naming conventions
or those of the API. If the method contains some processing of the request or
response, consult the corresponding PHP implementation for
reference. New code should be covered by tests.
Note that if you submit a PR from your own forked repository, the automated functional tests will fail.
This is expected for security reasons, please do send the PR anyway.
Either run the tests locally (set KBC_TEST_TOKEN (your token to test project) and
KBC_TEST_API_URL (https://connection.keboola.com) variables) or ask for access. In case, you need a
project for local testing, feel free to ask for one.
The recommended workflow for making a pull request is:
git clone https://github.com/keboola/sapi-python-client.git
git checkout master
git pull
git checkout -b my-new-feature
# work on branch my-new-feature
git push origin my-new-feature:my-new-feature
This will create a new branch which can be used to make a pull request for your new feature.
License
MIT licensed, see LICENSE file.
Release files for kbcstorage 0.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kbcstorage-0.11.0.tar.gz | 47.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kbcstorage-0.11.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 78.1 kB
Release files / kbcstorage-0.11.0.tar.gz
| Download URL | kbcstorage-0.11.0.tar.gz |
|---|---|
| Size | 47.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9deb8f99939d6e57c87a4665a09287fc24551fce85acd06cb67efbef7f70c73
|
|
BLAKE2b-256 checksum How to use checksums |
5e3ec9d89c0b18ab92e091f1d3a60fe830d2868e3b6c52a2628bf11cfea497e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / kbcstorage-0.11.0-py3-none-any.whl
| Download URL | kbcstorage-0.11.0-py3-none-any.whl |
|---|---|
| Size | 31.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79fc868bd0b0a7ddef73348bbd3511b4dc8512966abf79031f069651817f3a1b
|
|
BLAKE2b-256 checksum How to use checksums |
d5432b278b0c4784dda7e3b0ee9a800e5c576a369816d06a302272982688f6e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|