Overview
The MentionConfig class provides a type-safe way to manage configuration for the Mention API client. It supports loading configuration from environment variables and creating clients with consistent settings.
Class Definition
@dataclass(frozen=True, slots=True)
class MentionConfig:
access_token: str
account_id: str | None = None
base_url: str = "https://api.mention.net/api"
timeout: float = 30.0
max_retries: int = 3
retry_delay: float = 1.0
extra: dict[str, Any] = field(default_factory=dict)
Attributes
OAuth2 access token for authentication
Default account ID to use (optional)
base_url
str
default:"https://api.mention.net/api"
Base URL for the Mention API
Request timeout in seconds
Maximum number of retry attempts for failed requests
Base delay between retries in seconds (uses exponential backoff)
Additional configuration data for custom use cases
Class Methods
from_env
@classmethod
def from_env(
cls,
env_file: str | Path | None = None,
*,
prefix: str = "MENTION_",
) -> MentionConfig
Create configuration from environment variables.
Parameters:
env_file (str | Path | None): Optional path to a .env file to load
prefix (str): Environment variable prefix (default: “MENTION_”)
Returns: MentionConfig instance with values loaded from environment
Raises: ValueError if required ACCESS_TOKEN is not found
Environment Variables
The method looks for environment variables with the specified prefix, falling back to unprefixed versions:
MENTION_ACCESS_TOKEN or ACCESS_TOKEN (required)
MENTION_ACCOUNT_ID or ACCOUNT_ID (optional)
MENTION_BASE_URL or BASE_URL (optional, defaults to https://api.mention.net/api)
MENTION_TIMEOUT or TIMEOUT (optional, defaults to 30.0)
MENTION_MAX_RETRIES or MAX_RETRIES (optional, defaults to 3)
MENTION_RETRY_DELAY or RETRY_DELAY (optional, defaults to 1.0)
Instance Methods
with_account
def with_account(self, account_id: str) -> MentionConfig
Return a new config with a different account ID.
Parameters:
account_id (str): The new account ID to use
Returns: New MentionConfig instance with updated account ID
Note: This method returns a new instance because MentionConfig is frozen (immutable).
Usage Examples
Create from Environment Variables
from mention import MentionConfig, MentionClient
# Load from environment variables with default prefix
config = MentionConfig.from_env()
client = MentionClient.from_config(config)
Create from .env File
from mention import MentionConfig, MentionClient
# Load from specific .env file
config = MentionConfig.from_env(env_file=".env.production")
client = MentionClient.from_config(config)
Create with Custom Prefix
from mention import MentionConfig
# Use custom environment variable prefix
config = MentionConfig.from_env(prefix="MY_APP_")
# Looks for: MY_APP_ACCESS_TOKEN, MY_APP_ACCOUNT_ID, etc.
Create Manually
from mention import MentionConfig, MentionClient
config = MentionConfig(
access_token="your-token",
account_id="account-123",
timeout=60.0,
max_retries=5,
)
client = MentionClient.from_config(config)
Switch Account IDs
from mention import MentionConfig, MentionClient
base_config = MentionConfig.from_env()
# Create config for different accounts
account1_config = base_config.with_account("account-1")
account2_config = base_config.with_account("account-2")
client1 = MentionClient.from_config(account1_config)
client2 = MentionClient.from_config(account2_config)
Example .env File
# .env
MENTION_ACCESS_TOKEN=your-oauth2-token-here
MENTION_ACCOUNT_ID=12345
MENTION_BASE_URL=https://api.mention.net/api
MENTION_TIMEOUT=45.0
MENTION_MAX_RETRIES=5
MENTION_RETRY_DELAY=2.0
Immutability
The MentionConfig class is frozen (immutable), meaning its attributes cannot be modified after creation. To change values, create a new instance:
config = MentionConfig.from_env()
# This will raise an error
# config.timeout = 60.0
# Instead, create a new instance
new_config = MentionConfig(
access_token=config.access_token,
account_id=config.account_id,
base_url=config.base_url,
timeout=60.0, # Changed value
max_retries=config.max_retries,
retry_delay=config.retry_delay,
)
Or use the with_account() method for account ID changes:
config = MentionConfig.from_env()
new_config = config.with_account("different-account")