|
1 | 1 | # stackcoin-python |
2 | 2 |
|
3 | | -Python client for interacting with the StackCoin HTTP API. |
| 3 | +Python library for the StackCoin API. Provides a typed async REST client and a WebSocket gateway for real-time events. |
4 | 4 |
|
5 | | -https://pypi.org/project/stackcoin/ |
| 5 | +## Install |
6 | 6 |
|
7 | 7 | ```sh |
8 | 8 | pip install stackcoin |
9 | 9 | ``` |
10 | 10 |
|
| 11 | +Requires Python 3.13+. Dependencies: `httpx`, `pydantic>=2`, `websockets`. |
| 12 | + |
| 13 | +## Quick start |
| 14 | + |
| 15 | +```python |
| 16 | +import asyncio |
| 17 | +import stackcoin |
| 18 | + |
| 19 | +async def main(): |
| 20 | + async with stackcoin.Client(token="...") as client: |
| 21 | + me = await client.get_me() |
| 22 | + print(f"{me.username}: {me.balance} STK") |
| 23 | + |
| 24 | + events = await client.get_events() |
| 25 | + for event in events: |
| 26 | + print(f"[{event.type}] {event.data}") |
| 27 | + |
| 28 | +asyncio.run(main()) |
| 29 | +``` |
| 30 | + |
| 31 | +## Gateway (real-time events) |
| 32 | + |
| 33 | +```python |
| 34 | +import stackcoin |
| 35 | + |
| 36 | +gateway = stackcoin.Gateway(token="...") |
| 37 | + |
| 38 | +@gateway.on("transfer.completed") |
| 39 | +async def on_transfer(event: stackcoin.TransferCompletedEvent): |
| 40 | + print(f"Transfer of {event.data.amount} STK from #{event.data.from_id} to #{event.data.to_id}") |
| 41 | + |
| 42 | +@gateway.on("request.accepted") |
| 43 | +async def on_accepted(event: stackcoin.RequestAcceptedEvent): |
| 44 | + print(f"Request #{event.data.request_id} accepted") |
| 45 | + |
| 46 | +await gateway.connect() |
| 47 | +``` |
| 48 | + |
11 | 49 | ## Examples |
12 | 50 |
|
13 | | -- `./examples/basic_usage.py`, bare bones creation of `AuthenticatedClient`, checking balance, making some requests, etc. |
14 | | -- `./examples/simple_cli.py`, thorough usage of the API in a bare-bones command line interface. |
| 51 | +- `examples/basic_usage.py` -- REST client basics (balance, requests, transactions) |
| 52 | +- `examples/simple_cli.py` -- interactive REPL with live gateway events |
15 | 53 |
|
16 | | ---- |
| 54 | +## Development |
| 55 | + |
| 56 | +Models are generated from the StackCoin OpenAPI spec using `datamodel-codegen`: |
| 57 | + |
| 58 | +```sh |
| 59 | +STACKCOIN_ROOT=/path/to/StackCoin just generate |
| 60 | +``` |
17 | 61 |
|
18 | | -This package is generated by running [openapi-python-client](https://github.com/openapi-generators/openapi-python-client) against [StackCoin's OpenAPI specification](https://stackcoin.world/swaggerui), the `./stackcoin` directory in this repository is the package, and is generated by running `just generate` (or running the commands under `generate` in the `Justfile`). |
| 62 | +This regenerates `stackcoin/stackcoin/models.py` from `openapi.json`. |
0 commit comments