Skip to main content

Python client library for StreamCI

Project description

PyStreamCI

Python client library for StreamCI — a connector-style API in the spirit of database drivers.

Installation

pip install pystreamci
# or editable install for development
pip install -e clients/python/

Quick Start

from pathlib import Path
import pystreamci

# Secret-key auth (recommended) — use as context manager
with pystreamci.connect("https://api.streamci.org",
                        target="my_sensors",
                        secret_key="...") as client:

    # Insert a document
    client.insert({"time": {"$date": "2025-08-04T10:00:00Z"}, "val": 42})

    # Insert many documents (auto-batched)
    client.insert_many(records, batch_size=1000)

    # Upload an NDJSON/CSV file
    client.insert_file("data.ndjson")

    # Query (returns list of dicts)
    docs = client.query({"val": {"$gte": 10}}, sort={"time": 1}, limit=50)

    # Stream large results with an iterator
    for doc in client.query_iter({"val": {"$gte": 0}}):
        process(doc)

    # Download a blob field
    blob = client.query_blob(document_id="67abc...", field="image1")

    # Download blob fields from multiple documents
    blobs = client.query_blobs_by_field(["67abc...", "89def..."], field="image1")

    # Download all blob fields from a single document
    all_blobs = client.query_blobs_by_doc(document_id="67abc...")

    # Insert with blob attachment — flexible formats accepted
    client.insert({"name": "doc"}, blobs={"photo": "/path/to/photo.jpg"})     # file path
    client.insert({"name": "doc"}, blobs={"photo": Path("photo.jpg")})        # Path object
    with open("photo.jpg", "rb") as f:
        client.insert({"name": "doc"}, blobs={"photo": f})                    # file object
        client.insert({"name": "doc"}, blobs={"photo": ("photo.jpg", f)})     # 2-tuple
        client.insert({"name": "doc"}, blobs={"photo": ("photo.jpg", f, "image/jpeg")})  # 3-tuple

    # Update and delete
    client.update({"val": 42}, {"val": 43})
    client.delete({"val": 43})

# Password auth
client = pystreamci.connect("https://api.streamci.org",
                            target="my_sensors",
                            username="user1", password="pass123")

API Reference

pystreamci.connect(host, target, *, secret_key=None, username=None, password=None, max_retries=3, timeout=30.0, log_level="WARNING", log_file=None)

Returns a StreamCIClient. Auth type is inferred from the provided credentials.

StreamCIClient

Method Description
insert(data, blobs=None) Insert a single document (with optional blob attachments)
insert_many(data, datatype, batch_size) Batch insert (auto-splits at batch_size)
insert_file(file_path, datatype) Upload NDJSON/CSV file
update(filter, data, blobs=None) Update matching documents
delete(filter) Delete matching documents
query(query, *, project, sort, limit) Query and return list
query_iter(query, *, project, sort, limit) Query with streaming iterator
query_blob(document_id, field) Download blob field as bytes
query_blobs_by_field(document_ids, field) Download blob field from multiple documents
query_blobs_by_doc(document_id, *, ignore_errors) Download all blob fields from a document (including nested blob fields); returns dot-notation keys

pystreamci.make_date(dt)

Convert a datetime or ISO-8601 string to a StreamCI {"$date": "..."} object.

pystreamci.make_date("2025-08-04T10:00:00Z")
pystreamci.make_date(datetime(2025, 8, 4, 10, 0, tzinfo=timezone.utc))

Exceptions

Exception When raised
StreamCIConnectionError Connection failed after retries
StreamCIAuthError Authentication/authorization error (subclass of StreamCIRequestError)
StreamCIRequestError Server returned status=0
StreamCIParseError Response parsing failed
StreamCITimeoutError Request timed out
StreamCIValidationError Invalid client-side input

query() vs query_iter()

Method Returns Use when
query(...) list[Document] Result fits in memory; need random access
query_iter(...) Iterator[Document] Large result sets; process one doc at a time

query_iter() uses HTTP streaming (stream=True) and parses NDJSON line-by-line, keeping memory usage constant regardless of result size.

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

pystreamci-0.4.2.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

pystreamci-0.4.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file pystreamci-0.4.2.tar.gz.

File metadata

  • Download URL: pystreamci-0.4.2.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pystreamci-0.4.2.tar.gz
Algorithm Hash digest
SHA256 54d9270739adcd5db48f7f4ba1fd68dc4bc60f6b4420645dec07a8f57f4e7e52
MD5 98c5b4876d34d51f7e1f3c762e164301
BLAKE2b-256 1baf7247320d2a3f2e95aac1d0d2bd67bebdb195437494e79b342b989d87bf4b

See more details on using hashes here.

File details

Details for the file pystreamci-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: pystreamci-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pystreamci-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed4471e6a7c0eff5913883b2707f8b753676aeffe0f9f34c0d507a2fbc590e4
MD5 ad989536494e0eb321aa55d216ce29d4
BLAKE2b-256 3f4f57af54e3eb0a51cda7851c34d9a0ce8a9e4d561bf769ce3714816fcf790d

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