Skip to content

Commit 76920ca

Browse files
committed
Adds API client example and restructures project
- Introduces an example demonstrating API client usage, including fetching, creating, searching, and deleting vCons. - Restructures the examples project. - Adds necessary dependencies for API client functionality, configuration, and logging. - Provides instructions for setting up user secrets for API credentials.
1 parent dc2b75d commit 76920ca

18 files changed

+1258
-383
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}

VConSharp.Examples/AudioVideoExample.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using VConSharp;
2-
31
namespace VConSharp.Examples
42
{
53
public class AudioVideoExample
@@ -34,7 +32,7 @@ public static void Run()
3432
var textDialog = new Dialog(
3533
"text/plain",
3634
DateTime.UtcNow,
37-
new[] { 0, 1 });
35+
new[] { 0, 1, });
3836

3937
textDialog.Originator = 0;
4038
textDialog.Body = "I need to schedule a video call for technical support.";
@@ -46,7 +44,7 @@ public static void Run()
4644
var audioDialog = new Dialog(
4745
"audio/wav",
4846
DateTime.UtcNow.AddSeconds(1),
49-
new[] { 0, 1 });
47+
new[] { 0, 1, });
5048

5149
audioDialog.Originator = 0;
5250
audioDialog.Mimetype = "audio/wav";
@@ -65,7 +63,7 @@ public static void Run()
6563
var videoDialog = new Dialog(
6664
"video/mp4",
6765
DateTime.UtcNow.AddSeconds(310),
68-
new[] { 0, 1 });
66+
new[] { 0, 1, });
6967

7068
videoDialog.Originator = 1;
7169
videoDialog.Mimetype = "video/mp4";
@@ -92,9 +90,9 @@ public static void Run()
9290
["label"] = "positive",
9391
["segments"] = new[]
9492
{
95-
new Dictionary<string, object> { ["start"] = 0, ["end"] = 60, ["score"] = 0.6, ["label"] = "neutral" },
96-
new Dictionary<string, object> { ["start"] = 60, ["end"] = 180, ["score"] = 0.8, ["label"] = "positive" },
97-
new Dictionary<string, object> { ["start"] = 180, ["end"] = 300, ["score"] = 0.7, ["label"] = "positive" },
93+
new Dictionary<string, object> { ["start"] = 0, ["end"] = 60, ["score"] = 0.6, ["label"] = "neutral", },
94+
new Dictionary<string, object> { ["start"] = 60, ["end"] = 180, ["score"] = 0.8, ["label"] = "positive", },
95+
new Dictionary<string, object> { ["start"] = 180, ["end"] = 300, ["score"] = 0.7, ["label"] = "positive", },
9896
},
9997
},
10098
});
@@ -107,7 +105,7 @@ public static void Run()
107105
["vendor"] = "content-analyzer",
108106
["body"] = new Dictionary<string, object>
109107
{
110-
["labels"] = new[] { "screen-sharing", "technical-support", "software-demo" },
108+
["labels"] = new[] { "screen-sharing", "technical-support", "software-demo", },
111109
["confidence"] = 0.92,
112110
},
113111
});
@@ -134,7 +132,7 @@ public static void Run()
134132
var loadedVcon = VCon.BuildFromJson(json);
135133
Console.WriteLine($"Loaded vCon UUID: {loadedVcon.Uuid}");
136134
Console.WriteLine($"Loaded vCon parties: {loadedVcon.Parties.Count}");
137-
Console.WriteLine($"Loaded vCon dialogs: {loadedVcon.Dialogs.Count}");
135+
Console.WriteLine($"Loaded vCon dialogs: {loadedVcon.Dialog.Count}");
138136
Console.WriteLine($"Loaded vCon analysis: {loadedVcon.Analysis.Count}");
139137
Console.WriteLine($"Loaded vCon tags: {string.Join(", ", loadedVcon.Tags!.Select(t => $"{t.Key}={t.Value}"))}");
140138
}

VConSharp.Examples/Program.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
namespace VConSharp.Examples
1+
using Microsoft.Extensions.Configuration;
2+
3+
namespace VConSharp.Examples
24
{
3-
public class Program
5+
public static class Program
46
{
5-
public static void Main(string[] args)
7+
public static async Task Main(string[] args)
68
{
79
Console.WriteLine("VConSharp Examples");
810
Console.WriteLine("=================");
@@ -11,9 +13,46 @@ public static void Main(string[] args)
1113
// Run simple example
1214
SimpleExample.Run();
1315

14-
// Run audio/video example
15-
Console.WriteLine();
16-
AudioVideoExample.Run();
16+
// Temporarily comment out other examples for testing
17+
// Console.WriteLine();
18+
// AudioVideoExample.Run();
19+
20+
// Load configuration from secrets.json
21+
var configuration = new ConfigurationBuilder()
22+
.AddJsonFile("appsettings.json", optional: true)
23+
.AddUserSecrets(typeof(ApiClientExample).Assembly) // Using assembly instead of static class
24+
.AddCommandLine(args)
25+
.Build();
26+
27+
// Get API settings from configuration
28+
var rootUrl = configuration["root_url"];
29+
var apiToken = configuration["api_token"];
30+
31+
// Run API client example if configuration is available
32+
if (!string.IsNullOrEmpty(rootUrl) && !string.IsNullOrEmpty(apiToken))
33+
{
34+
Console.WriteLine();
35+
Console.WriteLine($"Running API client example with URL: {rootUrl}");
36+
await ApiClientExample.RunAsync(rootUrl, apiToken);
37+
}
38+
39+
// If command line arguments are provided, use those instead
40+
else if (args.Length >= 2)
41+
{
42+
Console.WriteLine();
43+
Console.WriteLine($"Running API client example with URL: {args[0]}");
44+
await ApiClientExample.RunAsync(args[0], args[1]);
45+
}
46+
else
47+
{
48+
Console.WriteLine();
49+
Console.WriteLine("API client example not run. Missing configuration.");
50+
Console.WriteLine("To run the API client example, either:");
51+
Console.WriteLine("1. Add user secrets using: dotnet user-secrets set \"root_url\" \"https://api-url\"");
52+
Console.WriteLine("2. Add user secrets using: dotnet user-secrets set \"api_token\" \"your-token\"");
53+
Console.WriteLine(" OR");
54+
Console.WriteLine("3. Provide as command line arguments: dotnet run --project VConSharp.Examples/VConSharp.Examples.csproj https://api-url x-api-token");
55+
}
1756
}
1857
}
1958
}

