|
2 | 2 |
|
3 | 3 | SDK for the following Sneaks & Data Platform Services: |
4 | 4 |
|
5 | | -- Crystal, a high-performance runner for containerized machine learning algorithms based on Akka framework and Kubernetes Jobs API. |
| 5 | +- Nexus, a high-performance runner for containerized machine learning algorithms based on Akka framework and Kubernetes Jobs API. |
6 | 6 | - Boxer, an authorization API based on Json Web Tokens. |
| 7 | + |
| 8 | +# Usage |
| 9 | +To use the SnD.ApiClient, you can follow these steps: |
| 10 | +1. Install the SnD.ApiClient package from NuGet. |
| 11 | +This package is hosted on GitHub Packages, so you need to add the GitHub Packages NuGet source to your project. |
| 12 | +You can do this by running the following command in your terminal, replacing `your-token` and `your-username` with your GitHub token and username: |
| 13 | +```bash |
| 14 | +export GITHUB_TOKEN=your-token |
| 15 | +export USERNAME=your-username |
| 16 | +dotnet nuget add source --username $USERNAME --password $GITHUB_TOKEN \ |
| 17 | + --store-password-in-clear-text \ |
| 18 | + --name github "https://nuget.pkg.github.com/sneaksAndData/index.json" |
| 19 | +``` |
| 20 | + |
| 21 | +2. Inject the Nexus Client into application dependency injection container: |
| 22 | + |
| 23 | +```csharp |
| 24 | + using SnD.ApiClient.Config; |
| 25 | + using SnD.ApiClient.Extensions; |
| 26 | + |
| 27 | + var services = new ServiceCollection(); |
| 28 | + |
| 29 | + // Add Http Client for Nexus Client |
| 30 | + services.AddHttpClient() |
| 31 | + |
| 32 | + // Add ILoggerFactory for logging |
| 33 | + services.AddLogging(); |
| 34 | + |
| 35 | + // Assuming that we have a configuration object that contains the necessary settings for the Nexus Client |
| 36 | + // named configurationRoot |
| 37 | + services.Configure<NexusClientOptions>(configurationRoot.GetSection(nameof(NexusClientOptions))) |
| 38 | + |
| 39 | + // Add retry policy for transient errors |
| 40 | + // For now, only RetryAllErrors is supported, which retries on all exceptions. In the future, we may add more specific retry policies. |
| 41 | + services.AddNexusRetryPolicy(sp => new RetryAllErrors(sp.GetRequiredService<ILogger<RetryAllErrors>>(), sp.GetRequiredService<IOptions<NexusClientOptions>>())) |
| 42 | + |
| 43 | + // Add the Nexus Client to the dependency injection container |
| 44 | + services.AddNexusClient(); |
| 45 | + |
| 46 | + // Add the Boxer Client to the dependency injection container |
| 47 | + services.AddBoxerClient(configurationRoot.GetSection(nameof(BoxerClientOptions))); |
| 48 | + |
| 49 | + // Add azure credential provider for authentication |
| 50 | + // This is required for Nexus Client to authenticate with Azure Entra ID to obtain the Access token for calling Nexus API. |
| 51 | + // This will add the authenticaiton provider with the "https://management.core.windows.net/.default" scope. |
| 52 | + // Addionaly it will alsot inject Boxer authentication provider to the dependency injection container, |
| 53 | + // which will be used by the Nexus Client to authenticate with Boxer API. |
| 54 | + services.AuthorizeWithBoxerOnAzure().AddAuthenticationProvider() |
| 55 | +``` |
| 56 | + |
| 57 | +## Creating and Awaiting Nexus Jobs |
| 58 | + |
| 59 | +The acceptance test `test/SnD.ApiClient.Tests.Acceptance/Nexus/NexusAcceptancetests.cs` shows the full runtime flow: |
| 60 | + |
| 61 | +1. Resolve `INexusClient` from DI. |
| 62 | +2. Build a `NexusAlgorithmRequest` with the algorithm payload and other parameters. |
| 63 | +3. Call `CreateRunAsync(...)` to submit the run. |
| 64 | +4. Use `AwaitRunAsync(...)` with a polling interval and cancellation timeout. |
| 65 | +5. Check final state with `IsFinished(...)` and optionally `HasSucceeded(...)` if to validate success vs failure. |
| 66 | + |
| 67 | +```csharp |
| 68 | +using System; |
| 69 | +using System.Threading; |
| 70 | +using System.Threading.Tasks; |
| 71 | +using KiotaPosts.Client.Models.Models; |
| 72 | +using Microsoft.Extensions.DependencyInjection; |
| 73 | +using SnD.ApiClient.Nexus; |
| 74 | +using SnD.ApiClient.Nexus.Base; |
| 75 | + |
| 76 | +// Assuning that we resolved INexusClient from the dependency injection container and have the algorithm name and payload ready. |
| 77 | +INexusClient nexusClient; |
| 78 | + |
| 79 | +// Start from generated request model, then attach serialized payload. |
| 80 | +var request = NexusAlgorithmRequest.Create( |
| 81 | + this.configuration.AlgorithmPayload, |
| 82 | + customConfiguration: null, |
| 83 | + parentRequest: null, |
| 84 | + payloadValidFor: "6h", |
| 85 | + requestApiVersion: null, |
| 86 | + tag: "example" |
| 87 | +); |
| 88 | + |
| 89 | +// Use a bounded timeout so await does not run forever. |
| 90 | +using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5)); |
| 91 | + |
| 92 | +// Create the run and get the request ID. |
| 93 | +var createResponse = await nexusClient.CreateRunAsync(request, algorithmName, dryRun: false, cancellationToken: cts.Token); |
| 94 | + |
| 95 | +// Await the run completion with polling, and get the final result. |
| 96 | +var result = await nexusClient.AwaitRunAsync( |
| 97 | + createResponse.RequestId, |
| 98 | + algorithmName, |
| 99 | + pollInterval: TimeSpan.FromSeconds(2), |
| 100 | + cancellationToken: cts.Token); |
| 101 | + |
| 102 | +if (!nexusClient.IsFinished(result)) |
| 103 | +{ |
| 104 | + throw new TimeoutException("Nexus run did not reach a terminal state in time."); |
| 105 | +} |
| 106 | + |
| 107 | +var succeeded = nexusClient.HasSucceeded(result); |
| 108 | +Console.WriteLine($"Run {createResponse.RequestId} finished with status: {result.Status}"); |
| 109 | + |
| 110 | +if (succeeded == false) |
| 111 | +{ |
| 112 | + throw new Exception($"Nexus run failed with status: {result.Status}"); |
| 113 | +} |
| 114 | +``` |
0 commit comments