ironlicensing-python/src/ironlicensing/__init__.py

203 lines
5.1 KiB
Python

"""IronLicensing SDK for Python.
Software licensing and activation SDK.
Example:
>>> import ironlicensing
>>>
>>> # Initialize
>>> ironlicensing.init("pk_live_xxxxx", "my-app")
>>>
>>> # Validate a license
>>> result = ironlicensing.validate("IRON-XXXX-XXXX-XXXX-XXXX")
>>> if result.valid:
... print("License is valid!")
>>>
>>> # Check features
>>> if ironlicensing.has_feature("premium"):
... print("Premium features enabled")
"""
from .types import (
LicenseStatus,
LicenseType,
LicenseOptions,
Feature,
License,
Activation,
LicenseResult,
CheckoutResult,
ProductTier,
)
from .client import LicenseClient, LicenseRequiredError
__all__ = [
# Types
"LicenseStatus",
"LicenseType",
"LicenseOptions",
"Feature",
"License",
"Activation",
"LicenseResult",
"CheckoutResult",
"ProductTier",
# Client
"LicenseClient",
"LicenseRequiredError",
# Global functions
"init",
"validate",
"validate_async",
"activate",
"activate_async",
"deactivate",
"deactivate_async",
"start_trial",
"start_trial_async",
"has_feature",
"require_feature",
"get_feature",
"get_features",
"get_license",
"get_status",
"is_licensed",
"is_trial",
"get_tiers",
"get_tiers_async",
"start_purchase",
"start_purchase_async",
]
_global_client: LicenseClient | None = None
def init(
public_key: str,
product_slug: str,
api_base_url: str = "https://api.ironlicensing.com",
debug: bool = False,
enable_offline_cache: bool = True,
cache_validation_minutes: int = 60,
offline_grace_days: int = 7,
) -> LicenseClient:
"""Initialize the global license client."""
global _global_client
options = LicenseOptions(
public_key=public_key,
product_slug=product_slug,
api_base_url=api_base_url,
debug=debug,
enable_offline_cache=enable_offline_cache,
cache_validation_minutes=cache_validation_minutes,
offline_grace_days=offline_grace_days,
)
_global_client = LicenseClient(options)
return _global_client
def _get_client() -> LicenseClient:
if _global_client is None:
raise RuntimeError("IronLicensing not initialized. Call init() first.")
return _global_client
def validate(license_key: str | None = None) -> LicenseResult:
"""Validate a license key (sync)."""
return _get_client().validate(license_key)
async def validate_async(license_key: str | None = None) -> LicenseResult:
"""Validate a license key (async)."""
return await _get_client().validate_async(license_key)
def activate(license_key: str, machine_name: str | None = None) -> LicenseResult:
"""Activate a license (sync)."""
return _get_client().activate(license_key, machine_name)
async def activate_async(license_key: str, machine_name: str | None = None) -> LicenseResult:
"""Activate a license (async)."""
return await _get_client().activate_async(license_key, machine_name)
def deactivate() -> bool:
"""Deactivate the current license (sync)."""
return _get_client().deactivate()
async def deactivate_async() -> bool:
"""Deactivate the current license (async)."""
return await _get_client().deactivate_async()
def start_trial(email: str) -> LicenseResult:
"""Start a trial (sync)."""
return _get_client().start_trial(email)
async def start_trial_async(email: str) -> LicenseResult:
"""Start a trial (async)."""
return await _get_client().start_trial_async(email)
def has_feature(feature_key: str) -> bool:
"""Check if a feature is enabled."""
return _get_client().has_feature(feature_key)
def require_feature(feature_key: str) -> None:
"""Require a feature, raises LicenseRequiredError if not available."""
_get_client().require_feature(feature_key)
def get_feature(feature_key: str) -> Feature | None:
"""Get a feature by key."""
return _get_client().get_feature(feature_key)
def get_features() -> list[Feature]:
"""Get all features."""
return _get_client().get_features()
def get_license() -> License | None:
"""Get the current license."""
return _get_client().license
def get_status() -> LicenseStatus:
"""Get the current license status."""
return _get_client().status
def is_licensed() -> bool:
"""Check if the license is valid."""
return _get_client().is_licensed
def is_trial() -> bool:
"""Check if the license is a trial."""
return _get_client().is_trial
def get_tiers() -> list[ProductTier]:
"""Get product tiers (sync)."""
return _get_client().get_tiers()
async def get_tiers_async() -> list[ProductTier]:
"""Get product tiers (async)."""
return await _get_client().get_tiers_async()
def start_purchase(tier_id: str, email: str) -> CheckoutResult:
"""Start a purchase checkout (sync)."""
return _get_client().start_purchase(tier_id, email)
async def start_purchase_async(tier_id: str, email: str) -> CheckoutResult:
"""Start a purchase checkout (async)."""
return await _get_client().start_purchase_async(tier_id, email)