Metadata-Version: 2.4
Name: inlineshield
Version: 1.0.1
Summary: Official Python client for the InlineShield sign-up screening API: verify, siteverify, feedback, review status and webhook signature checks.
License-Expression: MIT
License-File: LICENSE
Project-URL: Documentation, https://inlineshield.io/docs/sdks/python/
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# inlineshield

The official Python client for the [InlineShield](https://inlineshield.io) sign-up screening API.

- Python 3.9 or later; standard library only (`urllib`, `hmac`, `json`); typed.
- Fail-open by default: an outage never blocks your sign-ups.

Docs: https://inlineshield.io/docs/sdks/python/

## Install

Until the package is on PyPI:

```sh
pip install https://inlineshield.io/downloads/sdk/inlineshield-1.0.1-py3-none-any.whl
```

## Use

```python
import os
from inlineshield import Client, verify_webhook

shield = Client(os.environ["INLINESHIELD_API_KEY"])  # bs_live_... or bs_test_...

# With the browser script: redeem its token, fall back to a server-side screening.
# Check `unavailable` first (fail-open: action "allow"), then `success`, then the error.
result = shield.siteverify(token, email)
if not result["unavailable"] and not result["success"]:
    if result["error"] != "NO_TOKEN":
        return reject(result["error"])  # INVALID_TOKEN, EXPIRED_TOKEN, ...
    result = shield.verify(email, ip=visitor_ip, user_agent=visitor_user_agent)
# result["action"]: "allow", "hold" or "block".

shield.feedback(request_id, "fraud", category="bonus_abuse")
review = shield.get_review(review_id)  # pending, approved or rejected

event = verify_webhook(signature_header, raw_body, os.environ["INLINESHIELD_WEBHOOK_SECRET"])
```

Results are dicts with the API's field names, plus `unavailable`.

## siteverify results

Check `unavailable` first: it is true when InlineShield could not answer (fail-open: `action` is "allow" and `success` is false, because no token was redeemed). Otherwise `success` true carries the decision; on `error` NO_TOKEN screen with verify; on any other error reject the sign-up.

## Options

`Client(api_key=None, *, base_url=None, timeout_ms=3000, fail_open=True, max_retries=1)`

- `api_key`: without one, `INLINESHIELD_API_KEY` from the environment.
- `base_url`: default `INLINESHIELD_BASE_URL`, else `https://api.inlineshield.io`.
- `fail_open=True`: when InlineShield cannot be reached, times out, answers 5xx, 429 or is over quota, the methods
  return `{"unavailable": True, "error": ..., "action": "allow", ...}` instead of raising. `False`: they raise.
- `timeout_ms` is a deadline for the whole request (the answer is read in a worker thread that is cut off at the
  deadline); `base_url` must use https (plain http only for localhost).
- `max_retries`: only a failed connection (nothing sent) is retried; never a timeout or an answer.

A rate limit (429) or an exhausted quota also fails open by default: every further sign-up is admitted unscreened until the limit resets. To choose otherwise, check `error` on an unavailable result (`RATE_LIMIT_EXCEEDED`, `TEST_LIMIT`, `QUOTA_EXCEEDED`) and hold or reject those sign-ups, or turn fail-open off and handle the errors.

## Errors

All extend `InlineShieldError` (`code`, `status`):

| Class | When |
|---|---|
| `AuthError` | 401, or 403 TENANT_SUSPENDED, or no key (MISSING_API_KEY). Always raised. |
| `ApiError` | 400, 404, 422, or a redirect (UNEXPECTED_REDIRECT). Always raised. |
| `UnavailableError` | TIMEOUT or UNAVAILABLE, with `fail_open=False`. |
| `RateLimitError` | RATE_LIMIT_EXCEEDED or TEST_LIMIT (`retry_after`), with `fail_open=False`. |
| `QuotaError` | QUOTA_EXCEEDED, with `fail_open=False`. |
| `WebhookVerificationError` | NO_SECRET, MALFORMED_HEADER, TIMESTAMP_OUT_OF_RANGE, SIGNATURE_MISMATCH, INVALID_PAYLOAD. |

Messages never contain the API key or an email address, and the SDK writes nothing to any output.

## Webhooks

`verify_webhook(signature_header, raw_body, secret, tolerance_sec=300)` checks `X-InlineShield-Signature`
(`t=<seconds>,v1=<hex>`): HMAC-SHA256 of `<t>.<raw body>` with the whole `whsec_...` secret, compared in constant
time, and `t` within `tolerance_sec` of now. Pass the raw body bytes (Flask: `request.get_data()`).

## Licence

MIT. See `LICENSE`.
