|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using ArcadeDotnet; |
| 5 | +using ArcadeDotnet.Models.Tools; |
| 6 | +using ArcadeDotnet.Exceptions; |
| 7 | +using ArcadeDotnet.Models; |
| 8 | + |
| 9 | +namespace Examples; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Basic example demonstrating how to use the Arcade SDK. |
| 13 | +/// </summary> |
| 14 | +class Program |
| 15 | +{ |
| 16 | + static async Task Main(string[] args) |
| 17 | + { |
| 18 | + // Create client using environment variables |
| 19 | + var client = new ArcadeClient(); |
| 20 | + Console.WriteLine($"Connected to: {client.BaseUrl}\n"); |
| 21 | + |
| 22 | + // Example 1: Execute a Simple Tool (No OAuth Required) |
| 23 | + Console.WriteLine("=== Example 1: Execute Simple Tool (No OAuth) ==="); |
| 24 | + Console.WriteLine(" Note: Most tools require OAuth. This example shows the pattern."); |
| 25 | + Console.WriteLine(" For a working example, use a tool that doesn't require authentication."); |
| 26 | + try |
| 27 | + { |
| 28 | + // Example: Execute a tool (this will likely require UserID for most tools) |
| 29 | + // In practice, you'd use a tool that doesn't need OAuth like math operations |
| 30 | + var executeParams = new ToolExecuteParams |
| 31 | + { |
| 32 | + ToolName = "CheckArcadeEngineHealth", // Example tool name |
| 33 | + // UserID = "user-id" // Required for most tools |
| 34 | + }; |
| 35 | + |
| 36 | + var result = await client.Tools.Execute(executeParams); |
| 37 | + result.Validate(); |
| 38 | + |
| 39 | + Console.WriteLine($"✅ Tool executed successfully!"); |
| 40 | + Console.WriteLine($" Execution ID: {result.ExecutionID}"); |
| 41 | + Console.WriteLine($" Status: {result.Status}"); |
| 42 | + Console.WriteLine($" Success: {result.Success}"); |
| 43 | + } |
| 44 | + catch (ArcadeBadRequestException ex) |
| 45 | + { |
| 46 | + Console.WriteLine($" ⚠️ Expected: Most tools require UserID or specific parameters"); |
| 47 | + Console.WriteLine($" Error: {ex.Message}"); |
| 48 | + Console.WriteLine($" Tip: Use Tools.Authorize() first for OAuth tools"); |
| 49 | + } |
| 50 | + catch (ArcadeNotFoundException ex) |
| 51 | + { |
| 52 | + Console.WriteLine($"❌ Tool not found: {ex.Message}"); |
| 53 | + } |
| 54 | + catch (Exception ex) |
| 55 | + { |
| 56 | + Console.WriteLine($"❌ Error: {ex.GetType().Name}: {ex.Message}"); |
| 57 | + } |
| 58 | + |
| 59 | + // Example 2: Execute Tool Requiring OAuth (GitHub Example) |
| 60 | + Console.WriteLine("\n=== Example 2: Tool Requiring OAuth (GitHub) ==="); |
| 61 | + try |
| 62 | + { |
| 63 | + // For tools requiring OAuth, you need to authorize first |
| 64 | + // This example shows the pattern (GitHub tools require OAuth) |
| 65 | + var authorizeParams = new ToolAuthorizeParams |
| 66 | + { |
| 67 | + ToolName = "GitHub.ListRepositories" // Example GitHub tool |
| 68 | + }; |
| 69 | + |
| 70 | + Console.WriteLine(" Authorizing tool access..."); |
| 71 | + var authResponse = await client.Tools.Authorize(authorizeParams); |
| 72 | + authResponse.Validate(); |
| 73 | + |
| 74 | + Console.WriteLine($" ✅ Authorization initiated!"); |
| 75 | + if (authResponse.Status != null) |
| 76 | + { |
| 77 | + Console.WriteLine($" Status: {authResponse.Status.Value}"); |
| 78 | + } |
| 79 | + if (!string.IsNullOrEmpty(authResponse.URL)) |
| 80 | + { |
| 81 | + Console.WriteLine($" OAuth URL: {authResponse.URL}"); |
| 82 | + } |
| 83 | + Console.WriteLine($" Note: Complete OAuth flow, then use UserID in Execute()"); |
| 84 | + |
| 85 | + // After OAuth completes, execute with UserID: |
| 86 | + // var executeParams = new ToolExecuteParams |
| 87 | + // { |
| 88 | + // ToolName = "GitHub.ListRepositories", |
| 89 | + // UserID = "user-id-from-oauth-flow" |
| 90 | + // }; |
| 91 | + } |
| 92 | + catch (ArcadeNotFoundException ex) |
| 93 | + { |
| 94 | + Console.WriteLine($" ⚠️ Tool not found (this is expected if GitHub tools aren't available)"); |
| 95 | + Console.WriteLine($" Error: {ex.Message}"); |
| 96 | + } |
| 97 | + catch (Exception ex) |
| 98 | + { |
| 99 | + Console.WriteLine($" ⚠️ Error: {ex.GetType().Name}: {ex.Message}"); |
| 100 | + Console.WriteLine(" Note: This demonstrates the OAuth authorization pattern"); |
| 101 | + } |
| 102 | + |
| 103 | + // Example 3: List Available Tools |
| 104 | + Console.WriteLine("\n=== Example 3: List Available Tools ==="); |
| 105 | + try |
| 106 | + { |
| 107 | + var tools = await client.Tools.List(); |
| 108 | + tools.Validate(); |
| 109 | + var count = tools.Items?.Count ?? 0; |
| 110 | + Console.WriteLine($"✅ Found {count} available tools"); |
| 111 | + |
| 112 | + if (tools.Items != null && tools.Items.Count > 0) |
| 113 | + { |
| 114 | + Console.WriteLine($" First tool: {tools.Items[0].Name}"); |
| 115 | + } |
| 116 | + } |
| 117 | + catch (Exception ex) |
| 118 | + { |
| 119 | + Console.WriteLine($"❌ Error: {ex.GetType().Name}: {ex.Message}"); |
| 120 | + } |
| 121 | + |
| 122 | + // Example 4: Get Tool Details |
| 123 | + Console.WriteLine("\n=== Example 4: Get Tool Details ==="); |
| 124 | + try |
| 125 | + { |
| 126 | + var toolParams = new ToolGetParams { Name = "Google.ListEmails" }; |
| 127 | + var tool = await client.Tools.Get(toolParams); |
| 128 | + tool.Validate(); |
| 129 | + Console.WriteLine($"✅ Tool retrieved: {tool.Name}"); |
| 130 | + Console.WriteLine($" Description: {tool.Description ?? "N/A"}"); |
| 131 | + } |
| 132 | + catch (ArcadeNotFoundException ex) |
| 133 | + { |
| 134 | + Console.WriteLine($"❌ Tool not found: {ex.Message}"); |
| 135 | + } |
| 136 | + catch (Exception ex) |
| 137 | + { |
| 138 | + Console.WriteLine($"❌ Error: {ex.GetType().Name}: {ex.Message}"); |
| 139 | + } |
| 140 | + |
| 141 | + // Example 5: Health Check |
| 142 | + Console.WriteLine("\n=== Example 5: Health Check ==="); |
| 143 | + try |
| 144 | + { |
| 145 | + var health = await client.Health.Check(); |
| 146 | + health.Validate(); |
| 147 | + Console.WriteLine($"✅ Health check passed: {health.Healthy}"); |
| 148 | + } |
| 149 | + catch (Exception ex) |
| 150 | + { |
| 151 | + Console.WriteLine($"❌ Error: {ex.GetType().Name}: {ex.Message}"); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments