Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 Intento.SDK/Intento.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

Expand Down
37 changes: 29 additions & 8 deletions Intento.SDK/Translate/TranslateDynamicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Threading.Tasks;
using System.Web;
using Intento.SDK.Client;
using Intento.SDK.Exceptions;
using Intento.SDK.Logging;
using Intento.SDK.Translate.Converters;
using Intento.SDK.Translate.DTO;
using Intento.SDK.Translate.Options;
using Intento.SDK.Validation;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Polly;

namespace Intento.SDK.Translate
{
Expand Down Expand Up @@ -203,14 +205,33 @@ await Client.PostAsync<TranslateRequest, TranslateResponse>(url, request,
// Not a (1) - return result immediately, nothing to wait
return jsonResult;
}

var taskCompletion = new TaskCompletionSource<bool>();
var wrapperTask = await Task.WhenAny(
WaitAsync(taskCompletion, id, 1000),
TimeoutError());
var wrapper = await wrapperTask;
taskCompletion.SetResult(true);

var wrapperResult = await Policy
.Handle<Exception>().Or<IntentoApiException>()
.WaitAndRetryAsync(20, _ => TimeSpan.FromSeconds(1))
.ExecuteAndCaptureAsync(async () =>
{
var response = await CheckAsyncJobAsync(id);
if (response.Done)
{
return response;
}

throw new IntentoException("Job is active");
});
if (wrapperResult.FinalException != null)
{
jsonResult = new TranslateResponse
{
Error = new TranslationRequestError
{
Message = "Async operation haven't finished yet",
Data = wrapperResult.FinalException.Message
}
};
}

var wrapper = wrapperResult.Result;
if (wrapper.Done)
{
jsonResult = wrapper.Response is { Length: > 0 } ? wrapper.Response[0] : new TranslateResponse
Expand Down Expand Up @@ -629,7 +650,7 @@ private async Task<TranslateResponseWrapper> TimeoutError()
await Task.Delay(20000);
return new TranslateResponseWrapper
{
Done = true,
Done = false,
Error = new Error
{
Reason = "Timeout of async operation"
Expand Down