|
| 1 | +# BabelQueue for Python |
| 2 | + |
| 3 | +[](https://github.com/BabelQueue/babelqueue-python/actions/workflows/ci.yml) |
| 4 | +[](https://pypi.org/project/babelqueue/) |
| 5 | +[](https://pypi.org/project/babelqueue/) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +> **Polyglot Queues, Simplified.** Read and write the canonical BabelQueue message |
| 9 | +> envelope from Python — so your Python services (AI/ML, data processing, …) |
| 10 | +> exchange messages with Laravel, Symfony, Go, .NET and Node over one strict JSON |
| 11 | +> format, on the broker you already run. |
| 12 | +
|
| 13 | +This is the framework-agnostic **Python core**: the wire-envelope codec, |
| 14 | +contracts, and dead-letter helpers — **zero runtime dependencies** (standard |
| 15 | +library only). The full standard is documented at |
| 16 | +**[babelqueue.com](https://babelqueue.com)**. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install babelqueue |
| 22 | +``` |
| 23 | + |
| 24 | +Requires Python `>=3.9`. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```python |
| 29 | +from babelqueue import EnvelopeCodec |
| 30 | + |
| 31 | +# Produce — build the canonical envelope and publish the JSON to your broker. |
| 32 | +envelope = EnvelopeCodec.make("urn:babel:orders:created", {"order_id": 1042}) |
| 33 | +body = EnvelopeCodec.encode(envelope) # -> UTF-8 JSON string |
| 34 | +# redis.rpush("queues:orders", body) / channel.basic_publish(body=body, ...) |
| 35 | + |
| 36 | +# Consume — decode a message produced by ANY BabelQueue SDK. |
| 37 | +incoming = EnvelopeCodec.decode(body) |
| 38 | +urn = incoming["job"] # "urn:babel:orders:created" |
| 39 | +data = incoming["data"] # {"order_id": 1042} |
| 40 | +trace_id = incoming["trace_id"] # correlate across services |
| 41 | +``` |
| 42 | + |
| 43 | +The envelope is identical to every other SDK's: |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "job": "urn:babel:orders:created", |
| 48 | + "trace_id": "…", |
| 49 | + "data": { "order_id": 1042 }, |
| 50 | + "meta": { "id": "…", "queue": "default", "lang": "python", "schema_version": 1, "created_at": 1749132727000 }, |
| 51 | + "attempts": 0 |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Typed messages (optional) |
| 56 | + |
| 57 | +```python |
| 58 | +from babelqueue import EnvelopeCodec, PolyglotMessage |
| 59 | + |
| 60 | +class OrderCreated: # structurally a PolyglotMessage |
| 61 | + def __init__(self, order_id: int): |
| 62 | + self.order_id = order_id |
| 63 | + def get_babel_urn(self) -> str: |
| 64 | + return "urn:babel:orders:created" |
| 65 | + def to_payload(self) -> dict: |
| 66 | + return {"order_id": self.order_id} |
| 67 | + |
| 68 | +envelope = EnvelopeCodec.from_message(OrderCreated(1042), queue="orders") |
| 69 | +``` |
| 70 | + |
| 71 | +Continue an existing trace by adding `get_babel_trace_id(self) -> str | None` |
| 72 | +(see `HasTraceId`), or pass `trace_id=` to `EnvelopeCodec.make`. |
| 73 | + |
| 74 | +### Dead-letter |
| 75 | + |
| 76 | +```python |
| 77 | +from babelqueue import dead_letter |
| 78 | + |
| 79 | +dlq = dead_letter.annotate(envelope, "failed", "orders", attempts=3, error="boom") |
| 80 | +# publish `EnvelopeCodec.encode(dlq)` to the "orders.dlq" queue |
| 81 | +``` |
| 82 | + |
| 83 | +## What's here vs. coming |
| 84 | + |
| 85 | +- **Now (this package):** the codec, contracts, dead-letter and unknown-URN |
| 86 | + helpers, plus the shared conformance fixtures. Bring your own broker client. |
| 87 | +- **Next (planned):** a built-in runtime — `BabelQueue(broker_url=...)` with an |
| 88 | + `@app.handler("urn:…")` decorator over `redis`/`pika` — and **Celery** / **Django** |
| 89 | + adapters. Install via extras (`babelqueue[redis]`, `babelqueue[celery]`, …). |
| 90 | + |
| 91 | +## Testing |
| 92 | + |
| 93 | +```bash |
| 94 | +pip install -e ".[dev]" |
| 95 | +pytest |
| 96 | +# (or, dependency-free) python -m unittest discover -s tests |
| 97 | +``` |
| 98 | + |
| 99 | +## License |
| 100 | + |
| 101 | +MIT © Muhammet Şafak. See [LICENSE](LICENSE). |
0 commit comments