diff --git a/DeepL/DeepL.csproj b/DeepL/DeepL.csproj index 63d3a94..8b068a8 100644 --- a/DeepL/DeepL.csproj +++ b/DeepL/DeepL.csproj @@ -32,6 +32,7 @@ + diff --git a/DeepL/ServiceCollectionExtensions.cs b/DeepL/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..5693335 --- /dev/null +++ b/DeepL/ServiceCollectionExtensions.cs @@ -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? configureOptions = null) { + if (services is null) throw new ArgumentNullException(nameof(services)); + if (configuration is null) throw new ArgumentNullException(nameof(configuration)); + + services.Configure(options => + { + configuration.GetSection("DeepL").Bind(options); + configureOptions?.Invoke(options); + }); + + services.AddScoped(); + + return services; + } + } +} diff --git a/DeepL/Translator.cs b/DeepL/Translator.cs index 44f48e7..3c38bcc 100644 --- a/DeepL/Translator.cs +++ b/DeepL/Translator.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using DeepL.Internal; using DeepL.Model; +using Microsoft.Extensions.Options; namespace DeepL { @@ -416,6 +417,10 @@ public sealed class Translator : ITranslator { /// Internal class implementing HTTP requests. private readonly DeepLClient _client; + /// Initializes a new object using your . + /// Options to setup and control Translator behaviour. + public Translator(IOptions options) : this(options.Value.AuthKey ?? "", options.Value) { } + /// Initializes a new object using your authentication key. /// /// Authentication Key as found in your @@ -851,7 +856,7 @@ await _client.ApiGetAsync("/v2/languages", cancellationToken, queryParams) /// system and language runtime version. /// /// - /// true to send platform information with every API request (default), + /// true to send platform information with every API request (default), /// false to only send the library version. /// /// diff --git a/DeepL/TranslatorOptions.cs b/DeepL/TranslatorOptions.cs index d6d9d78..6a45cd5 100644 --- a/DeepL/TranslatorOptions.cs +++ b/DeepL/TranslatorOptions.cs @@ -9,6 +9,10 @@ namespace DeepL { /// Class containing containing options controlling behaviour. public sealed class TranslatorOptions { + /// + /// Authentication key to access the API. + /// + public string? AuthKey { get; set; } /// /// HTTP headers attached to every HTTP request. By default no extra headers are used. Note that during /// initialization headers for Authorization and User-Agent are added, unless they are