Skip to main content

Python client for the Lightrate API

Project description

LightRate Client Python

A Python client for the Lightrate token management API, providing easy-to-use methods for consuming tokens with local bucket management.

Installation

pip install lightrate-client

Usage

Configuration

Configure the client with your API credentials:

from lightrate_client import configure, get_client

configure(
    api_key='your_api_key',
    application_id='your_application_id',  # required
    timeout=30,  # optional, defaults to 30 seconds
    retry_attempts=3,  # optional, defaults to 3
    default_local_bucket_size=5  # optional, defaults to 5
)

Basic Usage

from lightrate_client import LightRateClient, create_client

# Simple usage - pass your API key and application ID
client = LightRateClient('your_api_key', 'your_application_id')

# Or use the convenience method
client = create_client('your_api_key', 'your_application_id')

# With additional options
client = LightRateClient('your_api_key', 'your_application_id', ClientOptions(
    timeout=60,
    default_local_bucket_size=10
))

# Or configure globally and use the default client
configure(api_key='your_api_key', application_id='your_application_id')
client = get_client()

Consuming Tokens

# Consume tokens by operation
response = client.consume_tokens(
    user_identifier='user123',
    tokens_requested=1,
    operation='send_email'
)

# Or consume tokens by path
response = client.consume_tokens(
    user_identifier='user123',
    tokens_requested=1,
    path='/api/v1/emails/send',
    http_method='POST'
)

if response.tokens_consumed > 0:
    print(f"Tokens consumed successfully. Remaining: {response.tokens_remaining}")
else:
    print(f"Failed to consume tokens")

Using Local Token Buckets

The client supports local token buckets for improved performance. Buckets are automatically created based on the rules returned by the API, and are matched against incoming requests using the matcher field from the rule.

# Configure client with default bucket size
client = LightRateClient('your_api_key', 'your_application_id', ClientOptions(
    default_local_bucket_size=20  # All operations use this bucket size
))

# Consume tokens using local bucket (more efficient)
result = client.consume_local_bucket_token(
    user_identifier='user123',
    operation='send_email'
)

print(f"Success: {result.success}")
print(f"Used local token: {result.used_local_token}")
print(f"Bucket status: {result.bucket_status}")

Bucket Matching:

  • Buckets are matched using the matcher field from the rule, which supports regex patterns
  • Each user has separate buckets per rule, ensuring proper isolation
  • Buckets expire after 60 seconds of inactivity
  • Default rules (isDefault: true) do not create local buckets

Complete Example

from lightrate_client import LightRateClient

# Create a client with your API key and application ID
client = LightRateClient('your_api_key', 'your_application_id')

try:
    # Consume tokens
    consume_response = client.consume_tokens(
        user_identifier='user123',
        tokens_requested=1,
        operation='send_email'
    )

    if consume_response.tokens_consumed > 0:
        print(f"Successfully consumed tokens. Remaining: {consume_response.tokens_remaining}")
        # Proceed with your operation
    else:
        print("Failed to consume tokens")
        # Handle rate limiting

except UnauthorizedError as e:
    print(f"Authentication failed: {e}")
except TooManyRequestsError as e:
    print(f"Rate limited: {e}")
except APIError as e:
    print(f"API Error ({e.status_code}): {e}")
except NetworkError as e:
    print(f"Network error: {e}")

Error Handling

The client provides comprehensive error handling with specific exception types:

from lightrate_client import (
    UnauthorizedError,
    NotFoundError,
    APIError,
    NetworkError,
    TimeoutError
)

try:
    response = client.consume_tokens(...)
except UnauthorizedError as e:
    print(f"Authentication failed: {e}")
except NotFoundError as e:
    print(f"Resource not found: {e}")
except APIError as e:
    print(f"API Error ({e.status_code}): {e}")
except NetworkError as e:
    print(f"Network error: {e}")
except TimeoutError as e:
    print(f"Request timed out: {e}")

Available error types:

  • LightRateError - Base error class
  • ConfigurationError - Configuration-related errors
  • AuthenticationError - Authentication-related errors
  • APIError - Base API error class
  • BadRequestError - 400 errors
  • UnauthorizedError - 401 errors
  • ForbiddenError - 403 errors
  • NotFoundError - 404 errors
  • UnprocessableEntityError - 422 errors
  • TooManyRequestsError - 429 errors
  • InternalServerError - 500 errors
  • ServiceUnavailableError - 503 errors
  • NetworkError - Network-related errors
  • TimeoutError - Request timeout errors

API Reference

Classes

LightRateClient

Main client class for interacting with the LightRate API.

Constructor:

LightRateClient(api_key: str, application_id: str, options: Optional[ClientOptions] = None)

Methods:

  • consume_tokens(user_identifier, tokens_requested, operation=None, path=None, http_method=None) -> ConsumeTokensResponse
  • consume_local_bucket_token(user_identifier, operation=None, path=None, http_method=None) -> ConsumeLocalBucketTokenResponse
  • consume_tokens_with_request(request) -> ConsumeTokensResponse
  • get_all_bucket_statuses() -> Dict[str, Any]
  • reset_all_buckets() -> None
  • get_configuration() -> Configuration

Configuration

Configuration class for client settings.

Methods:

  • is_valid() -> bool
  • to_dict() -> Dict[str, Any]
  • update(**options) -> None

Global Functions

  • configure(**options) -> None - Configure global client
  • get_client() -> LightRateClient - Get global client instance
  • create_client(api_key, application_id, **options) -> LightRateClient - Create new client
  • reset() -> None - Reset global configuration

Development

After checking out the repo, run pip install -e .[dev] to install dependencies. Then, run pytest to run the tests.

To build the project, run python setup.py sdist bdist_wheel.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/lightbourne-technologies/lightrate-client-python. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The package is available as open source under the terms of the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lightrate_client-1.0.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lightrate_client-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file lightrate_client-1.0.0.tar.gz.

File metadata

  • Download URL: lightrate_client-1.0.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for lightrate_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 770d0b30caaf3dc7508db6df4465c1128c54e86760d05252c2706a0d0066acb0
MD5 691fcb78a496ee4ab1ebdae9e0dcf286
BLAKE2b-256 5f23b3d45cdcb7cdaa23174bc797966c1bfd7df29cfe89e83da7361008759025

See more details on using hashes here.

File details

Details for the file lightrate_client-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lightrate_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22bff6675e04346a93695d12c3a8ac0d015121ff49be84e71430430743e89d55
MD5 0c0f9096099569fe9438b623e97fbd0c
BLAKE2b-256 7de64599b568db6b814c1f1093b5c075d6cf59a1a89541d03a947dd8b1f26420

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page