Metadata-Version: 2.5
Name: bazhuayu-client
Version: 0.2.10
Summary: Official Python SDK for Bazhuayu DataHub: discover, run and consume Data Apps
Project-URL: Homepage, https://www.bazhuayu.com
Author: Bazhuayu DataHub Team
License: MIT
License-File: LICENSE
Keywords: bazhuayu,data-app,datahub,sdk
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27
Description-Content-Type: text/markdown

# bazhuayu-client-python — the official DataHub Python SDK

The official Python client for Bazhuayu DataHub (`bazhuayu-client`): discover, run and consume Data Apps.
A small self-contained project with zero platform-side dependencies (`httpx` is the only runtime
dependency) that speaks only the platform's public `/v1` REST API.
Repositories follow the `<brand>-client-<language>` naming; clients for other languages sit alongside
(e.g. bazhuayu-client-js).

## Installation

```bash
pip install bazhuayu-client
```

## Usage

```python
from bazhuayu_client import Client

# base_url defaults to the production endpoint https://api-datahub.bazhuayu.com;
# pass it explicitly for local / staging environments
with Client(api_key="demo-key") as client:
    # Discover
    result = client.search("reviews", limit=10)
    detail = client.get_app("demo/reviews-store-query")   # or the card's app_id

    # Run (wait for the terminal state) and consume
    run = client.call("demo/partner-reviews-api", {"product": "p-9001"})
    for record in client.iterate_records(run["run_id"]):
        print(record["content"], record["rating"])

    # Inventory and reconciliation
    for r in client.iterate_runs(run_kind="production", created_from="2026-08-01T00:00:00Z"):
        print(r["run_id"], r["state"], r["billing"])
    print(client.billing(group_by="data_app", tz_offset=480))   # spend per local day / per App

    # Result data is kept for 90 days by default; mark datasets you want to keep
    client.set_dataset_retention(run["dataset_id"], retained=True)
```

`call()` starts a run and polls until it reaches a terminal state; for non-blocking semantics use
`run()` to get a `run_id`, then `get_run()` / `cancel()` yourself. Apps shared with you
point-to-point do not appear in marketplace results; query them with `search(shared_with="me")`.
All methods and the error mapping live in `src/bazhuayu_client/client.py`.

`iterate_records()` and `iterate_dataset_records()` automatically follow the opaque
`pagination.next_cursor` returned by cursor-capable DataHub deployments. This keeps all pages on
the same server snapshot, including when filtering produces an empty page. They page by offset
until a response first carries a cursor (older servers and local storage never send one, so they
stay on offset paging); from then on they follow cursors only and never go back to offset. For manual paging, pass that opaque value back as `cursor=` to `get_records()` or
`get_dataset_records()`; cursor requests omit `offset`, and combining a cursor with a nonzero
offset is rejected.

`get_app(app_id, version=...)` reads a historical version's contract, the same version string
`run(version=...)` pins. `list_app_versions(app_id)` lists the versions open to you (newest
first; `latest` marks the default), and `get_app_readme(app_id, version=...)` returns the App's
README as Markdown text.

## Run logs

Only code-kind runs have logs; for api-kind runs the endpoint returns an empty list with
`complete=true`. A code-kind run's log is the stdout and stderr of the App's code, and it is
visible only to whoever started the run (anyone else, the App's owner included, gets 404).

```python
# Tail a run that is still in progress; stops once the log is complete
for line in client.iterate_run_logs(run["run_id"]):
    print(line["time"], line["source"], line["text"])

# Or read one page: {items, pagination, complete, truncated}
page = client.get_run_logs(run["run_id"], offset=0, limit=1000)
```

`iterate_run_logs()` reads on from the last line number + 1 and long-polls with `wait` (at most
20 seconds per request) until `complete` is true; `follow=False` reads only the lines that already
exist. `source` is `app` for the App's own output and `platform` for lines the platform writes
(attempt separators, the size-limit marker). `truncated=true` means the output exceeded the size
limit: the middle was dropped, the beginning and the end of each attempt are kept. Values the
platform handed to the run (environment values, inputs marked sensitive) and common credential
patterns are masked before the log is stored. The endpoint also serves plain text with
`format=text`, which the SDK does not wrap.

App references come in two forms, accepted interchangeably by `get_app()` / `run()` / `call()` and
by the `data_app` filter of the run listings: the two-part `<namespace>/<app_name>` reference
(taken from the card's `namespace` and `app_name`, human-readable, breaks once the publisher renames
the App); and the stable identifier `app_id` (`app_<hex>`, taken from the card's / detail's `app_id`
field, immune to renames). For long-term integrations such as config files or scheduled jobs, pin
the `app_id`.

## Configuration

When `Client()` is constructed without `base_url` / `api_key`, they fall back to the
`BAZHUAYU_BASE_URL` / `BAZHUAYU_API_KEY` environment variables (template in `.env.example`; the SDK
does not load `.env` itself, the caller does), then to the production endpoint
`https://api-datahub.bazhuayu.com` / anonymous access.

## Tests

```bash
uv run pytest -q    # offline unit tests (MockTransport, no server required)
```
