|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Demo script for zone deletion functionality. |
| 4 | +
|
| 5 | +This script demonstrates: |
| 6 | +1. Listing all zones |
| 7 | +2. Creating a test zone |
| 8 | +3. Verifying it exists |
| 9 | +4. Deleting the test zone |
| 10 | +5. Verifying it's gone |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import asyncio |
| 16 | +import time |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +# Add parent directory to path |
| 20 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src")) |
| 21 | + |
| 22 | +from brightdata import BrightDataClient |
| 23 | +from brightdata.exceptions import ZoneError, AuthenticationError |
| 24 | + |
| 25 | + |
| 26 | +async def demo_delete_zone(): |
| 27 | + """Demonstrate zone deletion functionality.""" |
| 28 | + |
| 29 | + print("\n" + "="*60) |
| 30 | + print("ZONE DELETION DEMO") |
| 31 | + print("="*60) |
| 32 | + |
| 33 | + # Check for API token |
| 34 | + if not os.environ.get("BRIGHTDATA_API_TOKEN"): |
| 35 | + print("\n❌ ERROR: No API token found") |
| 36 | + print("Please set BRIGHTDATA_API_TOKEN environment variable") |
| 37 | + return False |
| 38 | + |
| 39 | + # Create client |
| 40 | + client = BrightDataClient(validate_token=False) |
| 41 | + |
| 42 | + # Create a unique test zone name |
| 43 | + timestamp = str(int(time.time()))[-6:] |
| 44 | + test_zone_name = f"test_delete_zone_{timestamp}" |
| 45 | + |
| 46 | + try: |
| 47 | + async with client: |
| 48 | + # Step 1: List initial zones |
| 49 | + print("\n📊 Step 1: Listing current zones...") |
| 50 | + initial_zones = await client.list_zones() |
| 51 | + initial_zone_names = {z.get('name') for z in initial_zones} |
| 52 | + print(f"✅ Found {len(initial_zones)} zones") |
| 53 | + |
| 54 | + # Step 2: Create a test zone |
| 55 | + print(f"\n🔧 Step 2: Creating test zone '{test_zone_name}'...") |
| 56 | + test_client = BrightDataClient( |
| 57 | + auto_create_zones=True, |
| 58 | + web_unlocker_zone=test_zone_name, |
| 59 | + validate_token=False |
| 60 | + ) |
| 61 | + |
| 62 | + try: |
| 63 | + async with test_client: |
| 64 | + # Trigger zone creation |
| 65 | + try: |
| 66 | + await test_client.scrape_url_async( |
| 67 | + url="https://example.com", |
| 68 | + zone=test_zone_name |
| 69 | + ) |
| 70 | + except Exception as e: |
| 71 | + # Zone might be created even if scrape fails |
| 72 | + print(f" ℹ️ Scrape error (expected): {e}") |
| 73 | + |
| 74 | + print(f"✅ Test zone '{test_zone_name}' created") |
| 75 | + except Exception as e: |
| 76 | + print(f"❌ Failed to create test zone: {e}") |
| 77 | + return False |
| 78 | + |
| 79 | + # Wait a bit for zone to be fully registered |
| 80 | + await asyncio.sleep(2) |
| 81 | + |
| 82 | + # Step 3: Verify zone exists |
| 83 | + print(f"\n🔍 Step 3: Verifying zone '{test_zone_name}' exists...") |
| 84 | + zones_after_create = await client.list_zones() |
| 85 | + zone_names_after_create = {z.get('name') for z in zones_after_create} |
| 86 | + |
| 87 | + if test_zone_name in zone_names_after_create: |
| 88 | + print(f"✅ Zone '{test_zone_name}' found in zone list") |
| 89 | + # Print zone details |
| 90 | + test_zone = next(z for z in zones_after_create if z.get('name') == test_zone_name) |
| 91 | + print(f" Type: {test_zone.get('type', 'unknown')}") |
| 92 | + print(f" Status: {test_zone.get('status', 'unknown')}") |
| 93 | + else: |
| 94 | + print(f"⚠️ Zone '{test_zone_name}' not found (might still be creating)") |
| 95 | + |
| 96 | + # Step 4: Delete the test zone |
| 97 | + print(f"\n🗑️ Step 4: Deleting zone '{test_zone_name}'...") |
| 98 | + try: |
| 99 | + await client.delete_zone(test_zone_name) |
| 100 | + print(f"✅ Zone '{test_zone_name}' deleted successfully") |
| 101 | + except ZoneError as e: |
| 102 | + print(f"❌ Failed to delete zone: {e}") |
| 103 | + return False |
| 104 | + except AuthenticationError as e: |
| 105 | + print(f"❌ Authentication error: {e}") |
| 106 | + return False |
| 107 | + |
| 108 | + # Wait a bit for deletion to propagate |
| 109 | + await asyncio.sleep(2) |
| 110 | + |
| 111 | + # Step 5: Verify zone is gone |
| 112 | + print(f"\n🔍 Step 5: Verifying zone '{test_zone_name}' is deleted...") |
| 113 | + final_zones = await client.list_zones() |
| 114 | + final_zone_names = {z.get('name') for z in final_zones} |
| 115 | + |
| 116 | + if test_zone_name not in final_zone_names: |
| 117 | + print(f"✅ Confirmed: Zone '{test_zone_name}' no longer exists") |
| 118 | + else: |
| 119 | + print(f"⚠️ Zone '{test_zone_name}' still appears in list (deletion might be delayed)") |
| 120 | + |
| 121 | + # Summary |
| 122 | + print("\n" + "="*60) |
| 123 | + print("📈 SUMMARY:") |
| 124 | + print(f" Initial zones: {len(initial_zones)}") |
| 125 | + print(f" After creation: {len(zones_after_create)}") |
| 126 | + print(f" After deletion: {len(final_zones)}") |
| 127 | + print(f" Net change: {len(final_zones) - len(initial_zones)}") |
| 128 | + |
| 129 | + print("\n" + "="*60) |
| 130 | + print("✅ DEMO COMPLETED SUCCESSFULLY") |
| 131 | + print("="*60) |
| 132 | + |
| 133 | + return True |
| 134 | + |
| 135 | + except Exception as e: |
| 136 | + print(f"\n❌ Unexpected error: {e}") |
| 137 | + import traceback |
| 138 | + traceback.print_exc() |
| 139 | + return False |
| 140 | + |
| 141 | + |
| 142 | +def main(): |
| 143 | + """Main entry point.""" |
| 144 | + try: |
| 145 | + success = asyncio.run(demo_delete_zone()) |
| 146 | + sys.exit(0 if success else 1) |
| 147 | + except KeyboardInterrupt: |
| 148 | + print("\n\n⚠️ Demo interrupted by user") |
| 149 | + sys.exit(2) |
| 150 | + except Exception as e: |
| 151 | + print(f"\n❌ Fatal error: {e}") |
| 152 | + sys.exit(3) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + main() |
| 157 | + |
0 commit comments