Skip to content

refactor(translation): implement eFormApi.BasePn's ITranslationService (needs BasePn 10.0.37) - #8110

Merged
renemadsen merged 2 commits into
stablefrom
fix/1384-translation-service-from-basepn
Oct 4, 2026
Merged

renemadsen merged 2 commits into
stablefrom
fix/1384-translation-service-from-basepn

Conversation

@renemadsen

@renemadsen renemadsen commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

eFormApi.BasePn 10.0.37 is on nuget.org (microting/eFormApi.BasePn#974). Merge this before the backendconfiguration-plugin PR for #1384.

Summary

The host switches to the translation interface that moved to eFormApi.BasePn, so plugins can translate server-side (microting/eform-backendconfiguration-plugin#1384, v2 of #1324). The implementation, GoogleTranslateOptions and the API key stay in the host.

  • TranslationService now implements Microting.eFormApi.BasePn.Abstractions.Translation.ITranslationService:
    • The key comes from IOptions<GoogleTranslateOptions>, and IsConfigured reports whether a key is set.
    • Without a key it returns an unsuccessful result and does not call Google.
    • The FAKE_TRANSLATE_E2E hook is unchanged.
  • Failures no longer throw, as the interface promises: a network failure (HttpRequestException), a timeout (TaskCanceledException) or an unreadable response (JsonException) becomes an unsuccessful result.
  • One static HttpClient (10 s timeout, PooledConnectionLifetime 5 min) replaces a new client per call. The service is registered as a singleton.
  • TranslationController: translation-possible now reads IsConfigured. The host's own Abstractions/ITranslationService.cs is removed.
  • Bumps Microting.eFormApi.BasePn from 10.0.36 to 10.0.37.

Tests

  • eFormAPI.Web.Tests/TranslationServiceTests: two new tests. Without a key the service is not configured and fails without a network call. With a key, IsConfigured is true.
  • The integration test is updated for the new constructor.

Not verified

Refs microting/eform-backendconfiguration-plugin#1384

🤖 Generated with Claude Code

renemadsen and others added 2 commits October 3, 2026 11:22
microting/eform-backendconfiguration-plugin#1384)

The translation interface moves to eFormApi.BasePn 10.0.37 so plugins can
translate server-side; the host keeps the implementation and the options,
so the API key never leaves the host.

- TranslationService implements the BasePn interface, reads the key from
  IOptions<GoogleTranslateOptions> and exposes IsConfigured; the
  translation-possible endpoint uses it. Without a key it fails without
  calling Google.
- As the interface promises, a network failure, timeout or unreadable
  response becomes an unsuccessful result instead of an exception.
- One static HttpClient (10 s timeout, pooled connections recycled every
  5 min) instead of a client per call; the service is now a singleton.
- Bumps Microting.eFormApi.BasePn to 10.0.37.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

Review effort: Lite
Findings: 1 High severity · 4 Medium severity

Open (5)
What changed in this PR

Refactors the host’s translation implementation to adopt Microting.eFormApi.BasePn’s ITranslationService so plugins can perform server-side translations while keeping Google Translate configuration in the host.

Changes:

  • Upgrades Microting.eFormApi.BasePn to 10.0.37 and switches DI to register translation as a singleton.
  • Updates TranslationService to implement BasePn’s translation interface, moving API key handling into options with a shared HttpClient and non-throwing failures.
  • Updates controller + tests to match the new interface/constructor and removes the host-local translation abstraction.
File Description
eFormAPI/​eFormAPI.Web/​eFormAPI.Web.csproj Bumps BasePn dependency to the version that contains the moved translation interface.
eFormAPI/​eFormAPI.Web/​Startup.cs Registers the translation service as a singleton against BasePn’s ITranslationService.
eFormAPI/​eFormAPI.Web/​Services/​TranslationService.cs Implements BasePn translation interface; adds options-based API key handling, shared HttpClient, and exception-to-failure mapping.
eFormAPI/​eFormAPI.Web/​Controllers/​TranslationController.cs Removes API key parameter passing and relies on ITranslationService.IsConfigured.
eFormAPI/​eFormAPI.Web/​Abstractions/​ITranslationService.cs Removes the host’s translation interface in favor of BasePn’s interface.
eFormAPI/​eFormAPI.Web.Tests/​TranslationServiceTests.cs Adds unit tests for IsConfigured behavior and no-key failure behavior.
eFormAPI/​eFormAPI.Web.Integration.Tests/​Services/​TranslationServiceTests.cs Updates integration test construction for the new options-based service constructor.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +61 to +65
catch (Exception e) when (e is HttpRequestException or TaskCanceledException or JsonException)
{
Console.WriteLine($"Translate error: {e.Message}");
return new OperationDataResult<string>(false, $"Translate failed: {e.Message}");
}
Comment on lines +32 to +39
public async Task TranslateText_WithoutApiKey_IsNotConfigured_AndFailsWithoutCallingGoogle(string apiKey)
{
var service = new TranslationService(Options.Create(new GoogleTranslateOptions { ApiKey = apiKey }));

Assert.That(service.IsConfigured, Is.False);
var result = await service.TranslateText("Tank 4", "da", "en-US");
Assert.That(result.Success, Is.False);
}
Comment on lines +30 to +32
private readonly string _apiKey = options.Value.ApiKey;

public bool IsConfigured => !string.IsNullOrEmpty(_apiKey);
// network failure or the timeout never fails the caller's save.
try
{
using var response = await Client.PostAsync(apiUrl, new FormUrlEncodedContent(form));
<PackageReference Include="Microting.eForm" Version="10.0.41" />
<PackageReference Include="Microting.EformAngularFrontendBase" Version="10.0.40" />
<PackageReference Include="Microting.eFormApi.BasePn" Version="10.0.36" />
<PackageReference Include="Microting.eFormApi.BasePn" Version="10.0.37" />
@renemadsen
renemadsen merged commit 156a453 into stable Oct 4, 2026
18 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants