Metadata-Version: 2.4
Name: spermit-sdk
Version: 0.1.0
Summary: Dependency-free Python client for the Spermit platform API
Author-email: Intecion Group <karol.gajda@intecion.com>
Maintainer-email: Intecion Group <karol.gajda@intecion.com>
License: Proprietary
Project-URL: Homepage, https://intecion.com
Project-URL: Documentation, https://github.com/IntecionSoftware/ArgonCentral/blob/main/sdk/README.md
Project-URL: Repository, https://github.com/IntecionSoftware/ArgonCentral
Project-URL: Changelog, https://github.com/IntecionSoftware/ArgonCentral/blob/main/sdk/CHANGELOG.md
Project-URL: API reference, https://spermit.intecion.net/api/v1
Keywords: spermit,licensing,delivery,billing,api-client,intecion
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
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.9
Description-Content-Type: text/markdown

# Spermit SDK for Python

A small, dependency-free client for the **Spermit** platform API:

```
https://spermit.intecion.net/api/v1
```

It is designed to be vendored by desktop, on-prem and air-gapped products
(itsTrack, ArgonCompany, …). Only the Python standard library is used:
`urllib.request`, `json`, `dataclasses` and `typing`.

## Install / vendor

From PyPI (recommended):

```bash
pip install spermit-sdk
```

From TestPyPI (pre-releases):

```bash
pip install \
  --index-url https://test.pypi.org/simple/ \
  --extra-index-url https://pypi.org/simple \
  spermit-sdk
```

From this repository:

```bash
pip install ./sdk
```

Or copy the `sdk/spermit_sdk` package into your application. There are **no
runtime dependencies** and it works on Python 3.9+.

## Quick start

```python
from spermit_sdk import SpermitClient

client = SpermitClient(
    "https://spermit.intecion.net/api/v1",
    token="<contractor-api-token>",  # only needed for billing/delivery
    timeout=30,
)
```

The token is sent as `Authorization: Bearer <token>` on every request.
Licensing activation/CRL endpoints do not require a token; billing and
download grants do.

Every call returns plain decoded JSON or a small dataclass. Failures raise
`SpermitError`:

```python
from spermit_sdk import SpermitError

try:
    invoices = client.billing.list_invoices(customer_account_id=42)
except SpermitError as exc:
    print(exc.status)   # 401, 403, 404, 500, …
    print(exc.detail)   # server-provided message
```

## Licensing

### Offline activation (air-gapped)

The product writes a signed request file on the offline machine, the customer
brings it to a connected machine, and the platform returns a signed license
envelope. Feed that envelope token back into the SDK:

```python
from spermit_sdk import SpermitClient

client = SpermitClient("https://spermit.intecion.net/api/v1")

result = client.licensing.offline_activate(
    license_id=license_id,          # from the signed activation request
    grant_id=grant_id,              # module or product grant
    hwid=device_fingerprint(),
    platform="windows",
    version="4.8.2",
)

if result.perpetual:
    ...  # runs forever; updates depend on updates_policy

if result.valid_until and is_expired(result.valid_until):
    ...  # subscription/trial expired

print(result.license_id, result.grant_id, result.state)
print(result.features, result.limits)
```

Store `result.license_envelope` next to `result.signature`. The SDK deliberately
does **not** verify signatures (no crypto dependency). Verify with your
application's existing crypto stack / `SPERMIT_PUBLIC_KEY`.

The SDK sends exactly the current Spermit contract:
`{license_id, grant_id, hwid, platform?, version?, instance_lookup_id?}`.
`license_id` and `grant_id` come from the signed activation request/machine
identity, not from the product name.

### CRL

```python
signed = client.licensing.get_crl("itstrack", signed=True)
# {"payload": {...}, "signature": "...", "crl_version": 7}

entries = client.licensing.get_crl("itstrack", signed=False, since_version=7)
# [{"grant_id": "gr_ocr", "reason": "revoked", ...}]
```

### Envelope structure check

```python
from spermit_sdk import SpermitValidationError

try:
    check = client.licensing.validate_envelope(license_json)
    print(check.grants[0]["grant_id"], check.crl_version)
except SpermitValidationError as exc:
    print("broken license file:", exc.detail)
```

`validate_envelope` checks the JSON shape (schema, at least one grant, unique
`grant_id`s) and returns a `LicenseValidation`; it does not verify the
cryptographic signature.

## Updates and downloads

