Utilities

Helper functions for validation, formatting, and data conversion.


πŸ’‘ Don't have a Stake.com account yet? Sign up here to get started and unlock all API features.

Overview

The stakeapi.utils module provides utility functions for common operations like validation, formatting, and type conversion.

Import:

from stakeapi.utils import (
    validate_api_key,
    safe_decimal,
    parse_datetime,
    format_currency,
    calculate_win_rate,
    validate_bet_amount,
    sanitize_game_name,
)

validate_api_key(api_key)

Validate that an API key matches the expected format.

def validate_api_key(api_key: str) -> bool
Parameter Type Description
api_key str The API key to validate

Returns: True if the key is a 32-64 character alphanumeric string.

validate_api_key("abc123def456")  # True
validate_api_key("")               # False
validate_api_key(None)             # False

safe_decimal(value)

Safely convert any value to a Decimal, returning None on failure.

def safe_decimal(value: Any) -> Optional[Decimal]
safe_decimal("123.45")     # Decimal('123.45')
safe_decimal(100)          # Decimal('100')
safe_decimal("invalid")   # None
safe_decimal(None)         # None

parse_datetime(date_string)

Parse an ISO format datetime string to a datetime object.

def parse_datetime(date_string: str) -> Optional[datetime]

Handles:

  • ISO 8601 with timezone: 2024-01-01T12:00:00+00:00
  • ISO 8601 with Z: 2024-01-01T12:00:00Z
  • ISO 8601 without timezone: 2024-01-01T12:00:00
parse_datetime("2024-01-01T12:00:00Z")
# datetime(2024, 1, 1, 12, 0, tzinfo=timezone.utc)

parse_datetime("invalid")  # None
parse_datetime("")          # None

format_currency(amount, currency)

Format a decimal amount with the appropriate currency symbol.

def format_currency(amount: Decimal, currency: str = "USD") -> str
Currency Example Output
USD $100.50
EUR €100.50
GBP Β£100.50
Other 100.50 BTC
from decimal import Decimal

format_currency(Decimal("100.50"), "USD")  # "$100.50"
format_currency(Decimal("0.001"), "BTC")   # "0.00 BTC"

calculate_win_rate(wins, total_bets)

Calculate the win rate as a percentage.

def calculate_win_rate(wins: int, total_bets: int) -> float
calculate_win_rate(25, 100)  # 25.0
calculate_win_rate(0, 0)     # 0.0
calculate_win_rate(3, 10)    # 30.0

validate_bet_amount(amount, min_bet, max_bet)

Check that a bet amount falls within the allowed range.

def validate_bet_amount(
    amount: Decimal, 
    min_bet: Decimal, 
    max_bet: Decimal
) -> bool
from decimal import Decimal

validate_bet_amount(
    Decimal("0.5"),
    min_bet=Decimal("0.01"),
    max_bet=Decimal("1000")
)  # True

validate_bet_amount(
    Decimal("0.001"),
    min_bet=Decimal("0.01"),
    max_bet=Decimal("1000")
)  # False β€” below minimum

sanitize_game_name(name)

Remove special characters and normalize whitespace in game names.

def sanitize_game_name(name: str) -> str
sanitize_game_name("Sweet Bonanzaβ„’ 1000")  # "Sweet Bonanza 1000"
sanitize_game_name("  Multiple   Spaces  ")  # "Multiple Spaces"
sanitize_game_name("")                        # ""

🎰 Ready to experience Stake.com?

Create your account and start using StakeAPI with real data today.

Sign Up on Stake.com β†’

πŸ’¬ Join the StakeAPI Community on Discord

Get help, share your projects, discuss strategies, and stay up to date with the latest StakeAPI news.

Join Our Discord Server β†’

These utilities make working with Stake.com data clean and safe. Sign up and start building!