Skip to content

Commit

Permalink
update: add DI container provider
Browse files Browse the repository at this point in the history
update: update readme.md
  • Loading branch information
ikeliec committed Feb 19, 2024
1 parent 9115410 commit b0f4bf9
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 71 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ dotnet add package dotta.Net
// Register HttpClient to enable dotta.Net make http network requests
builder.Services.AddHttpClient();
builder.Services.AddScoped<Dotta>((serviceProvider) =>
builder.Services.AddDotta(new DottaServiceOptions
{
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
return new Dotta(new DottaOptions
{
ApiKey = "",
BaseUrlProduction = "",
BaseUrlSandbox = "",
Environment = DottaEnvironment.Sandbox,
HttpClient = httpClient
});
ApiKey = "",
BaseUrlProduction = "",
BaseUrlSandbox = "",
Environment = DottaEnvironment.Sandbox,
PrivateKey = "",
PublicKey = ""
});
```

Expand All @@ -44,6 +39,18 @@ public ApiController(Dotta dotta)
```
var response = await _dotta.FaceAttributes(photo);
```
**Dotta Service Options**
| **Option** | **Description** |
| ---------- | --------------- |
| ApiKey | Base64 encode string of your dotta public and private API keys concatenated in this format PUBLICKEY:PRIVATEKEY |
| PublicKey | Your dotta public API key |
| PrivateKey | Your dotta private API key |
| Environment | Enum to specify which dotta environment you want to use |
| BaseUrlProduction | API base url for dotta's production environment. |
| BaseUrlSandbox | API base url for dotta's sandbox or test environment. |

Pass the your public and private key if you don't know how to get a base64 string encoding of your keys. Otherwise, just pass the ApiKey. When you pass the ApiKey, you won't need to pass the public and private keys


### Other links
- [NuGet Package][nugetlink]
Expand Down
35 changes: 22 additions & 13 deletions Src/dotta.Net.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

// Add services to the container.
builder.Services.AddHttpClient();

builder.Services.AddScoped<Dotta>((serviceProvider) =>
builder.Services.AddDotta(new DottaServiceOptions
{
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
return new Dotta(new DottaOptions
{
ApiKey = "ODZCNzExNjczMkUyNDI2OUFGRjg2NkJGMERBNjBFNjg6MzE0QzJERkYzNDRENDhFRjlFNkUyOTI5RUQ5MEQwRTM=",
BaseUrlProduction = "https://apps.securedrecords.com/dotta-biometrics/api",
BaseUrlSandbox = "https://apps.securedrecords.com/DevDottaBiometrics/api",
Environment = DottaEnvironment.Sandbox,
HttpClient = httpClient
});
ApiKey = "ODZCNzExNjczMkUyNDI2OUFGRjg2NkJGMERBNjBFNjg6MzE0QzJERkYzNDRENDhFRjlFNkUyOTI5RUQ5MEQwRTM=",
BaseUrlProduction = "https://apps.securedrecords.com/dotta-biometrics/api",
BaseUrlSandbox = "https://apps.securedrecords.com/DevDottaBiometrics/api",
Environment = DottaEnvironment.Sandbox,
PrivateKey = "",
PublicKey = ""
});

// builder.Services.AddScoped<Dotta>((serviceProvider) =>
// {
// var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
// var httpClient = httpClientFactory.CreateClient();

// return new Dotta(new DottaOptions
// {
// ApiKey = "ODZCNzExNjczMkUyNDI2OUFGRjg2NkJGMERBNjBFNjg6MzE0QzJERkYzNDRENDhFRjlFNkUyOTI5RUQ5MEQwRTM=",
// BaseUrlProduction = "https://apps.securedrecords.com/dotta-biometrics/api",
// BaseUrlSandbox = "https://apps.securedrecords.com/DevDottaBiometrics/api",
// Environment = DottaEnvironment.Sandbox,
// HttpClient = httpClient
// });
// });

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
Expand Down
28 changes: 28 additions & 0 deletions Src/dotta.Net.Test/DottaTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Moq;

namespace dotta.Net.Test;

public class DottaTest
{
[Fact]
public void Test_InstantiateDotta()
{
// Arrange
var mockHttpClient = new Mock<HttpClient>();
var dottaService = new Dotta(new DottaOptions {
ApiKey = "ABSCDE"
});

// Act
var apiKey = dottaService.ApiKey;
var environment = dottaService.Environment;
var prodBaseUrl = dottaService.BaseUrlProduction;
var sandboxBaseUrl = dottaService.BaseUrlSandbox;

// Assert
Assert.Equal("ABCDE", apiKey);
Assert.Equal(DottaEnvironment.Sandbox, environment);
Assert.Equal("https://apps.securedrecords.com/dotta-biometrics/api/", prodBaseUrl);
Assert.Equal("https://apps.securedrecords.com/DevDottaBiometrics/api/", sandboxBaseUrl);
}
}
46 changes: 0 additions & 46 deletions Src/dotta.Net.Test/UnitTest1.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Src/dotta.Net/Models/DottaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

namespace dotta.Net
{
public class DottaServiceOptions
{
public string ApiKey { get; set; }
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public DottaEnvironment Environment { get; set; }
public string BaseUrlProduction { get; set; }
public string BaseUrlSandbox { get; set; }
}

public class DottaOptions
{
public string ApiKey { get; set; }
Expand Down
28 changes: 28 additions & 0 deletions Src/dotta.Net/ServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;

namespace dotta.Net
{
public static class ServiceProvider
{
public static IServiceCollection AddDotta(this IServiceCollection services, DottaServiceOptions options)
{
services.AddScoped<Dotta>((serviceProvider) =>
{
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient();
return new Dotta(new DottaOptions
{
ApiKey = options.ApiKey,
BaseUrlProduction = options.BaseUrlProduction,
BaseUrlSandbox = options.BaseUrlSandbox,
Environment = options.Environment,
HttpClient = httpClient
});
});

return services;
}
}
}
2 changes: 2 additions & 0 deletions Src/dotta.Net/dotta.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
</ItemGroup>

Expand Down

0 comments on commit b0f4bf9

Please sign in to comment.