Skip to content

Commit ae372e9

Browse files
authored
Add FormRecognizerAudience and DocumentAnalysisAudience to Azure.AI.FormRecognizer (Azure#26741)
1 parent 3e98b7e commit ae372e9

File tree

11 files changed

+190
-6
lines changed

11 files changed

+190
-6
lines changed

sdk/formrecognizer/Azure.AI.FormRecognizer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Added the `tags` parameter to the `StartCreateComposedModel` methods.
1616

1717
### Bugs Fixed
18+
- FormRecognizerAudience and DocumentAnalysisAudience have been added to allow the user to select the Azure cloud where the resource is located. Issue [17192](https://github.com/azure/azure-sdk-for-net/issues/17192).
1819

1920
### Other Changes
2021

sdk/formrecognizer/Azure.AI.FormRecognizer/api/Azure.AI.FormRecognizer.netstandard2.0.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ public enum FormReadingOrder
1414
Basic = 0,
1515
Natural = 1,
1616
}
17+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
18+
public readonly partial struct FormRecognizerAudience : System.IEquatable<Azure.AI.FormRecognizer.FormRecognizerAudience>
19+
{
20+
private readonly object _dummy;
21+
private readonly int _dummyPrimitive;
22+
public FormRecognizerAudience(string value) { throw null; }
23+
public static Azure.AI.FormRecognizer.FormRecognizerAudience AzureChina { get { throw null; } }
24+
public static Azure.AI.FormRecognizer.FormRecognizerAudience AzureGovernment { get { throw null; } }
25+
public static Azure.AI.FormRecognizer.FormRecognizerAudience AzurePublicCloud { get { throw null; } }
26+
public bool Equals(Azure.AI.FormRecognizer.FormRecognizerAudience other) { throw null; }
27+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
28+
public override bool Equals(object obj) { throw null; }
29+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
30+
public override int GetHashCode() { throw null; }
31+
public static bool operator ==(Azure.AI.FormRecognizer.FormRecognizerAudience left, Azure.AI.FormRecognizer.FormRecognizerAudience right) { throw null; }
32+
public static implicit operator Azure.AI.FormRecognizer.FormRecognizerAudience (string value) { throw null; }
33+
public static bool operator !=(Azure.AI.FormRecognizer.FormRecognizerAudience left, Azure.AI.FormRecognizer.FormRecognizerAudience right) { throw null; }
34+
public override string ToString() { throw null; }
35+
}
1736
public partial class FormRecognizerClient
1837
{
1938
protected FormRecognizerClient() { }
@@ -49,6 +68,7 @@ public FormRecognizerClient(System.Uri endpoint, Azure.Core.TokenCredential cred
4968
public partial class FormRecognizerClientOptions : Azure.Core.ClientOptions
5069
{
5170
public FormRecognizerClientOptions(Azure.AI.FormRecognizer.FormRecognizerClientOptions.ServiceVersion version = Azure.AI.FormRecognizer.FormRecognizerClientOptions.ServiceVersion.V2_1) { }
71+
public Azure.AI.FormRecognizer.FormRecognizerAudience? Audience { get { throw null; } set { } }
5272
public Azure.AI.FormRecognizer.FormRecognizerClientOptions.ServiceVersion Version { get { throw null; } }
5373
public enum ServiceVersion
5474
{
@@ -349,6 +369,25 @@ internal DocTypeInfo() { }
349369
public System.Collections.Generic.IReadOnlyDictionary<string, float> FieldConfidence { get { throw null; } }
350370
public System.Collections.Generic.IReadOnlyDictionary<string, Azure.AI.FormRecognizer.DocumentAnalysis.DocumentFieldSchema> FieldSchema { get { throw null; } }
351371
}
372+
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
373+
public readonly partial struct DocumentAnalysisAudience : System.IEquatable<Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience>
374+
{
375+
private readonly object _dummy;
376+
private readonly int _dummyPrimitive;
377+
public DocumentAnalysisAudience(string value) { throw null; }
378+
public static Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience AzureChina { get { throw null; } }
379+
public static Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience AzureGovernment { get { throw null; } }
380+
public static Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience AzurePublicCloud { get { throw null; } }
381+
public bool Equals(Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience other) { throw null; }
382+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
383+
public override bool Equals(object obj) { throw null; }
384+
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
385+
public override int GetHashCode() { throw null; }
386+
public static bool operator ==(Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience left, Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience right) { throw null; }
387+
public static implicit operator Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience (string value) { throw null; }
388+
public static bool operator !=(Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience left, Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience right) { throw null; }
389+
public override string ToString() { throw null; }
390+
}
352391
public partial class DocumentAnalysisClient
353392
{
354393
protected DocumentAnalysisClient() { }
@@ -364,6 +403,7 @@ public DocumentAnalysisClient(System.Uri endpoint, Azure.Core.TokenCredential cr
364403
public partial class DocumentAnalysisClientOptions : Azure.Core.ClientOptions
365404
{
366405
public DocumentAnalysisClientOptions(Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisClientOptions.ServiceVersion version = Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisClientOptions.ServiceVersion.V2022_01_30_preview) { }
406+
public Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisAudience? Audience { get { throw null; } set { } }
367407
public Azure.AI.FormRecognizer.DocumentAnalysis.DocumentAnalysisClientOptions.ServiceVersion Version { get { throw null; } }
368408
public enum ServiceVersion
369409
{

sdk/formrecognizer/Azure.AI.FormRecognizer/src/Constants.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ internal static class Constants
1111

1212
public const string OperationLocationHeader = "Operation-Location";
1313

14-
public const string DefaultCognitiveScope = "https://cognitiveservices.azure.com/.default";
15-
1614
public const float DefaultConfidenceValue = 1.0f;
1715

1816
public static readonly StringIndexType DefaultStringIndexType = StringIndexType.Utf16CodeUnit;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.ComponentModel;
6+
using Azure.Core;
7+
8+
namespace Azure.AI.FormRecognizer.DocumentAnalysis
9+
{
10+
/// <summary> Cloud audiences available for FormRecognizer.DocumentAnalysis. </summary>
11+
public readonly partial struct DocumentAnalysisAudience : IEquatable<DocumentAnalysisAudience>
12+
{
13+
private readonly string _value;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="DocumentAnalysisAudience"/> object.
17+
/// </summary>
18+
/// <param name="value">The Azure Active Directory audience to use when forming authorization scopes. For FormRecognizer.DocumentAnalysis, this value corresponds to a URL that identifies the Azure cloud where the resource is located. For more information: <see href="https://docs.microsoft.com/azure/azure-government/documentation-government-cognitiveservices" />.</param>
19+
/// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception>
20+
/// <exception cref="ArgumentException"> <paramref name="value"/> is empty. </exception>
21+
/// <remarks>Please use one of the constant members over creating a custom value unless you have special needs for doing so.</remarks>
22+
public DocumentAnalysisAudience(string value)
23+
{
24+
Argument.AssertNotNullOrEmpty(value, nameof(value));
25+
_value = value;
26+
}
27+
28+
private const string AzureChinaValue = "https://cognitiveservices.azure.cn";
29+
private const string AzureGovernmentValue = "https://cognitiveservices.azure.us";
30+
private const string AzurePublicCloudValue = "https://cognitiveservices.azure.com";
31+
32+
/// <summary> Azure China. </summary>
33+
public static DocumentAnalysisAudience AzureChina { get; } = new DocumentAnalysisAudience(AzureChinaValue);
34+
35+
/// <summary> Azure Government. </summary>
36+
public static DocumentAnalysisAudience AzureGovernment { get; } = new DocumentAnalysisAudience(AzureGovernmentValue);
37+
38+
/// <summary> Azure Public Cloud. </summary>
39+
public static DocumentAnalysisAudience AzurePublicCloud { get; } = new DocumentAnalysisAudience(AzurePublicCloudValue);
40+
41+
/// <summary> Determines if two <see cref="DocumentAnalysisAudience"/> values are the same. </summary>
42+
public static bool operator ==(DocumentAnalysisAudience left, DocumentAnalysisAudience right) => left.Equals(right);
43+
/// <summary> Determines if two <see cref="DocumentAnalysisAudience"/> values are not the same. </summary>
44+
public static bool operator !=(DocumentAnalysisAudience left, DocumentAnalysisAudience right) => !left.Equals(right);
45+
/// <summary> Converts a string to a <see cref="DocumentAnalysisAudience"/>. </summary>
46+
public static implicit operator DocumentAnalysisAudience(string value) => new DocumentAnalysisAudience(value);
47+
48+
/// <inheritdoc />
49+
[EditorBrowsable(EditorBrowsableState.Never)]
50+
public override bool Equals(object obj) => obj is DocumentAnalysisAudience other && Equals(other);
51+
/// <inheritdoc />
52+
public bool Equals(DocumentAnalysisAudience other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);
53+
54+
/// <inheritdoc />
55+
[EditorBrowsable(EditorBrowsableState.Never)]
56+
public override int GetHashCode() => _value?.GetHashCode() ?? 0;
57+
/// <inheritdoc />
58+
public override string ToString() => _value;
59+
}
60+
}

sdk/formrecognizer/Azure.AI.FormRecognizer/src/DocumentAnalysisClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ public DocumentAnalysisClient(Uri endpoint, TokenCredential credential, Document
9797

9898
options ??= new DocumentAnalysisClientOptions();
9999

100+
string defaultScope = $"{(string.IsNullOrEmpty(options.Audience?.ToString()) ? DocumentAnalysisAudience.AzurePublicCloud : options.Audience)}/.default";
101+
100102
Diagnostics = new ClientDiagnostics(options);
101-
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, Constants.DefaultCognitiveScope));
103+
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, defaultScope));
102104
ServiceClient = new DocumentAnalysisRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri);
103105
}
104106

sdk/formrecognizer/Azure.AI.FormRecognizer/src/DocumentAnalysisClientOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public DocumentAnalysisClientOptions(ServiceVersion version = LatestVersion)
2828
};
2929

3030
AddLoggedHeadersAndQueryParameters();
31+
32+
//Default Audience to Azure Public Cloud
33+
Audience ??= DocumentAnalysisAudience.AzurePublicCloud;
3134
}
3235

3336
/// <summary>
@@ -45,6 +48,12 @@ public enum ServiceVersion
4548
#pragma warning restore CA1707 // Identifiers should not contain underscores
4649
}
4750

