10
10
import com .azure .core .http .HttpHeaders ;
11
11
import com .azure .core .http .HttpPipeline ;
12
12
import com .azure .core .http .HttpPipelineBuilder ;
13
+ import com .azure .core .http .HttpPipelinePosition ;
13
14
import com .azure .core .http .policy .AddDatePolicy ;
14
15
import com .azure .core .http .policy .AddHeadersPolicy ;
15
16
import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
@@ -47,7 +48,8 @@ public final class DigitalTwinsClientBuilder {
47
48
private static final String SDK_NAME = "name" ;
48
49
private static final String SDK_VERSION = "version" ;
49
50
50
- private final List <HttpPipelinePolicy > additionalPolicies ;
51
+ private final List <HttpPipelinePolicy > perCallPolicies = new ArrayList <>();
52
+ private final List <HttpPipelinePolicy > perRetryPolicies = new ArrayList <>();
51
53
52
54
// mandatory
53
55
private String endpoint ;
@@ -79,7 +81,6 @@ public final class DigitalTwinsClientBuilder {
79
81
* The public constructor for DigitalTwinsClientBuilder
80
82
*/
81
83
public DigitalTwinsClientBuilder () {
82
- additionalPolicies = new ArrayList <>();
83
84
properties = CoreUtils .getProperties (DIGITAL_TWINS_PROPERTIES );
84
85
httpLogOptions = new HttpLogOptions ();
85
86
}
@@ -90,7 +91,8 @@ private static HttpPipeline setupPipeline(
90
91
HttpLogOptions httpLogOptions ,
91
92
ClientOptions clientOptions ,
92
93
HttpClient httpClient ,
93
- List <HttpPipelinePolicy > additionalPolicies ,
94
+ List <HttpPipelinePolicy > perCallPolicies ,
95
+ List <HttpPipelinePolicy > perRetryPolicies ,
94
96
RetryPolicy retryPolicy ,
95
97
Configuration configuration ,
96
98
Map <String , String > properties ) {
@@ -111,6 +113,8 @@ private static HttpPipeline setupPipeline(
111
113
// Adds a "x-ms-client-request-id" header to each request. This header is useful for tracing requests through Azure ecosystems
112
114
policies .add (new RequestIdPolicy ());
113
115
116
+ policies .addAll (perCallPolicies );
117
+
114
118
// Only the RequestIdPolicy and UserAgentPolicy will take effect prior to the retry policy since neither of those need
115
119
// to change in any way upon retry
116
120
HttpPolicyProviders .addBeforeRetryPolicies (policies );
@@ -124,7 +128,7 @@ private static HttpPipeline setupPipeline(
124
128
HttpPipelinePolicy credentialPolicy = new BearerTokenAuthenticationPolicy (tokenCredential , ADT_PUBLIC_SCOPE );
125
129
policies .add (credentialPolicy );
126
130
127
- policies .addAll (additionalPolicies );
131
+ policies .addAll (perRetryPolicies );
128
132
129
133
// If client options has headers configured, add a policy for each
130
134
if (clientOptions != null ) {
@@ -135,7 +139,7 @@ private static HttpPipeline setupPipeline(
135
139
}
136
140
137
141
// Custom policies, authentication policy, and add date policy all take place after the retry policy which means
138
- // they will be applied once per http request, and once for every retried http request. For instance , the
142
+ // they will be applied once per http request, and once for every retried http request. For example , the
139
143
// AddDatePolicy will add a date time header for each request that is sent, and if the http request fails
140
144
// and the retry policy dictates that the request should be retried, then the date time header policy will
141
145
// be applied again and the current date time will be put in the header instead of the date time from
@@ -192,7 +196,8 @@ public DigitalTwinsAsyncClient buildAsyncClient() {
192
196
this .httpLogOptions ,
193
197
this .clientOptions ,
194
198
this .httpClient ,
195
- this .additionalPolicies ,
199
+ this .perCallPolicies ,
200
+ this .perRetryPolicies ,
196
201
retryPolicy ,
197
202
buildConfiguration ,
198
203
this .properties );
@@ -273,7 +278,14 @@ public DigitalTwinsClientBuilder httpLogOptions(HttpLogOptions logOptions) {
273
278
* @throws NullPointerException If {@code pipelinePolicy} is {@code null}.
274
279
*/
275
280
public DigitalTwinsClientBuilder addPolicy (HttpPipelinePolicy pipelinePolicy ) {
276
- this .additionalPolicies .add (Objects .requireNonNull (pipelinePolicy , "'pipelinePolicy' cannot be null" ));
281
+ Objects .requireNonNull (pipelinePolicy , "'pipelinePolicy' cannot be null." );
282
+
283
+ if (pipelinePolicy .getPipelinePosition () == HttpPipelinePosition .PER_CALL ) {
284
+ perCallPolicies .add (pipelinePolicy );
285
+ } else {
286
+ perRetryPolicies .add (pipelinePolicy );
287
+ }
288
+
277
289
return this ;
278
290
}
279
291
0 commit comments