|
| 1 | +"""Demo of the simplified API accessor. |
| 2 | +
|
| 3 | +This example demonstrates the .simple accessor available on all ADCPClient instances: |
| 4 | +- Accepts kwargs directly (no request objects needed) |
| 5 | +- Returns unwrapped data (no TaskResult.data unwrapping) |
| 6 | +- Raises exceptions on errors |
| 7 | +
|
| 8 | +Compare this to the standard API which requires explicit request objects |
| 9 | +and TaskResult unwrapping. |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | + |
| 14 | +# Import test agents |
| 15 | +from adcp.testing import creative_agent, test_agent |
| 16 | +from adcp.types.generated import GetProductsRequest |
| 17 | + |
| 18 | + |
| 19 | +async def demo_simple_api(): |
| 20 | + """Demo the .simple accessor API.""" |
| 21 | + print("=== Simple API Demo (client.simple.*) ===\n") |
| 22 | + |
| 23 | + # Simple kwargs-based call, direct data return |
| 24 | + products = await test_agent.simple.get_products( |
| 25 | + brief="Coffee subscription service for busy professionals", |
| 26 | + ) |
| 27 | + |
| 28 | + print(f"Found {len(products.products)} products") |
| 29 | + if products.products: |
| 30 | + product = products.products[0] |
| 31 | + print(f" - {product.name}") |
| 32 | + print(f" {product.description}\n") |
| 33 | + |
| 34 | + # List formats with simple API |
| 35 | + formats = await test_agent.simple.list_creative_formats() |
| 36 | + print(f"Found {len(formats.formats)} creative formats") |
| 37 | + if formats.formats: |
| 38 | + fmt = formats.formats[0] |
| 39 | + print(f" - {fmt.name}") |
| 40 | + print(f" {fmt.description}\n") |
| 41 | + |
| 42 | + # Creative agent also has .simple accessor |
| 43 | + print("Creative agent preview:") |
| 44 | + try: |
| 45 | + preview = await creative_agent.simple.preview_creative( |
| 46 | + manifest={ |
| 47 | + "format_id": { |
| 48 | + "id": "banner_300x250", |
| 49 | + "agent_url": "https://creative.adcontextprotocol.org", |
| 50 | + }, |
| 51 | + "assets": {}, |
| 52 | + } |
| 53 | + ) |
| 54 | + if preview.previews: |
| 55 | + print(f" Generated {len(preview.previews)} preview(s)\n") |
| 56 | + except Exception as e: |
| 57 | + print(f" Preview failed (expected for demo): {e}\n") |
| 58 | + |
| 59 | + |
| 60 | +async def demo_standard_api_comparison(): |
| 61 | + """Compare with standard API for reference.""" |
| 62 | + print("=== Standard API (for comparison) ===\n") |
| 63 | + |
| 64 | + # Standard API: More verbose but full control over error handling |
| 65 | + request = GetProductsRequest( |
| 66 | + brief="Coffee subscription service for busy professionals", |
| 67 | + ) |
| 68 | + |
| 69 | + result = await test_agent.get_products(request) |
| 70 | + |
| 71 | + if result.success and result.data: |
| 72 | + print(f"Found {len(result.data.products)} products") |
| 73 | + if result.data.products: |
| 74 | + product = result.data.products[0] |
| 75 | + print(f" - {product.name}") |
| 76 | + print(f" {product.description}\n") |
| 77 | + else: |
| 78 | + print(f"Error: {result.error}\n") |
| 79 | + |
| 80 | + |
| 81 | +async def demo_production_client(): |
| 82 | + """Show that .simple works on any ADCPClient.""" |
| 83 | + print("=== Simple API on Production Clients ===\n") |
| 84 | + |
| 85 | + # Create a production client |
| 86 | + from adcp import ADCPClient, AgentConfig, Protocol |
| 87 | + from adcp.testing import TEST_AGENT_TOKEN |
| 88 | + |
| 89 | + client = ADCPClient( |
| 90 | + AgentConfig( |
| 91 | + id="my-agent", |
| 92 | + agent_uri="https://test-agent.adcontextprotocol.org/mcp/", |
| 93 | + protocol=Protocol.MCP, |
| 94 | + auth_token=TEST_AGENT_TOKEN, # Public test token (rate-limited) |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + # Both APIs available |
| 99 | + print("Standard API:") |
| 100 | + result = await client.get_products(GetProductsRequest(brief="Test")) |
| 101 | + print(f" Result type: {type(result).__name__}") |
| 102 | + print(f" Has .success: {hasattr(result, 'success')}") |
| 103 | + print(f" Has .data: {hasattr(result, 'data')}\n") |
| 104 | + |
| 105 | + print("Simple API:") |
| 106 | + try: |
| 107 | + products = await client.simple.get_products(brief="Test") |
| 108 | + print(f" Result type: {type(products).__name__}") |
| 109 | + print(f" Direct access to .products: {hasattr(products, 'products')}") |
| 110 | + except Exception as e: |
| 111 | + print(f" (Expected error for demo: {e})") |
| 112 | + |
| 113 | + |
| 114 | +def demo_sync_usage(): |
| 115 | + """Show how to use simple API in sync contexts.""" |
| 116 | + print("\n=== Using Simple API in Sync Contexts ===\n") |
| 117 | + |
| 118 | + print("The simple API is async-only, but you can use asyncio.run() for sync contexts:") |
| 119 | + print() |
| 120 | + print(" # In a Jupyter notebook or sync function:") |
| 121 | + print(" import asyncio") |
| 122 | + print(" from adcp.testing import test_agent") |
| 123 | + print() |
| 124 | + print(" products = asyncio.run(test_agent.simple.get_products(brief='Coffee'))") |
| 125 | + print(" print(f'Found {len(products.products)} products')") |
| 126 | + print() |
| 127 | + print(" # Or create an async function and run it:") |
| 128 | + print(" async def my_function():") |
| 129 | + print(" products = await test_agent.simple.get_products(brief='Coffee')") |
| 130 | + print(" return products") |
| 131 | + print() |
| 132 | + print(" result = asyncio.run(my_function())") |
| 133 | + print() |
| 134 | + |
| 135 | + |
| 136 | +async def main(): |
| 137 | + """Run all demos.""" |
| 138 | + print("\n" + "=" * 60) |
| 139 | + print("ADCP Python SDK - Simple API Demo") |
| 140 | + print("=" * 60 + "\n") |
| 141 | + |
| 142 | + # Demo simple API |
| 143 | + await demo_simple_api() |
| 144 | + |
| 145 | + # Show standard API for comparison |
| 146 | + await demo_standard_api_comparison() |
| 147 | + |
| 148 | + # Show it works on any client |
| 149 | + await demo_production_client() |
| 150 | + |
| 151 | + # Show sync usage pattern |
| 152 | + demo_sync_usage() |
| 153 | + |
| 154 | + print("\n" + "=" * 60) |
| 155 | + print("Key Differences:") |
| 156 | + print("=" * 60) |
| 157 | + print("\nSimple API (client.simple.*):") |
| 158 | + print(" ✓ Kwargs instead of request objects") |
| 159 | + print(" ✓ Direct data return (no unwrapping)") |
| 160 | + print(" ✓ Raises exceptions on errors") |
| 161 | + print(" ✓ Available on ALL ADCPClient instances") |
| 162 | + print(" ✓ Use asyncio.run() for sync contexts") |
| 163 | + print(" → Best for: documentation, examples, quick testing, notebooks") |
| 164 | + print("\nStandard API (client.*):") |
| 165 | + print(" ✓ Explicit request objects (type-safe)") |
| 166 | + print(" ✓ TaskResult wrapper (full status info)") |
| 167 | + print(" ✓ Explicit error handling") |
| 168 | + print(" → Best for: production code, complex workflows, webhooks") |
| 169 | + print("\n") |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + asyncio.run(main()) |
0 commit comments