Skip to content

Enhancement: Add configuration module for constants #40

@knowlen

Description

@knowlen

Description

As identified in PR #32 review, magic numbers and constants like TOKEN_EXPIRY_BUFFER = 60 could be made configurable to allow users to adjust behavior for their specific needs.

Current Constants to Make Configurable

OAuth2 Module

  • TOKEN_EXPIRY_BUFFER = 60 - seconds before token expiry to refresh
  • STATE_LENGTH = 32 - length of CSRF state parameter
  • DEFAULT_TIMEOUT = 30 - HTTP request timeout
  • CALLBACK_PORT = 8765 - default OAuth2 callback port

Client Module

  • DEFAULT_RETRY_COUNT = 3 - number of retries for failed requests
  • DEFAULT_RETRY_DELAY = 1.0 - delay between retries
  • CONNECTION_POOL_SIZE = 10 - HTTP connection pool size

Proposed Configuration System

1. Environment Variables

import os

TOKEN_EXPIRY_BUFFER = int(os.getenv('ESOLOGS_TOKEN_EXPIRY_BUFFER', '60'))
DEFAULT_TIMEOUT = float(os.getenv('ESOLOGS_DEFAULT_TIMEOUT', '30'))

2. Configuration Class

from dataclasses import dataclass
from typing import Optional

@dataclass
class ESOLogsConfig:
    token_expiry_buffer: int = 60
    state_length: int = 32
    default_timeout: float = 30.0
    callback_port: int = 8765
    retry_count: int = 3
    retry_delay: float = 1.0
    connection_pool_size: int = 10
    
    @classmethod
    def from_env(cls) -> 'ESOLogsConfig':
        '''Load configuration from environment variables'''
        # Implementation
    
    @classmethod
    def from_file(cls, path: str) -> 'ESOLogsConfig':
        '''Load configuration from JSON/YAML file'''
        # Implementation

# Global config instance
config = ESOLogsConfig.from_env()

3. Usage Pattern

from esologs.config import config

# Use in code
if token.expires_in <= config.token_expiry_buffer:
    # Refresh token

Benefits

  • Users can tune behavior for their environment
  • Easier testing with different configurations
  • Better documentation of tunable parameters
  • Follows 12-factor app principles

Implementation Notes

  • Maintain backward compatibility
  • Document all configuration options
  • Validate configuration values
  • Consider using pydantic for validation

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions