Skip to content
Merged
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 .github/workflows/_test-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ env:
MindeeV2__Findoc__Model__Id: ${{ secrets.MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID }}
MindeeV2__Blank__Pdf__Url: ${{ secrets.MINDEE_V2_SE_TESTS_BLANK_PDF_URL }}
MindeeV2__Failure__Webhook__Id: ${{ secrets.MINDEE_V2_SE_TESTS_FAILURE_WEBHOOK_ID }}
MindeeV2__Se__Tests__Blank__Pdf__Url: ${{ secrets.MINDEE_V2_SE_TESTS_BLANK_PDF_URL }}

jobs:
test-nix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private static void RegisterV1RestSharpClient(IServiceCollection services, bool
#else
services.AddSingleton(provider =>
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var settings = provider.GetRequiredService<IOptions<MindeeSettings>>().Value;
settings.MindeeBaseUrl = Environment.GetEnvironmentVariable("Mindee__BaseUrl");
if (string.IsNullOrEmpty(settings.MindeeBaseUrl))
Expand Down Expand Up @@ -230,6 +231,7 @@ private static void RegisterV2RestSharpClient(IServiceCollection services, bool
// For .NET Framework, register as a named singleton using a wrapper approach
services.AddSingleton(provider =>
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
var settings = provider.GetRequiredService<IOptions<MindeeSettingsV2>>().Value;
settings.MindeeBaseUrl = Environment.GetEnvironmentVariable("MindeeV2__BaseUrl");
if (string.IsNullOrEmpty(settings.MindeeBaseUrl))
Expand Down
21 changes: 14 additions & 7 deletions src/Mindee/Extraction/PdfExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public PdfExtractor(LocalInputSource localInput)
else
{
var memoryStream = new MemoryStream();
var image = SKImage.FromEncodedData(localInput.FileBytes);
var bmp = SKBitmap.FromImage(image);
using var image = SKImage.FromEncodedData(localInput.FileBytes);
using var bmp = SKBitmap.FromImage(image);
var pageSize = new SKSize(bmp.Width, bmp.Height);
using (var document = SKDocument.CreatePdf(memoryStream))
{
var canvas = document.BeginPage(pageSize.Width, pageSize.Height);
canvas.DrawBitmap(bmp, SKPoint.Empty);
document.EndPage();
}

SourcePdf = memoryStream.ToArray();
Expand All @@ -55,8 +56,11 @@ public PdfExtractor(LocalInputSource localInput)
/// <returns>The number of pages in the file.</returns>
public int GetPageCount()
{
var docInstance = DocLib.Instance.GetDocReader(SourcePdf, new PageDimensions(1, 1));
return docInstance.GetPageCount();
lock (DocLib.Instance)
{
using var docInstance = DocLib.Instance.GetDocReader(SourcePdf, new PageDimensions(1, 1));
return docInstance.GetPageCount();
}
}

/// <summary>
Expand Down Expand Up @@ -84,9 +88,12 @@ public List<ExtractedPdf> ExtractSubDocuments(List<List<int>> pageIndexes)
var splitQuery = new SplitQuery(
SourcePdf,
new PageOptions(pageIndexElem.ConvertAll(item => (short)item).ToArray()));
var pdfOperation = new DocNetApi(new NullLogger<DocNetApi>());
var mergedPdfBytes = pdfOperation.Split(splitQuery).File;
extractedPdfs.Add(new ExtractedPdf(mergedPdfBytes, fieldFilename));
lock (DocLib.Instance)
{
var pdfOperation = new DocNetApi(new NullLogger<DocNetApi>());
var mergedPdfBytes = pdfOperation.Split(splitQuery).File;
extractedPdfs.Add(new ExtractedPdf(mergedPdfBytes, fieldFilename));
}
}

return extractedPdfs;
Expand Down
3 changes: 1 addition & 2 deletions src/Mindee/Http/MindeeApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public override async Task<InferenceResponse> ReqGetInferenceAsync(string infere
var request = new RestRequest($"v2/inferences/{inferenceId}");
Logger?.LogInformation("HTTP GET to {RequestResource}...", _baseUrl + request.Resource);
var queueResponse = await _httpClient.ExecuteGetAsync(request);
var handledResponse = ResponseHandler<InferenceResponse>(queueResponse);
return handledResponse;
return ResponseHandler<InferenceResponse>(queueResponse);
}

private static void AddPredictRequestParameters(InferencePostParameters predictParameter, RestRequest request)
Expand Down
2 changes: 1 addition & 1 deletion src/Mindee/Input/LocalInputSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public int GetPageCount()

lock (DocLib.Instance)
{
var docInstance = DocLib.Instance.GetDocReader(FileBytes, new PageDimensions(1, 1));
using var docInstance = DocLib.Instance.GetDocReader(FileBytes, new PageDimensions(1, 1));
return docInstance.GetPageCount();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Mindee/Input/UrlInputSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Threading.Tasks;
using Mindee.Exceptions;
using Mindee.Http;
using RestSharp;
using RestSharp.Authenticators;

Expand Down
9 changes: 4 additions & 5 deletions src/Mindee/MindeeClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -840,12 +839,12 @@ private async Task<AsyncPredictResponse<TInferenceModel>> PollForResultsAsync<TI
_logger?.LogInformation(
"Waiting {} seconds before attempting to retrieve the document...",
pollingOptions.InitialDelaySec);
Thread.Sleep(pollingOptions.InitialDelayMilliSec);
await Task.Delay(pollingOptions.InitialDelayMilliSec);
var retryCount = 1;
AsyncPredictResponse<TInferenceModel> response;
while (retryCount < maxRetries)
{
Thread.Sleep(pollingOptions.IntervalMilliSec);
await Task.Delay(pollingOptions.IntervalMilliSec);
_logger?.LogInformation("Attempting to retrieve: {RetryCount} of {MaxRetries}", retryCount, maxRetries);
response = await ParseQueuedAsync<TInferenceModel>(endpoint, jobId);
if (response.Document != null)
Expand Down Expand Up @@ -887,12 +886,12 @@ private async Task<AsyncPredictResponse<TInferenceModel>> PollForResultsAsync<TI
_logger?.LogInformation(
"Waiting {} seconds before attempting to retrieve the document...",
pollingOptions.InitialDelaySec);
Thread.Sleep(pollingOptions.InitialDelayMilliSec);
await Task.Delay(pollingOptions.InitialDelayMilliSec);
var retryCount = 1;
AsyncPredictResponse<TInferenceModel> response;
while (retryCount < maxRetries)
{
Thread.Sleep(pollingOptions.IntervalMilliSec);
await Task.Delay(pollingOptions.IntervalMilliSec);
_logger?.LogInformation("Attempting to retrieve: {RetryCount} of {MaxRetries}", retryCount, maxRetries);
response = await ParseQueuedAsync<TInferenceModel>(jobId);
if (response.Document != null)
Expand Down
5 changes: 2 additions & 3 deletions src/Mindee/MindeeClientV2.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -270,12 +269,12 @@ private async Task<InferenceResponse> PollForResultsAsync(
_logger?.LogInformation(
"Waiting {} seconds before attempting to retrieve the document...",
pollingOptions.InitialDelaySec);
Thread.Sleep(pollingOptions.InitialDelayMilliSec);
await Task.Delay(pollingOptions.InitialDelayMilliSec);
var retryCount = 1;
var response = enqueueResponse; // First init is only for error handling purposes.
while (retryCount < maxRetries)
{
Thread.Sleep(pollingOptions.IntervalMilliSec);
await Task.Delay(pollingOptions.IntervalMilliSec);
_logger?.LogInformation(
"Attempting to retrieve: {RetryCount} of {MaxRetries}",
retryCount,
Expand Down
6 changes: 0 additions & 6 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,4 @@
<Link>Resources\%(RecursiveDir)%(Filename)%(Extension)</Link>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="resources\async">
<Link>Resources\async</Link>
</Folder>
<Folder Include="V1\Parsing\"/>
</ItemGroup>
</Project>
19 changes: 15 additions & 4 deletions tests/Mindee.IntegrationTests/DependencyInjectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -10,7 +11,7 @@
namespace Mindee.IntegrationTests
{
[Trait("Category", "DI")]
public class DependencyInjectionTest : IDisposable
public class DependencyInjectionTest : IAsyncLifetime
{
private readonly IHost _host;
private readonly IServiceProvider _services;
Expand All @@ -37,13 +38,23 @@ public DependencyInjectionTest()
_host = builder.Build();
_services = _host.Services;
}
public Task InitializeAsync() => Task.CompletedTask;

public void Dispose()
public async Task DisposeAsync()
{
_host.StopAsync().GetAwaiter().GetResult();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(120));
try
{
await _host.StopAsync(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("DependencyInjectionTest teardown: StopAsync timed out after 120s.");
}
_host.Dispose();
}


[Fact]
public void ShouldInitBothClients()
{
Expand All @@ -53,7 +64,7 @@ public void ShouldInitBothClients()
Assert.NotNull(clientV2);
}

[Fact]
[Fact(Timeout = 180000)]
public async Task ShouldMaintainAuthenticationAcrossMultipleRequests()
{
var instance1ClientV1 = _services.GetRequiredService<MindeeClient>();
Expand Down
1 change: 0 additions & 1 deletion tests/Mindee.IntegrationTests/TestingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static class TestingUtilities
private static MindeeClientV2? _mindeeClientV2;

/// <summary>
/// g
/// Gets the API version from an RST output
/// </summary>
/// <param name="rstStr">The RST output of a prediction.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static string PrepareInvoiceReturn(string rstFilePath, Document<InvoiceV
return rstRefLines;
}

[Fact]
[Fact(Timeout = 180000)]
public async Task GivenAPdf_ShouldExtractInvoicesStrict_MustSucceed()
{
var apiKey = Environment.GetEnvironmentVariable("Mindee__ApiKey");
Expand Down
9 changes: 5 additions & 4 deletions tests/Mindee.IntegrationTests/V1/Input/UrlInputSourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ namespace Mindee.IntegrationTests.V1.Input
[Trait("Category", "Send URL")]
public class UrlInputSourceTest
{
[Fact]
[Fact(Timeout = 180000)]
public async Task GivenARemoteFile_MustRetrieveResponse()
{
var apiKey = Environment.GetEnvironmentVariable("Mindee__ApiKey");
var blankUrl = Environment.GetEnvironmentVariable("MindeeV2__Se__Tests__Blank__Pdf__Url");
var client = TestingUtilities.GetOrGenerateMindeeClient(apiKey);
var remoteInput =
new UrlInputSource(
"https://github.com/mindee/client-lib-test-data/blob/main/v1/products/invoice_splitter/invoice_5p.pdf?raw=true");
blankUrl);
var localInput = await remoteInput.AsLocalInputSource();
Assert.Equal("invoice_5p.pdf", localInput.Filename);
Assert.Equal("blank_1.pdf", localInput.Filename);
var result = await client.ParseAsync<InvoiceV4>(localInput);
Assert.Equal(5, result.Document.NPages);
Assert.Equal(1, result.Document.NPages);
}
}
}
Loading
Loading