|
| 1 | +"""SDK configuration and shared runtime state.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey |
| 10 | + |
| 11 | +from intentproof.http_exporter import HttpExporter, resolve_ingest_url |
| 12 | +from intentproof.keys import ensure_dir, load_or_create_keypair |
| 13 | +from intentproof.outbox import Outbox |
| 14 | +from intentproof.signing import load_private_key |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from intentproof.signing import Ed25519PublicKey |
| 18 | + |
| 19 | +SDK_VERSION = "python@0.1.0" |
| 20 | + |
| 21 | + |
| 22 | +def default_data_dir() -> Path: |
| 23 | + """Default SDK data directory (resolved lazily for container imports).""" |
| 24 | + return Path.home() / ".intentproof" / "sdk-python" |
| 25 | + |
| 26 | +_instance_private_key: Ed25519PrivateKey | None = None |
| 27 | +_instance_id: str | None = None |
| 28 | +_tenant_id: str = "tnt_default" |
| 29 | +_outbox: Outbox | None = None |
| 30 | +_exporter: HttpExporter | None = None |
| 31 | +_data_dir: Path | None = None |
| 32 | + |
| 33 | + |
| 34 | +def configure( |
| 35 | + *, |
| 36 | + db_path: str | None = None, |
| 37 | + tenant_id: str | None = None, |
| 38 | + data_dir: str | Path | None = None, |
| 39 | + ingest_url: str | None = None, |
| 40 | +) -> None: |
| 41 | + global _instance_private_key, _instance_id, _tenant_id, _outbox, _exporter, _data_dir |
| 42 | + |
| 43 | + prev_exporter = _exporter |
| 44 | + prev_outbox = _outbox |
| 45 | + |
| 46 | + new_data_dir = Path(data_dir) if data_dir else default_data_dir() |
| 47 | + ensure_dir(new_data_dir) |
| 48 | + |
| 49 | + kp = load_or_create_keypair(new_data_dir) |
| 50 | + new_private_key = load_private_key(kp.private_key) |
| 51 | + new_instance_id = kp.instance_id |
| 52 | + new_tenant_id = ( |
| 53 | + tenant_id |
| 54 | + or os.environ.get("INTENTPROOF_TENANT_ID", "").strip() |
| 55 | + or "tnt_default" |
| 56 | + ) |
| 57 | + |
| 58 | + resolved_db = db_path or os.environ.get("INTENTPROOF_OUTBOX_PATH", "").strip() |
| 59 | + if not resolved_db: |
| 60 | + resolved_db = str(new_data_dir / "outbox.db") |
| 61 | + new_outbox = Outbox(resolved_db) |
| 62 | + |
| 63 | + ingest = resolve_ingest_url(ingest_url) |
| 64 | + new_exporter = HttpExporter(ingest) if ingest else None |
| 65 | + |
| 66 | + if prev_exporter is not None: |
| 67 | + prev_exporter.flush() |
| 68 | + if prev_outbox is not None: |
| 69 | + prev_outbox.close() |
| 70 | + |
| 71 | + _data_dir = new_data_dir |
| 72 | + _instance_private_key = new_private_key |
| 73 | + _instance_id = new_instance_id |
| 74 | + _tenant_id = new_tenant_id |
| 75 | + _outbox = new_outbox |
| 76 | + _exporter = new_exporter |
| 77 | + |
| 78 | + |
| 79 | +def flush() -> None: |
| 80 | + if _exporter is not None: |
| 81 | + _exporter.flush() |
| 82 | + |
| 83 | + |
| 84 | +def get_outbox() -> Outbox: |
| 85 | + if _outbox is None: |
| 86 | + raise RuntimeError("SDK not configured: call configure() before use") |
| 87 | + return _outbox |
| 88 | + |
| 89 | + |
| 90 | +def get_instance_id() -> str: |
| 91 | + if _instance_id is None: |
| 92 | + raise RuntimeError("SDK not configured: call configure() before get_instance_id()") |
| 93 | + return _instance_id |
| 94 | + |
| 95 | + |
| 96 | +def get_private_key() -> Ed25519PrivateKey: |
| 97 | + if _instance_private_key is None: |
| 98 | + raise RuntimeError("SDK not configured: call configure() before signing") |
| 99 | + return _instance_private_key |
| 100 | + |
| 101 | + |
| 102 | +def get_tenant_id() -> str: |
| 103 | + return _tenant_id |
| 104 | + |
| 105 | + |
| 106 | +def get_exporter() -> HttpExporter | None: |
| 107 | + return _exporter |
| 108 | + |
| 109 | + |
| 110 | +def get_public_key() -> "Ed25519PublicKey": |
| 111 | + return get_private_key().public_key() |
0 commit comments