51+
/// <summary>
52+
/// Gets or sets the Audience to use for authentication with Azure Active Directory (AAD). The audience is not considered when using a shared key.
53+
/// </summary>
54+
/// <value>If <c>null</c>, <see cref="DocumentAnalysisAudience.AzurePublicCloud" /> will be assumed.</value>
55+
public DocumentAnalysisAudience? Audience { get; set; }
56+
4857
/// <summary>
4958
/// The service version.
5059
/// </summary>

sdk/formrecognizer/Azure.AI.FormRecognizer/src/DocumentModelAdministrationClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ public DocumentModelAdministrationClient(Uri endpoint, TokenCredential credentia
103103
Argument.AssertNotNull(credential, nameof(credential));
104104

105105
options ??= new DocumentAnalysisClientOptions();
106+
string defaultScope = $"{(string.IsNullOrEmpty(options.Audience?.ToString()) ? DocumentAnalysisAudience.AzurePublicCloud : options.Audience)}/.default";
106107

107108
Diagnostics = new ClientDiagnostics(options);
108-
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, Constants.DefaultCognitiveScope));
109+
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, defaultScope));
109110
ServiceClient = new DocumentAnalysisRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri);
110111
}
111112

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.ComponentModel;
6+
using Azure.Core;
7+
8+
namespace Azure.AI.FormRecognizer
9+
{
10+
/// <summary> Cloud audiences available for FormRecognizer. </summary>
11+
public readonly partial struct FormRecognizerAudience : IEquatable<FormRecognizerAudience>
12+
{
13+
private readonly string _value;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="FormRecognizerAudience"/> object.
17+
/// </summary>
18+
/// <param name="value">The Azure Active Directory audience to use when forming authorization scopes. For FormRecognizer, this value corresponds to a URL that identifies the Azure cloud where the resource is located. For more information: <see href="https://docs.microsoft.com/azure/azure-government/documentation-government-cognitiveservices" />.</param>
19+
/// <exception cref="ArgumentNullException"> <paramref name="value"/> is null. </exception>
20+
/// <exception cref="ArgumentException"> <paramref name="value"/> is empty. </exception>
21+
/// <remarks>Please use one of the constant members over creating a custom value unless you have special needs for doing so.</remarks>
22+
public FormRecognizerAudience(string value)
23+
{
24+
Argument.AssertNotNullOrEmpty(value, nameof(value));
25+
_value = value;
26+
}
27+
28+
private const string AzureChinaValue = "https://cognitiveservices.azure.cn";
29+
private const string AzureGovernmentValue = "https://cognitiveservices.azure.us";
30+
private const string AzurePublicCloudValue = "https://cognitiveservices.azure.com";
31+
32+
/// <summary> Azure China. </summary>
33+
public static FormRecognizerAudience AzureChina { get; } = new FormRecognizerAudience(AzureChinaValue);
34+
35+
/// <summary> Azure Government. </summary>
36+
public static FormRecognizerAudience AzureGovernment { get; } = new FormRecognizerAudience(AzureGovernmentValue);
37+
38+
/// <summary> Azure Public Cloud. </summary>
39+
public static FormRecognizerAudience AzurePublicCloud { get; } = new FormRecognizerAudience(AzurePublicCloudValue);
40+
41+
/// <summary> Determines if two <see cref="FormRecognizerAudience"/> values are the same. </summary>
42+
public static bool operator ==(FormRecognizerAudience left, FormRecognizerAudience right) => left.Equals(right);
43+
/// <summary> Determines if two <see cref="FormRecognizerAudience"/> values are not the same. </summary>
44+
public static bool operator !=(FormRecognizerAudience left, FormRecognizerAudience right) => !left.Equals(right);
45+
/// <summary> Converts a string to a <see cref="FormRecognizerAudience"/>. </summary>
46+
public static implicit operator FormRecognizerAudience(string value) => new FormRecognizerAudience(value);
47+
48+
/// <inheritdoc />
49+
[EditorBrowsable(EditorBrowsableState.Never)]
50+
public override bool Equals(object obj) => obj is FormRecognizerAudience other && Equals(other);
51+
/// <inheritdoc />
52+
public bool Equals(FormRecognizerAudience other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);
53+
54+
/// <inheritdoc />
55+
[EditorBrowsable(EditorBrowsableState.Never)]
56+
public override int GetHashCode() => _value?.GetHashCode() ?? 0;
57+
/// <inheritdoc />
58+
public override string ToString() => _value;
59+
}
60+
}