```python
delivery = client.delivery

manifest = delivery.get_update_manifest(
    product_key="itstrack",
    current_version=APP_VERSION,
    module_key="core",
    license_id=license.license_id,
    grant_id=license.grant_id,
    os="windows",
    arch="x86_64",
)

if manifest.target_version and manifest.target_version != APP_VERSION:
    print("update available:", manifest.target_version)
    for artifact in manifest.artifacts:
        print(artifact.kind, artifact.os, artifact.arch, artifact.sha256)

# Resolve a short-lived signed URL for one artifact.
url = delivery.create_download(
    artifact_id=manifest.artifacts[0].id,
    customer_account_id=42,
)
```

`create_download` performs the server's two-step flow: it creates a download
grant (`POST /delivery/artifacts/{id}/grants`) and resolves the token
(`GET /delivery/downloads/{token}`), returning the final URL.

`license_id`/`grant_id` can also be set once on the delivery client:

```python
delivery = DeliveryClient(
    client,
    license_id="lic_...",
    grant_id="gr_core",
    customer_account_id=42,
)
```

### Listing releases

```python
releases = client.delivery.list_releases("itstrack", channel="stable")
```

### Offline bundle

For air-gapped installs, download one signed bundle containing the license
envelope, CRL, update manifest and artifact metadata:

```python
bundle = delivery.get_offline_bundle(
    product_key="itstrack",
    license_id=license.license_id,
    grant_id=license.grant_id,
    os="windows",
    arch="x86_64",
)

# Structural check only; verify license_signature with your crypto stack.
verify_offline_bundle_structure(bundle)
```

## Billing

```python
billing = client.billing

subscriptions = billing.list_subscriptions(customer_account_id=42)
for sub in subscriptions:
    print(sub.product_key, sub.status, sub.period_end)
    for line in sub.lines:
        print("  ", line.module_key, line.billing_model, line.price_snapshot)

invoices = billing.list_invoices(customer_account_id=42)
for invoice in invoices:
    print(invoice.number, invoice.amount, invoice.currency, invoice.pdf_url)

detail = billing.get_invoice(invoices[0].id)
for payment in detail.payments:
    print(payment["status"], payment["amount"])
```

Amounts are `decimal.Decimal` (never `float`), matching the platform's money
rules. Unknown fields from newer API versions are ignored, so upgrades are
backwards compatible.

## Testing without a network

`SpermitClient` accepts an injectable transport:

```python
def fake_transport(method, url, headers, body):
    return 200, {"Content-Type": "application/json"}, b'{"items": []}'

client = SpermitClient("https://spermit.example/api/v1", transport=fake_transport)
client.delivery.list_releases("itstrack")   # no socket opened
```

Run the bundled test suite:

```bash
python -m unittest discover -s sdk/tests -v
```

## API surface

| Python | HTTP |
|---|---|
| `client.get/post/delete` | any path under `/api/v1` |
| `licensing.offline_activate` | `POST /licensing/offline-activate` |
| `licensing.get_crl(signed=True)` | `GET /licensing/crl/{product}/signed` |
| `licensing.get_crl(signed=False)` | `GET /licensing/crl/{product}` |
| `licensing.validate_envelope` | local structural validation |
| `delivery.list_releases` | `GET /delivery/releases` |
| `delivery.get_update_manifest` | `GET /delivery/updates/latest` |
| `delivery.create_download` | `POST /delivery/artifacts/{id}/grants` → `GET /delivery/downloads/{token}` |
| `delivery.get_offline_bundle` | `GET /delivery/offline-bundle` |
| `billing.list_subscriptions` | `GET /billing/subscriptions` |
| `billing.list_invoices` | `GET /billing/invoices` |
| `billing.get_invoice` | `GET /billing/invoices/{id}` |

## Publishing

The package is published as **`spermit-sdk`** on PyPI:

- package: https://pypi.org/project/spermit-sdk/
- pre-releases: https://test.pypi.org/project/spermit-sdk/
- repository: https://github.com/IntecionSoftware/ArgonCentral

Releases use GitHub Actions Trusted Publishing (OIDC), so no PyPI token is
stored in the repository:

1. Bump `version` in `sdk/pyproject.toml` and update `sdk/CHANGELOG.md`.
2. Push a tag `sdk-v<version>` (for example `sdk-v0.1.0`).
3. `.github/workflows/publish-sdk.yml` builds sdist/wheel, runs `twine check`,
   publishes to PyPI and attaches artifacts to the GitHub Release.

Use the workflow dispatch with `testpypi` first for release candidates.
Private/enterprise mirrors can consume the same wheel from an internal index
(AWS CodeArtifact, Cloudsmith, Artifactory) — see `docs/spermit/SDK.md`.
