diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/FormRecognizerClientExtensions.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/FormRecognizerClientExtensions.cs new file mode 100644 index 000000000000..cc94a6175df7 --- /dev/null +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/FormRecognizerClientExtensions.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Azure.AI.FormRecognizer.Models +{ + /// + /// + public static class FormRecognizerClientExtensions + { + /// + /// + /// + /// + /// + /// + /// + public static ValueTask>> RecognizeContentAsync(this FormRecognizerClient formRecognizerClient, Stream formFileStream, RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) + => formRecognizerClient.StartRecognizeContentAsync(formFileStream, recognizeOptions, cancellationToken).WaitForCompletionAsync, RecognizeContentOperation>(cancellationToken); + + /// + /// + /// + /// + /// + /// + /// + /// + public static ValueTask>> RecognizeReceiptsFromUriAsync(this FormRecognizerClient formRecognizerClient, Uri receiptFileUri, string receiptLocale = "en-US", RecognizeOptions recognizeOptions = default, CancellationToken cancellationToken = default) + => formRecognizerClient.StartRecognizeReceiptsFromUriAsync(receiptFileUri, receiptLocale, recognizeOptions, cancellationToken).WaitForCompletionAsync, RecognizeReceiptsOperation>(cancellationToken); + } +} diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/OperationExtensions.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/OperationExtensions.cs new file mode 100644 index 000000000000..2b603b8b0472 --- /dev/null +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/src/Generated/Models/OperationExtensions.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Azure.AI.FormRecognizer.Models +{ + /// + /// + public static class OperationExtensions + { + /// + /// + /// + /// + /// + public static async ValueTask> WaitForCompletionAsync(this Task operation, CancellationToken cancellationToken = default) + where U : Operation + { + U o = await operation.ConfigureAwait(false); + return await o.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false); + } + + /// + /// + /// + /// + /// + /// + public static async ValueTask> WaitForCompletionAsync(this Task operation, TimeSpan pollingInterval, CancellationToken cancellationToken = default) + where U : Operation + { + U o = await operation.ConfigureAwait(false); + return await o.WaitForCompletionAsync(pollingInterval, cancellationToken).ConfigureAwait(false); + } + } +} diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs index f729dade6cd0..21dcc17b992b 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/FormRecognizerClient/FormRecognizerClientLiveTests.cs @@ -131,24 +131,25 @@ public async Task StartRecognizeContentPopulatesFormPage(bool useStream) public async Task StartRecognizeReceiptsPopulatesExtractedReceipt(bool useStream) { var client = CreateInstrumentedClient(); - Operation> operation; + IReadOnlyList receipts; if (useStream) { using var stream = new FileStream(TestEnvironment.ReceiptPath, FileMode.Open); - operation = await client.StartRecognizeReceiptsAsync(stream, ContentType.Jpeg); + var operation = await client.StartRecognizeReceiptsAsync(stream, ContentType.Jpeg); + + await operation.WaitForCompletionAsync(); + + Assert.IsTrue(operation.HasValue); + receipts = operation.Value; } else { var uri = new Uri(TestEnvironment.ReceiptUri); - operation = await client.StartRecognizeReceiptsFromUriAsync(uri, default); + receipts = (await client.RecognizeReceiptsFromUriAsync(uri, default)).Value; } - await operation.WaitForCompletionAsync(); - - Assert.IsTrue(operation.HasValue); - - var receipt = operation.Value.Single().AsUSReceipt(); + var receipt = receipts.Single().AsUSReceipt(); // The expected values are based on the values returned by the service, and not the actual