VConSharp.Examples/SimpleExample.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using VConSharp;
2-
31
namespace VConSharp.Examples
42
{
5-
public class SimpleExample
3+
public static class SimpleExample
64
{
75
public static void Run()
86
{
@@ -34,7 +32,7 @@ public static void Run()
3432
var textDialog = new Dialog(
3533
"text/plain",
3634
DateTime.UtcNow,
37-
new[] { 0, 1 });
35+
new[] { 0, 1, });
3836

3937
textDialog.Originator = 0;
4038
textDialog.Body = "Hello, I need help with my account.";
@@ -46,7 +44,7 @@ public static void Run()
4644
var responseDialog = new Dialog(
4745
"text/plain",
4846
DateTime.UtcNow.AddSeconds(1),
49-
new[] { 0, 1 });
47+
new[] { 0, 1, });
5048

5149
responseDialog.Originator = 1;
5250
responseDialog.Body = "Hello John, how can I assist you today?";
@@ -64,7 +62,7 @@ public static void Run()
6462
vcon.AddAnalysis(new Dictionary<string, object>
6563
{
6664
["type"] = "sentiment",
67-
["dialog"] = new[] { 0, 1 }, // Analyze both dialogs
65+
["dialog"] = new[] { 0, 1, }, // Analyze both dialogs
6866
["vendor"] = "sentiment-analyzer",
6967
["body"] = new Dictionary<string, object>
7068
{
@@ -85,7 +83,7 @@ public static void Run()
8583
var loadedVcon = VCon.BuildFromJson(json);
8684
Console.WriteLine($"Loaded vCon UUID: {loadedVcon.Uuid}");
8785
Console.WriteLine($"Loaded vCon parties: {loadedVcon.Parties.Count}");
88-
Console.WriteLine($"Loaded vCon dialogs: {loadedVcon.Dialogs.Count}");
86+
Console.WriteLine($"Loaded vCon dialogs: {loadedVcon.Dialog.Count}");
8987
Console.WriteLine($"Loaded vCon attachments: {loadedVcon.Attachments.Count}");
9088
Console.WriteLine($"Loaded vCon analysis: {loadedVcon.Analysis.Count}");
9189
Console.WriteLine($"Loaded vCon tags: {loadedVcon.GetTag("category")}, {loadedVcon.GetTag("priority")}");

VConSharp.Examples/UserSecrets.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Setting Up User Secrets for VConSharp Examples
2+
3+
The VConSharp.Examples project uses the .NET user secrets feature to store API credentials securely.
4+
5+
## Prerequisites
6+
7+
Make sure you have the .NET SDK installed.
8+
9+
## Steps to Set Up User Secrets
10+
11+
1. Navigate to the VConSharp.Examples project directory:
12+
13+
```bash
14+
cd /path/to/VConSharp/VConSharp.Examples
15+
```
16+
17+
2. Set your API URL:
18+
19+
```bash
20+
dotnet user-secrets set "root_url" "https://your-api-url"
21+
```
22+
23+
3. Set your API token:
24+
25+
```bash
26+
dotnet user-secrets set "api_token" "your-api-token"
27+
```
28+
29+
4. Verify your secrets (optional):
30+
31+
```bash
32+
dotnet user-secrets list
33+
```
34+
35+
## Alternative: Using Command Line Arguments
36+
37+
You can also provide the API URL and token as command line arguments:
38+
39+
```bash
40+
dotnet run --project VConSharp.Examples/VConSharp.Examples.csproj https://your-api-url your-api-token
41+
```
42+
43+
## Notes
44+
45+
- User secrets are stored outside of your project directory, so they won't be committed to source control.
46+
- In a production environment, you might want to use environment variables or a secure key vault instead of user secrets.

VConSharp.Examples/VConSharp.Examples.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
<ProjectReference Include="..\VConSharp\VConSharp.csproj" />
55
</ItemGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.6" />
9+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.6" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.6" />
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
12+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.6" />
14+
</ItemGroup>
15+
716
<PropertyGroup>
817
<OutputType>Exe</OutputType>
918
<TargetFramework>net9.0</TargetFramework>
1019
<ImplicitUsings>enable</ImplicitUsings>
1120
<Nullable>enable</Nullable>
21+
<IsPackable>false</IsPackable>
22+
<UserSecretsId>be2946ba-cf7a-462f-bb02-4d18ab441e82</UserSecretsId>
1223
</PropertyGroup>
1324

1425
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"System": "Warning"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

0 commit comments

Comments
 (0)