Skip to main content

JsonBank.io Python SDK

Project description

JsonBank.io Python SDK

The official repository for the jsonbank.io Python SDK.

Documentation | PyPI | GitHub

Installation

pip install jsonbank

Usage

The client can be initialized with or without API keys. Keys are only required to access protected/private documents.

from jsonbank import JsonBank

# Initialize without keys (public content only)
jsb = JsonBank()

# Get json content of a public file
data = jsb.get_content("jsonbank/sdk-test/index")
print(data["author"])  # -> "jsonbank"

# Get a json file from GitHub (default branch)
pkg = jsb.get_github_content("jsonbankio/jsonbank-js/package.json")

# Get document metadata (typed, attribute access)
meta = jsb.get_document_meta("jsonbank/sdk-test/index")
print(meta.id, meta.content_size.number)

Authenticated requests

from jsonbank import JsonBank

# Flat keyword arguments are the primary style
jsb = JsonBank(public_key="JSB_PUBLIC_KEY", private_key="JSB_PRIVATE_KEY")

# Optionally verify the keys
auth = jsb.authenticate()
print("Authenticated as:", auth.username)

# Read your own content
content = jsb.get_own_content("sdk-test/index.json")

JsonBank also works as a context manager, which closes the underlying HTTP session on exit:

with JsonBank(public_key="...", private_key="...") as jsb:
    jsb.update_own_document("sdk-test/index.json", {"updated": True})

Flexible inputs

Write methods accept keyword arguments, a plain dict, or a typed body object — whichever you prefer. content may be a JSON string or any JSON-serializable object (it is serialized for you):

from jsonbank import JsonBank, CreateDocumentBody

jsb = JsonBank(public_key="...", private_key="...")

# keyword arguments
jsb.create_document(name="movie.json", project="sdk-test", content={"title": "Avatar"})

# a plain dict
jsb.create_document({"name": "movie.json", "project": "sdk-test", "content": "{}"})

# a typed body
jsb.create_document(CreateDocumentBody(name="movie.json", project="sdk-test", content={}))

Errors

Failed requests raise JsonBankError, which carries a code and message:

from jsonbank import JsonBank, JsonBankError

jsb = JsonBank()
try:
    jsb.get_content("does/not/exist")
except JsonBankError as err:
    print(err.code, "-", err.message)  # e.g. "notFound - ..."

Configuration

Keys can be supplied in whichever form is convenient:

from jsonbank import JsonBank, Keys, InitConfig

JsonBank(public_key="...", private_key="...")            # flat kwargs (recommended)
JsonBank(keys={"public": "...", "private": "..."})       # dict (also accepts pub/prv)
JsonBank(keys=Keys(public="...", private="..."))         # typed Keys
JsonBank(config=InitConfig(host="...", keys=Keys(...)))  # full InitConfig

Testing

Create an .env file in the root of the project and add the following variables:

JSB_HOST="https://api.jsonbank.io"
JSB_PUBLIC_KEY="your public key"
JSB_PRIVATE_KEY="your private key"

Install the dev dependencies and run the tests:

pip install -e ".[dev]"
pytest

The offline helper tests need neither network nor keys:

pytest tests/test_functions.py

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

jsonbank-0.1.0.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

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

jsonbank-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file jsonbank-0.1.0.tar.gz.

File metadata

  • Download URL: jsonbank-0.1.0.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for jsonbank-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6a3c3f8c5b344eaeac6afdd539c77a8c29f6df55dfa0e6474b6de8b66b188c44
MD5 c6d74bb1ab6d408a54131e1ff2c36992
BLAKE2b-256 b32db2cb0a198fad82dacd0c7cb4f496adc6dc658f48cb7ede29739bb1e009df

See more details on using hashes here.

File details

Details for the file jsonbank-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jsonbank-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for jsonbank-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b384222b85f205a482719c11b6682cd474872184e6249e546daf5d08f07af321
MD5 18eec72b20e21c14b86dad89e28523c2
BLAKE2b-256 70b6ec6866386d15ce6d343417f46aee49bd5920fd0e160911b844470eeb062f

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