Skip to main content

MiPasa connector for Swarm (BZZ) distributed storage network

Project description

MiPasa Swarm Connector

Tests Status Integration Tests Status

This Python library implements connecting to Swarm (BZZ) distributed storage network.

Learn more about MiPasa: https://www.mipasa.com/

Learn more about Swarm: https://www.ethswarm.org/

Optional dependencies

  • mipasa_swarm_connector[pandas] is required if you wish to read files as CSV or Parquet.
  • mipasa_swarm_connector[parquet] is required if you wish to read files as Parquet. More specific versions of this dependency exist:
    • mipasa_swarm_connector[parquet-pyarrow]
    • mipasa_swarm_connector[parquet-fastparquet]

Specifying the Swarm node address

In order to work with Swarm, you will need to specify a Bee node address.

For production environments, we advise doing so via setting the environment variable BEE_GATEWAY_URL to the HTTP endpoint of the chosen node.

For development, you can also specify the node address directly:

from mipasa_swarm_connector import SwarmConnection

content = SwarmConnection('http://localhost:1633').read_file('36f0830b0ece6273a50cc0ff58c4597883e8e41ea9c8ddabb0d87d6b0ca95a1a')
print(repr(content))

Reading files

Simplest usage

from mipasa_swarm_connector import SwarmConnection, SwarmAPIError

try:
    content = SwarmConnection().read_file('<your_swarm_file_hash_here>')
    print(repr(content))
except SwarmAPIError as e:
    print('Failed to fetch, status: %d' % e.status_code)

This code will read the contents of the specified file on Swarm.

By default, the exact file type will be autodetected:

  • If the file has a known file type (which is application/json, text/csv, application/vnd.apache.parquet and application/parquet) it will be reintepreted as a JSON object or a Pandas DataFrame.
  • Otherwise, the file will be downloaded as bytes.

Any use of read_file can raise SwarmAPIError, which, generally, means that a file is not found (status code 404).

Read a file as CSV

from mipasa_swarm_connector import SwarmConnection, SwarmTypeError

try:
  dataframe = SwarmConnection().read_file('<your_swarm_file_hash_here>', as_type='csv', verify_type=True)
  print(repr(dataframe))
except SwarmTypeError as e:
  print('Expected type %s, got type %s' % (e.expected_type, e.actual_type))

This code will read the contents of the specified file on Swarm as a Pandas DataFrame, expecting it to be in CSV format.

verify_type=True can be passed (but not required) in order to check whether file's MIME type or filename matches CSV.

Read a file as Parquet

from mipasa_swarm_connector import SwarmConnection

dataframe = SwarmConnection().read_file('<your_swarm_file_hash_here>', as_type='parquet')
print(repr(dataframe))

When type-checking for Parquet, two MIME types are recognized:

  • application/vnd.apache.parquet
  • application/parquet

Read a file as JSON

from mipasa_swarm_connector import SwarmConnection

dataframe = SwarmConnection().read_file('<your_swarm_file_hash_here>', as_type='json')
print(repr(dataframe))

Writing files

from mipasa_swarm_connector import SwarmConnection, SwarmAPIError

try:
    swarm_hash = SwarmConnection().write_file(b'binary-content')
except SwarmAPIError as e:
    print('Failed to upload, status: %d' % e.status_code)

This code will attempt uploading the specified bytes to Swarm.

By default, the exact type of the uploaded file will be autodetected:

  • If bytes or bytearray is passed, then the uploaded file type will be application/octet-stream.
  • If a string is passed, it will be encoded as UTF-8 and the file type will be text/plain.
  • If a Pandas DataFrame is passed, then the uploaded file type will be text/csv and the DataFrame will be formatted as CSV.
  • If any other object is passed, JSON encoding will be attempted. If it succeeds, then the uploaded file type will be application/json.

Additionally, if your content is a Pandas DataFrame, you may specify as_type='parquet' in order to automatically format this file as Parquet (the file type will be application/vnd.apache.parquet).

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

mipasa_swarm_connector-1.0.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

mipasa_swarm_connector-1.0.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file mipasa_swarm_connector-1.0.1.tar.gz.

File metadata

  • Download URL: mipasa_swarm_connector-1.0.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.5

File hashes

Hashes for mipasa_swarm_connector-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8e0991d929ad8d4061d58c027ae45b8c40337e8450b77c6dbc2c80aee714dae3
MD5 9879715ac0cb72c988d1a5a8362b6e9a
BLAKE2b-256 73a1e2e5b180278659b8472f788b4fb312daed35b2212eb93ec0f1ae3528ab63

See more details on using hashes here.

File details

Details for the file mipasa_swarm_connector-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mipasa_swarm_connector-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01d5d598a4cbd90120ce847d1ddc9e1a9d078a73c2de6841f2410b1b6c92b218
MD5 83e38045b61de439997f307fe8d293fa
BLAKE2b-256 dd4dd1f9a805425ae15b641ac1028dc8664e5c865909010a9fc140963c3ee5d2

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