#!/usr/bin/env python3 """Example: Using JSON configuration file with py-wikijs. This example shows how to use a JSON configuration file to configure the WikiJS client. Setup: 1. Copy config.json.example to config.json 2. Update config.json with your Wiki.js credentials 3. Run this script Usage: python using_json_config.py """ import json import sys from pathlib import Path # Add examples directory to path sys.path.insert(0, str(Path(__file__).parent)) from config_helper import create_client_from_config, load_config def main(): """Main function.""" print("=" * 60) print("Using JSON Configuration with py-wikijs") print("=" * 60) json_file = Path(__file__).parent / "config.json" if not json_file.exists(): print(f"\nāš ļø config.json not found at {json_file}") print(" Copy config.json.example to config.json and update it") return 1 try: # Load configuration print("\nšŸ“ Loading configuration from config.json...") config = load_config(json_file) print("āœ… Configuration loaded successfully") # Pretty print config (without sensitive data) safe_config = config.copy() if "wikijs" in safe_config and "auth" in safe_config["wikijs"]: if "api_key" in safe_config["wikijs"]["auth"]: safe_config["wikijs"]["auth"]["api_key"] = "***REDACTED***" print(f"\nConfiguration (sanitized):") print(json.dumps(safe_config, indent=2)) # Create client print("\nšŸ”Œ Creating WikiJS client...") client = create_client_from_config(config) print(f"āœ… Client created successfully") # Test connection print("\nšŸ” Testing connection...") pages = client.pages.list() print(f"āœ… Connected! Found {len(pages)} page(s)") # Create a new page (example) if input("\nā“ Create a test page? (y/N): ").lower() == "y": from wikijs.models import PageCreate test_page = PageCreate( title="Test Page from JSON Config", path="test/json-config-example", content="# Test Page\n\nCreated using JSON configuration!", description="Example page created from JSON config", is_published=False, locale="en", tags=["test", "json", "config"], ) print("\nšŸ“ Creating test page...") created = client.pages.create(test_page) print(f"āœ… Page created: {created.title} (ID: {created.id})") print(f" Path: {created.path}") # Show metrics metrics = client.get_metrics() print(f"\nšŸ“Š Client Metrics:") print(f" Total requests: {metrics['total_requests']}") print(f" Successful: {metrics['successful_requests']}") print(f" Failed: {metrics['failed_requests']}") except FileNotFoundError as e: print(f"āŒ {e}") return 1 except Exception as e: print(f"āŒ Error: {e}") import traceback traceback.print_exc() return 1 print("\n" + "=" * 60) print("āœ… Example completed successfully!") print("=" * 60) return 0 if __name__ == "__main__": sys.exit(main())