Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormRecognizer] Remove long-running operation transform #12652

Merged
merged 4 commits into from
Jun 9, 2020
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
13 changes: 13 additions & 0 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/src/ClientCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.AI.FormRecognizer.Models;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.AI.FormRecognizer
Expand Down Expand Up @@ -35,6 +36,18 @@ public static Guid ValidateModelId(string modelId, string paramName)
return guid;
}

public static string GetResponseHeader(ResponseHeaders responseHeaders, string headerName)
{
if (responseHeaders.TryGetValue(headerName, out var headerValue))
{
return headerValue;
}
else
{
throw new KeyNotFoundException($"Header '{headerName}' was not present in the response sent by the server.");
}
}

public static async ValueTask<RequestFailedException> CreateExceptionForFailedOperationAsync(bool async, ClientDiagnostics diagnostics, Response response, IReadOnlyList<FormRecognizerError> errors, string errorMessage = default)
{
string errorCode = default;
Expand Down
4 changes: 4 additions & 0 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ internal static class Constants
{
public const string AuthorizationHeader = "Ocp-Apim-Subscription-Key";

public const string LocationHeader = "Location";

public const string OperationLocationHeader = "Operation-Location";

public const string DefaultCognitiveScope = "https://cognitiveservices.azure.com/.default";

public const float DefaultConfidenceValue = 1.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public virtual RecognizeContentOperation StartRecognizeContent(Stream form, Reco
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(form, nameof(form));

ResponseWithHeaders<ServiceAnalyzeLayoutAsyncHeaders> response = ServiceClient.AnalyzeLayoutAsync(contentType, form, cancellationToken);
return new RecognizeContentOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeLayoutAsync(contentType, form, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeContentOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -152,8 +154,10 @@ public virtual async Task<RecognizeContentOperation> StartRecognizeContentAsync(
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(form, nameof(form));

ResponseWithHeaders<ServiceAnalyzeLayoutAsyncHeaders> response = await ServiceClient.AnalyzeLayoutAsyncAsync(contentType, form, cancellationToken).ConfigureAwait(false);
return new RecognizeContentOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeLayoutAsyncAsync(contentType, form, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeContentOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -170,8 +174,10 @@ public virtual RecognizeContentOperation StartRecognizeContentFromUri(Uri formUr
Argument.AssertNotNull(formUrl, nameof(formUrl));

SourcePath_internal sourcePath = new SourcePath_internal(formUrl.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeLayoutAsyncHeaders> response = ServiceClient.AnalyzeLayoutAsync(sourcePath, cancellationToken);
return new RecognizeContentOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeLayoutAsync(sourcePath, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeContentOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -188,8 +194,10 @@ public virtual async Task<RecognizeContentOperation> StartRecognizeContentFromUr
Argument.AssertNotNull(formUrl, nameof(formUrl));

SourcePath_internal sourcePath = new SourcePath_internal(formUrl.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeLayoutAsyncHeaders> response = await ServiceClient.AnalyzeLayoutAsyncAsync(sourcePath, cancellationToken).ConfigureAwait(false);
return new RecognizeContentOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeLayoutAsyncAsync(sourcePath, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeContentOperation(ServiceClient, Diagnostics, location);
}

#endregion
Expand All @@ -212,8 +220,10 @@ public virtual async Task<RecognizeReceiptsOperation> StartRecognizeReceiptsAsyn
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(receipt, nameof(receipt));

ResponseWithHeaders<ServiceAnalyzeReceiptAsyncHeaders> response = await ServiceClient.AnalyzeReceiptAsyncAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false);
return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeReceiptAsyncAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -232,8 +242,10 @@ public virtual RecognizeReceiptsOperation StartRecognizeReceipts(Stream receipt,
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(receipt, nameof(receipt));

ResponseWithHeaders<ServiceAnalyzeReceiptAsyncHeaders> response = ServiceClient.AnalyzeReceiptAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken);
return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeReceiptAsync(contentType, receipt, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -252,8 +264,10 @@ public virtual async Task<RecognizeReceiptsOperation> StartRecognizeReceiptsFrom
recognizeOptions ??= new RecognizeOptions();

SourcePath_internal sourcePath = new SourcePath_internal(receiptUrl.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeReceiptAsyncHeaders> response = await ServiceClient.AnalyzeReceiptAsyncAsync(includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken).ConfigureAwait(false);
return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeReceiptAsyncAsync(includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -272,8 +286,10 @@ public virtual RecognizeReceiptsOperation StartRecognizeReceiptsFromUri(Uri rece
recognizeOptions ??= new RecognizeOptions();

SourcePath_internal sourcePath = new SourcePath_internal(receiptUrl.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeReceiptAsyncHeaders> response = ServiceClient.AnalyzeReceiptAsync(includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken);
return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeReceiptAsync(includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeReceiptsOperation(ServiceClient, Diagnostics, location);
}

#endregion
Expand All @@ -300,8 +316,10 @@ public virtual RecognizeCustomFormsOperation StartRecognizeCustomForms(string mo
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream));

ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = ServiceClient.AnalyzeWithCustomModel(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken);
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeWithCustomModel(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -324,8 +342,10 @@ public virtual RecognizeCustomFormsOperation StartRecognizeCustomFormsFromUri(st
recognizeOptions ??= new RecognizeOptions();

SourcePath_internal sourcePath = new SourcePath_internal(formFileUri.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = ServiceClient.AnalyzeWithCustomModel(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken);
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = ServiceClient.AnalyzeWithCustomModel(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -348,8 +368,10 @@ public virtual async Task<RecognizeCustomFormsOperation> StartRecognizeCustomFor
recognizeOptions ??= new RecognizeOptions();
FormContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream));

ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false);
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location);
}

/// <summary>
Expand All @@ -372,8 +394,10 @@ public virtual async Task<RecognizeCustomFormsOperation> StartRecognizeCustomFor
recognizeOptions ??= new RecognizeOptions();

SourcePath_internal sourcePath = new SourcePath_internal(formFileUri.AbsoluteUri);
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken).ConfigureAwait(false);
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
Response response = await ServiceClient.AnalyzeWithCustomModelAsync(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.OperationLocationHeader);

return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, location);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ internal static string GetVersionString(ServiceVersion version)
/// </summary>
private void AddLoggedHeadersAndQueryParameters()
{
Diagnostics.LoggedHeaderNames.Add(Constants.LocationHeader);
Diagnostics.LoggedHeaderNames.Add(Constants.OperationLocationHeader);
Diagnostics.LoggedHeaderNames.Add("apim-request-id");
Diagnostics.LoggedHeaderNames.Add("Location");
Diagnostics.LoggedHeaderNames.Add("Operation-Location");
Diagnostics.LoggedHeaderNames.Add("Strict-Transport-Security");
Diagnostics.LoggedHeaderNames.Add("X-Content-Type-Options");
Diagnostics.LoggedHeaderNames.Add("x-envoy-upstream-service-time");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,10 @@ public virtual CopyModelOperation StartCopyModel(string modelId, CopyAuthorizati
target.Region,
new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.ExpiresOn/*.ToUnixTimeSeconds()*/));

ResponseWithHeaders<ServiceCopyCustomModelHeaders> response = ServiceClient.CopyCustomModel(guid, request, cancellationToken);
return new CopyModelOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation, target.ModelId);
Response response = ServiceClient.CopyCustomModel(guid, request, cancellationToken);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.LocationHeader);

return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId);
}

/// <summary>
Expand All @@ -340,8 +342,11 @@ public virtual async Task<CopyModelOperation> StartCopyModelAsync(string modelId
var request = new CopyRequest(target.ResourceId,
target.Region,
new CopyAuthorizationResult(target.ModelId, target.AccessToken, target.ExpiresOn/*.ToUnixTimeSeconds()*/));
ResponseWithHeaders<ServiceCopyCustomModelHeaders> response = await ServiceClient.CopyCustomModelAsync(guid, request, cancellationToken).ConfigureAwait(false);
return new CopyModelOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation, target.ModelId);

Response response = await ServiceClient.CopyCustomModelAsync(guid, request, cancellationToken).ConfigureAwait(false);
string location = ClientCommon.GetResponseHeader(response.Headers, Constants.LocationHeader);

return new CopyModelOperation(ServiceClient, Diagnostics, location, target.ModelId);
}

/// <summary>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading