| 
 | 1 | +using Microsoft.Extensions.DependencyInjection;  | 
 | 2 | +using Microsoft.Extensions.Logging;  | 
 | 3 | +using VConSharp.ApiClient;  | 
 | 4 | + | 
 | 5 | +namespace VConSharp.Examples;  | 
 | 6 | + | 
 | 7 | +/// <summary>  | 
 | 8 | +/// Example showing how to use the VConSharp API client.  | 
 | 9 | +/// </summary>  | 
 | 10 | +public static class ApiClientExample  | 
 | 11 | +{  | 
 | 12 | +    /// <summary>  | 
 | 13 | +    /// Runs the API client example.  | 
 | 14 | +    /// </summary>  | 
 | 15 | +    /// <param name="apiUrl">The base URL of the API.</param>  | 
 | 16 | +    /// <param name="apiToken">The API token for authentication.</param>  | 
 | 17 | +    /// <returns>A task representing the asynchronous operation.</returns>  | 
 | 18 | +    public static async Task RunAsync(string apiUrl, string apiToken)  | 
 | 19 | +    {  | 
 | 20 | +        // Set up dependency injection  | 
 | 21 | +        var services = new ServiceCollection();  | 
 | 22 | + | 
 | 23 | +        // Add logging  | 
 | 24 | +        services.AddLogging(builder =>  | 
 | 25 | +        {  | 
 | 26 | +            builder.AddConsole();  | 
 | 27 | +            builder.SetMinimumLevel(LogLevel.Debug);  | 
 | 28 | +        });  | 
 | 29 | + | 
 | 30 | +        // Register the VCon API client  | 
 | 31 | +        services.AddVConApiClient(options =>  | 
 | 32 | +        {  | 
 | 33 | +            options.BaseUrl = apiUrl;  | 
 | 34 | +            options.ApiToken = apiToken;  | 
 | 35 | +        });  | 
 | 36 | + | 
 | 37 | +        // Build the service provider  | 
 | 38 | +        var serviceProvider = services.BuildServiceProvider();  | 
 | 39 | + | 
 | 40 | +        // Get the API client from the service provider  | 
 | 41 | +        var apiClient = serviceProvider.GetRequiredService<IVConApiClient>();  | 
 | 42 | + | 
 | 43 | +        Console.WriteLine("VConSharp API Client Example");  | 
 | 44 | +        Console.WriteLine("============================");  | 
 | 45 | + | 
 | 46 | +        try  | 
 | 47 | +        {  | 
 | 48 | +            // Get a list of vCons  | 
 | 49 | +            Console.WriteLine("Getting a list of vCon UUIDs...");  | 
 | 50 | +            var vconUuids = await apiClient.GetVConsUuidsAsync();  | 
 | 51 | +            Console.WriteLine($"Found {vconUuids.Count} vCons");  | 
 | 52 | + | 
 | 53 | +            if (vconUuids.Count > 0)  | 
 | 54 | +            {  | 
 | 55 | +                // Get the first vCon from the list  | 
 | 56 | +                var firstUuid = Guid.Parse(vconUuids.First());  | 
 | 57 | +                Console.WriteLine($"Getting vCon with UUID: {firstUuid}");  | 
 | 58 | +                var vcon = await apiClient.GetVConAsync(firstUuid);  | 
 | 59 | + | 
 | 60 | +                if (vcon != null)  | 
 | 61 | +                {  | 
 | 62 | +                    Console.WriteLine($"Successfully retrieved vCon: {vcon.Subject ?? "No subject"}");  | 
 | 63 | +                    Console.WriteLine($"Created at: {vcon.CreatedAt}");  | 
 | 64 | +                    Console.WriteLine($"Parties: {vcon.Parties.Count}");  | 
 | 65 | +                    Console.WriteLine($"Dialogs: {vcon.Dialog?.Count ?? 0}");  | 
 | 66 | +                }  | 
 | 67 | +            }  | 
 | 68 | + | 
 | 69 | +            // Create a new vCon  | 
 | 70 | +            Console.WriteLine("\nCreating a new vCon...");  | 
 | 71 | +            var newVCon = new VCon();  | 
 | 72 | + | 
 | 73 | +            // Set the vCon property (required by the API)  | 
 | 74 | +            newVCon.Vcon = "1.0";  | 
 | 75 | +            newVCon.Subject = "Test conversation";  | 
 | 76 | +            newVCon.AddParty(new Party { Name = "John Doe", Tel = "+1234567890", Mailto = "[email protected]", });  | 
 | 77 | +            newVCon.AddParty(new Party { Name = "Jane Smith", Tel = "+1987654321", Mailto = "[email protected]", });  | 
 | 78 | + | 
 | 79 | +            // Add a text dialog  | 
 | 80 | +            newVCon.AddDialog(new Dialog  | 
 | 81 | +            {  | 
 | 82 | +                Type = "text",  | 
 | 83 | +                Start = DateTime.UtcNow,  | 
 | 84 | +                Parties = new List<int> { 0, },  | 
 | 85 | +                Body = "Hello, this is John!",  | 
 | 86 | +            });  | 
 | 87 | + | 
 | 88 | +            newVCon.AddDialog(new Dialog  | 
 | 89 | +            {  | 
 | 90 | +                Type = "text",  | 
 | 91 | +                Start = DateTime.UtcNow.AddMinutes(1),  | 
 | 92 | +                Parties = new List<int> { 1, },  | 
 | 93 | +                Body = "Hi John, nice to meet you!",  | 
 | 94 | +            });  | 
 | 95 | + | 
 | 96 | +            // Save the vCon via the API  | 
 | 97 | +            var createdVCon = await apiClient.CreateVConAsync(newVCon);  | 
 | 98 | +            Console.WriteLine($"Created new vCon with UUID: {createdVCon.Uuid}");  | 
 | 99 | + | 
 | 100 | +            // Search for vCons by party information  | 
 | 101 | +            Console.WriteLine("\nSearching for vCons...");  | 
 | 102 | +            var searchResults = await apiClient.SearchVConsAsync(tel: "+1234567890");  | 
 | 103 | +            Console.WriteLine($"Found {searchResults.Count} vCons matching the search criteria");  | 
 | 104 | + | 
 | 105 | +            // Clean up - delete the vCon we created  | 
 | 106 | +            Console.WriteLine("\nDeleting the newly created vCon...");  | 
 | 107 | +            await apiClient.DeleteVConAsync(Guid.Parse(createdVCon.Uuid));  | 
 | 108 | +            Console.WriteLine("vCon deleted successfully");  | 
 | 109 | + | 
 | 110 | +            // Get system configuration  | 
 | 111 | +            Console.WriteLine("\nGetting system configuration...");  | 
 | 112 | +            var config = await apiClient.GetConfigAsync();  | 
 | 113 | +            Console.WriteLine($"System configuration contains {config.Count} settings");  | 
 | 114 | +        }  | 
 | 115 | +        catch (Exception ex)  | 
 | 116 | +        {  | 
 | 117 | +            Console.WriteLine($"Error: {ex.Message}");  | 
 | 118 | +            if (ex.InnerException != null)  | 
 | 119 | +            {  | 
 | 120 | +                Console.WriteLine($"Inner exception: {ex.InnerException.Message}");  | 
 | 121 | +            }  | 
 | 122 | +        }  | 
 | 123 | +    }  | 
 | 124 | +}  | 
0 commit comments