sdk/formrecognizer/Azure.AI.FormRecognizer/src/FormRecognizerClient/FormRecognizerClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public FormRecognizerClient(Uri endpoint, TokenCredential credential, FormRecogn
9696
Argument.AssertNotNull(credential, nameof(credential));
9797
Argument.AssertNotNull(options, nameof(options));
9898

99+
string defaultScope = $"{(string.IsNullOrEmpty(options.Audience?.ToString()) ? FormRecognizerAudience.AzurePublicCloud : options.Audience)}/.default";
100+
99101
Diagnostics = new ClientDiagnostics(options);
100-
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, Constants.DefaultCognitiveScope));
102+
var pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, defaultScope));
101103
ServiceClient = new FormRecognizerRestClient(Diagnostics, pipeline, endpoint.AbsoluteUri, FormRecognizerClientOptions.GetVersionString(options.Version));
102104
}
103105

sdk/formrecognizer/Azure.AI.FormRecognizer/src/FormRecognizerClient/FormRecognizerClientOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public class FormRecognizerClientOptions : ClientOptions
1515
{
1616
internal const ServiceVersion LatestVersion = ServiceVersion.V2_1;
1717

18+
/// <summary>
19+
/// Gets or sets the Audience to use for authentication with Azure Active Directory (AAD). The audience is not considered when using a shared key.
20+
/// </summary>
21+
/// <value>If <c>null</c>, <see cref="FormRecognizerAudience.AzurePublicCloud" /> will be assumed.</value>
22+
public FormRecognizerAudience? Audience { get; set; }
23+
1824
/// <summary>
1925
/// Initializes a new instance of the <see cref="FormRecognizerClientOptions"/> class which allows
2026
/// to configure the behavior of the <see cref="FormRecognizerClient" /> or <see cref="FormTrainingClient"/>.
@@ -24,6 +30,9 @@ public FormRecognizerClientOptions(ServiceVersion version = LatestVersion)
2430
{
2531
Version = version;
2632
AddLoggedHeadersAndQueryParameters();
33+
34+
//Default Audience to Azure Public Cloud
35+
Audience ??= FormRecognizerAudience.AzurePublicCloud;
2736
}
2837

2938
/// <summary>

0 commit comments

Comments
 (0)