Skip to content

Commit

Permalink
Fix files, add sample and service
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeJayTC committed Apr 18, 2023
1 parent 67da1d7 commit 75fd2c6
Show file tree
Hide file tree
Showing 39 changed files with 2,211 additions and 1,924 deletions.
14 changes: 14 additions & 0 deletions samples/ASP.NET/ASP.NET.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\DeepL.Service\DeepL.Service.csproj" />
<ProjectReference Include="..\..\src\DeepL\DeepL.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions samples/ASP.NET/Controllers/TranslateController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 DeepL.Service;
using Microsoft.AspNetCore.Mvc;

namespace ASP.NET.Controllers;

public class TranslateController : Controller {

private readonly DeepLService deepl;

public TranslateController(DeepLService deepLService) {
this.deepl = deepLService;
}

[HttpGet]
public async Task<IActionResult> Index(string translateMe, string targetLanguage) {
var result = await deepl.client.TranslateTextAsync(translateMe, string.Empty, targetLanguage);
return Ok(result.Text);
}
}
46 changes: 46 additions & 0 deletions samples/ASP.NET/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 DeepL.Model.Options;
using DeepL.Service;

namespace ASP.NET;

public class Program {
public static void Main(string[] args) {
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

// Add DeepL Services
builder.Services.AddDeepL(options => {
options.ServiceLifetime = ServiceLifetime.Singleton;
options.ApiKey = "test";
options.TranslatorOptions = new TranslatorOptions() {
appInfo = new AppInfo() {
AppName = "test",
AppVersion = "1.0.0",
}
};
});




var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthorization();


app.MapControllers();

app.Run();
}
}
41 changes: 41 additions & 0 deletions samples/ASP.NET/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:41365",
"sslPort": 44320
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5030",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:7011;http://localhost:5030",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions samples/ASP.NET/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/ASP.NET/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
13 changes: 13 additions & 0 deletions src/DeepL.Service/DeepL.Service.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DeepL\DeepL.csproj" />
</ItemGroup>

</Project>
68 changes: 68 additions & 0 deletions src/DeepL.Service/DeepLService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 DeepL.Model.Interfaces;
using DeepL.Model.Options;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace DeepL.Service;

public class DeepLConfiguration {

public TranslatorOptions TranslatorOptions { get; set; }
public ServiceLifetime ServiceLifetime { get; set; } = ServiceLifetime.Scoped;
public string ApiKey { get; set; }

public Type? customTranslator { get; set; } = null;

}

public class DeepLService {

public Translator client { get; set; }

public DeepLService(IOptions<DeepLConfiguration> options) {
if(options.Value == null) throw new ArgumentNullException(nameof(options.Value));
this.client = new Translator(options.Value.ApiKey, options.Value.TranslatorOptions);
}
}


public static class DeepLExtension {

/// <summary>
/// Adds DeepL to DI
/// </summary>
/// <param name="services">Service Collection</param>
/// <param name="lifetime">Service Lifetime</param>
/// <param name="option" cref="TranslatorOptions">Translator Options</param>
/// <returns></returns>
public static IServiceCollection AddDeepL(this IServiceCollection services, Action<DeepLConfiguration> options) {

if (services == null) throw new ArgumentNullException(nameof(services));
if (options == null) throw new ArgumentNullException(nameof(options));

//Apply Config
services.AddOptions();
services.Configure(options);

var deeplConfig = new DeepLConfiguration();
options?.Invoke(deeplConfig);

// Lets see if we want to use a custom translator
var translatorType = typeof(Translator);
if (deeplConfig.customTranslator != null) {
if (deeplConfig.customTranslator.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ITranslator))) {
translatorType = typeof(Translator);
} else {
throw new ArgumentException("CustomTranslator must implement ITranslator Interface");
}
}

services.Add(new ServiceDescriptor(typeof(ITranslator), translatorType, deeplConfig.ServiceLifetime));
return services;
}
}

30 changes: 28 additions & 2 deletions src/DeepL.net.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepL", "DeepL\DeepL.csproj", "{87DDF1B7-2007-4E90-BDF3-6F6AE465A853}"
# Visual Studio Version 17
VisualStudioVersion = 17.6.33513.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeepL", "DeepL\DeepL.csproj", "{87DDF1B7-2007-4E90-BDF3-6F6AE465A853}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepLTests", "DeepLTests\DeepLTests.csproj", "{3582DAA3-A216-4D58-83C8-BA91B4EE2260}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeepLTests", "DeepLTests\DeepLTests.csproj", "{3582DAA3-A216-4D58-83C8-BA91B4EE2260}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepL.Service", "DeepL.Service\DeepL.Service.csproj", "{C649CF0E-AE41-4964-A958-4D70FDFB5CDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASP.NET", "..\samples\ASP.NET\ASP.NET.csproj", "{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{B4D0D020-6F5B-4C38-BE54-6CD8F1DA2971}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,5 +27,22 @@ Global
{3582DAA3-A216-4D58-83C8-BA91B4EE2260}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3582DAA3-A216-4D58-83C8-BA91B4EE2260}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3582DAA3-A216-4D58-83C8-BA91B4EE2260}.Release|Any CPU.Build.0 = Release|Any CPU
{C649CF0E-AE41-4964-A958-4D70FDFB5CDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C649CF0E-AE41-4964-A958-4D70FDFB5CDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C649CF0E-AE41-4964-A958-4D70FDFB5CDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C649CF0E-AE41-4964-A958-4D70FDFB5CDE}.Release|Any CPU.Build.0 = Release|Any CPU
{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4EB6C377-EBBA-4C62-9D71-4EFED283D7A2} = {B4D0D020-6F5B-4C38-BE54-6CD8F1DA2971}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CC5D0C9B-8F46-46B0-9877-CA22C51A5ABB}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 75fd2c6

Please sign in to comment.