Metadata-Version: 2.4
Name: esrating
Version: 0.1.0
Summary: ESRating Python SDK — typed client for the Distribution Hub API + webhook signature verification.
Author-email: ESRating <support@esrating.com>
License: Apache-2.0
Project-URL: Homepage, https://esrating.com/docs#sdk-python
Project-URL: Documentation, https://esrating.com/docs
Project-URL: Repository, https://github.com/ESRating-LLC/ESRating
Keywords: esrating,insurance,embed,webhook,sdk
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
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: Topic :: Software Development :: Libraries
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: requests>=2.31

# esrating

Python SDK for the ESRating Distribution Hub API. Mints embed sessions, confirms external payments, verifies webhook signatures.

## Install

```bash
pip install esrating
```

Requires Python 3.9+.

## Quick start

```python
import os
from esrating import ESRating

esrating = ESRating(api_key=os.environ["ESRATING_SK_LIVE"])

session = esrating.embed.create_session(
    user_id="partner-user-123",
    email="agent@partner.com",
    metadata={"partnerOrderId": "abc"},
)
print(session["session_url"])
```

## Webhook verification (Flask example)

```python
from flask import Flask, request, jsonify
from esrating import ESRating, WebhookSignatureError

app = Flask(__name__)
esrating = ESRating()

@app.post("/webhooks/esrating")
def esrating_webhook():
    try:
        event = esrating.webhooks.construct_event(
            raw_body=request.get_data(as_text=True),  # CRITICAL: raw bytes, not parsed
            signature=request.headers.get("X-ESRating-Signature"),
            secret=os.environ["ESRATING_WEBHOOK_SECRET"],
        )
    except WebhookSignatureError as exc:
        return jsonify(error=str(exc)), 400

    if event["event"] == "quote.bound":
        on_quote_bound(event["data"])
    elif event["event"] == "policy.issued":
        on_policy_issued(event["data"])

    return jsonify(received=True)
```

## Confirm an external payment

```python
esrating.embed.confirm_external_payment(
    quote_id="quote_abc123",
    external_payment_id="pi_partner_xyz",
    amount_cents=250_000,
    method="card",
)
```

Idempotent — retrying with the same `external_payment_id` is safe.

## Errors

```python
from esrating import ESRating, AuthError, ForbiddenError, RateLimitError
import time

try:
    esrating.embed.create_session(user_id="...", email="...")
except AuthError:
    # sk_ key bad
    raise
except ForbiddenError as exc:
    # Distributor paused, KYC missing, etc.
    print(exc)
except RateLimitError as exc:
    if exc.retry_after_seconds:
        time.sleep(exc.retry_after_seconds)
        # retry
```

## License

Apache-2.0.
