Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service collection extensions #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DeepL/DeepL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.1" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions DeepL/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DeepL {
public static class ServiceCollectionExtensions {
public static IServiceCollection AddDeepL(
this IServiceCollection services,
IConfiguration configuration,
Action<TranslatorOptions>? configureOptions = null) {
if (services is null) throw new ArgumentNullException(nameof(services));
if (configuration is null) throw new ArgumentNullException(nameof(configuration));

services.Configure<TranslatorOptions>(options =>
{
configuration.GetSection("DeepL").Bind(options);
configureOptions?.Invoke(options);
});

services.AddScoped<ITranslator, Translator>();

return services;
}
}
}
7 changes: 6 additions & 1 deletion DeepL/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using DeepL.Internal;
using DeepL.Model;
using Microsoft.Extensions.Options;

namespace DeepL {

Expand Down Expand Up @@ -416,6 +417,10 @@ public sealed class Translator : ITranslator {
/// <summary>Internal class implementing HTTP requests.</summary>
private readonly DeepLClient _client;

/// <summary>Initializes a new <see cref="Translator" /> object using your <see cref="TranslatorOptions" />.</summary>
/// <param name="options">Options to setup and control Translator behaviour.</param>
public Translator(IOptions<TranslatorOptions> options) : this(options.Value.AuthKey ?? "", options.Value) { }

/// <summary>Initializes a new <see cref="Translator" /> object using your authentication key.</summary>
/// <param name="authKey">
/// Authentication Key as found in your
Expand Down Expand Up @@ -851,7 +856,7 @@ await _client.ApiGetAsync("/v2/languages", cancellationToken, queryParams)
/// system and language runtime version.
/// </summary>
/// <param name="sendPlatformInfo">
/// <c>true</c> to send platform information with every API request (default),
/// <c>true</c> to send platform information with every API request (default),
/// <c>false</c> to only send the library version.
/// </param>
/// <param name="AppInfo">
Expand Down
4 changes: 4 additions & 0 deletions DeepL/TranslatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
namespace DeepL {
/// <summary>Class containing containing options controlling <see cref="Translator" /> behaviour.</summary>
public sealed class TranslatorOptions {
/// <summary>
/// Authentication key to access the API.
/// </summary>
public string? AuthKey { get; set; }
/// <summary>
/// HTTP headers attached to every HTTP request. By default no extra headers are used. Note that during
/// <see cref="Translator" /> initialization headers for Authorization and User-Agent are added, unless they are
Expand Down