#!/usr/bin/env python3 """Example: Using .env configuration file with py-wikijs. This example shows how to use environment variables (.env file) to configure the WikiJS client. Prerequisites: pip install python-dotenv Setup: 1. Copy config.env.example to .env 2. Update .env with your Wiki.js credentials 3. Run this script Usage: python using_env_config.py """ import sys from pathlib import Path # Add examples directory to path sys.path.insert(0, str(Path(__file__).parent)) try: from dotenv import load_dotenv except ImportError: print("āŒ python-dotenv is required for .env files") print("Install with: pip install python-dotenv") sys.exit(1) from config_helper import create_client_from_config, load_config def main(): """Main function.""" print("=" * 60) print("Using .env Configuration with py-wikijs") print("=" * 60) # Method 1: Load .env explicitly print("\nšŸ“ Method 1: Load .env explicitly") env_file = Path(__file__).parent / ".env" if not env_file.exists(): print(f"āš ļø .env file not found at {env_file}") print(" Copy config.env.example to .env and update it") print("\nšŸ“ Method 2: Using config helper") # Load the .env file load_dotenv(env_file) # Method 2: Use config helper (recommended) try: config = load_config(".env") print(f"āœ… Loaded configuration from .env") print(f"\nConfiguration:") print(f" URL: {config.get('wikijs', {}).get('url')}") print(f" Auth method: {config.get('wikijs', {}).get('auth', {}).get('method')}") print(f" Timeout: {config.get('client', {}).get('timeout')}s") print(f" Rate limit: {config.get('client', {}).get('rate_limit')} req/s") # Create client print("\nšŸ”Œ Creating WikiJS client...") client = create_client_from_config(config) print(f"āœ… Client created successfully") print(f" Base URL: {client.base_url}") # Test connection print("\nšŸ” Testing connection...") pages = client.pages.list() print(f"āœ… Connected! Found {len(pages)} page(s)") if pages: print(f"\nšŸ“„ First page:") print(f" ID: {pages[0].id}") print(f" Title: {pages[0].title}") print(f" Path: {pages[0].path}") # Show metrics metrics = client.get_metrics() print(f"\nšŸ“Š Metrics:") print(f" Total requests: {metrics['total_requests']}") print(f" Successful: {metrics['successful_requests']}") print(f" Failed: {metrics['failed_requests']}") except FileNotFoundError: print("āŒ .env file not found") print(" 1. Copy config.env.example to .env") print(" 2. Update with your Wiki.js credentials") except Exception as e: print(f"āŒ Error: {e}") return 1 print("\n" + "=" * 60) print("āœ… Example completed successfully!") print("=" * 60) return 0 if __name__ == "__main__": sys.exit(main())