4
4
// --------------------------------------------------------------------------
5
5
6
6
using System . Linq ;
7
+ using System . Net ;
7
8
using System . Net . Http ;
8
9
using System . Threading . Tasks ;
9
10
using Microsoft . Azure . Management . ApiManagement . ArmTemplates . Common . API . Clients . Abstractions ;
@@ -16,9 +17,9 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clien
16
17
public class PolicyClient : ApiClientBase , IPolicyClient
17
18
{
18
19
const string GetGlobalServicePolicyRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/policies/policy?api-version={4}&format=rawxml" ;
19
- const string GetPolicyLinkedToProductRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/products/{4}/policies?api-version={5}&format=rawxml" ;
20
- const string GetPolicyLinkedToApiRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/policies?api-version={5}&format=rawxml" ;
21
- const string GetPolicyLinkedToApiOperationRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/operations/{5}/policies?api-version={6}&format=rawxml" ;
20
+ const string GetPolicyLinkedToProductRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/products/{4}/policies/policy ?api-version={5}&format=rawxml" ;
21
+ const string GetPolicyLinkedToApiRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/policies/policy ?api-version={5}&format=rawxml" ;
22
+ const string GetPolicyLinkedToApiOperationRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/operations/{5}/policies/policy ?api-version={6}&format=rawxml" ;
22
23
23
24
public PolicyClient ( IHttpClientFactory httpClientFactory ) : base ( httpClientFactory )
24
25
{
@@ -41,8 +42,7 @@ public async Task<PolicyTemplateResource> GetPolicyLinkedToProductAsync(string p
41
42
string requestUrl = string . Format ( GetPolicyLinkedToProductRequest ,
42
43
this . BaseUrl , azSubId , extractorParameters . ResourceGroup , extractorParameters . SourceApimName , productName , GlobalConstants . ApiVersion ) ;
43
44
44
- var policies = await this . GetPagedResponseAsync < PolicyTemplateResource > ( azToken , requestUrl ) ;
45
- return policies . FirstOrDefault ( ) ;
45
+ return await this . FetchResourcePolicyTemplateAsync ( azToken , requestUrl ) ;
46
46
}
47
47
48
48
public async Task < PolicyTemplateResource > GetPolicyLinkedToApiAsync ( string apiName , ExtractorParameters extractorParameters )
@@ -52,8 +52,7 @@ public async Task<PolicyTemplateResource> GetPolicyLinkedToApiAsync(string apiNa
52
52
string requestUrl = string . Format ( GetPolicyLinkedToApiRequest ,
53
53
this . BaseUrl , azSubId , extractorParameters . ResourceGroup , extractorParameters . SourceApimName , apiName , GlobalConstants . ApiVersion ) ;
54
54
55
- var policies = await this . GetPagedResponseAsync < PolicyTemplateResource > ( azToken , requestUrl ) ;
56
- return policies . FirstOrDefault ( ) ;
55
+ return await this . FetchResourcePolicyTemplateAsync ( azToken , requestUrl ) ;
57
56
}
58
57
59
58
public async Task < PolicyTemplateResource > GetPolicyLinkedToApiOperationAsync ( string apiName , string operationName , ExtractorParameters extractorParameters )
@@ -63,8 +62,25 @@ public async Task<PolicyTemplateResource> GetPolicyLinkedToApiOperationAsync(str
63
62
string requestUrl = string . Format ( GetPolicyLinkedToApiOperationRequest ,
64
63
this . BaseUrl , azSubId , extractorParameters . ResourceGroup , extractorParameters . SourceApimName , apiName , operationName , GlobalConstants . ApiVersion ) ;
65
64
66
- var policies = await this . GetPagedResponseAsync < PolicyTemplateResource > ( azToken , requestUrl ) ;
67
- return policies . FirstOrDefault ( ) ;
65
+ return await this . FetchResourcePolicyTemplateAsync ( azToken , requestUrl ) ;
66
+ }
67
+
68
+ async Task < PolicyTemplateResource > FetchResourcePolicyTemplateAsync ( string azToken , string requestUrl )
69
+ {
70
+ PolicyTemplateResource policy = null ;
71
+ try
72
+ {
73
+ policy = await this . GetResponseAsync < PolicyTemplateResource > ( azToken , requestUrl ) ;
74
+ }
75
+ catch ( HttpRequestException ex )
76
+ {
77
+ // It is expected that entity will not have policy if it was not set after creation
78
+ if ( ex . StatusCode != HttpStatusCode . NotFound )
79
+ {
80
+ throw ;
81
+ }
82
+ }
83
+ return policy ;
68
84
}
69
85
}
70
86
}
0 commit comments