33import com .microsoft .graph .core .CoreConstants ;
44import com .microsoft .graph .core .requests .middleware .GraphTelemetryHandler ;
55import com .microsoft .graph .core .requests .options .GraphClientOption ;
6+ import com .microsoft .kiota .RequestOption ;
67import com .microsoft .kiota .authentication .BaseBearerTokenAuthenticationProvider ;
78import com .microsoft .kiota .http .KiotaClientFactory ;
89import com .microsoft .kiota .http .middleware .AuthorizationHandler ;
@@ -58,8 +59,13 @@ public static OkHttpClient.Builder create(@Nonnull List<Interceptor> interceptor
5859 */
5960 @ Nonnull
6061 public static OkHttpClient .Builder create (@ Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider ) {
62+ return create (authenticationProvider , new RequestOption [0 ]);
63+ }
64+
65+ @ Nonnull
66+ public static OkHttpClient .Builder create (@ Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider , @ Nonnull RequestOption [] requestOptions ) {
6167 final GraphClientOption graphClientOption = new GraphClientOption ();
62- final Interceptor [] interceptors = createDefaultGraphInterceptors (graphClientOption );
68+ final Interceptor [] interceptors = createDefaultGraphInterceptors (graphClientOption , requestOptions );
6369 final ArrayList <Interceptor > interceptorList = new ArrayList <>(Arrays .asList (interceptors ));
6470 interceptorList .add (new AuthorizationHandler (authenticationProvider ));
6571 graphClientOption .featureTracker .setFeatureUsage (FeatureFlag .AUTH_HANDLER_FLAG );
@@ -75,7 +81,13 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
7581 */
7682 @ Nonnull
7783 public static OkHttpClient .Builder create (@ Nonnull GraphClientOption graphClientOption , @ Nonnull Interceptor ... interceptors ) {
78- return KiotaClientFactory .create (interceptors );
84+ var builder = KiotaClientFactory .create (interceptors );
85+ var customInterceptors = builder .interceptors ();
86+ var telemetryHandlerExists = customInterceptors .stream ().anyMatch (x -> x instanceof GraphTelemetryHandler );
87+ if (!telemetryHandlerExists ) {
88+ customInterceptors .add (new GraphTelemetryHandler (graphClientOption ));
89+ }
90+ return builder ;
7991 }
8092
8193 /**
@@ -97,9 +109,15 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
97109 */
98110 @ Nonnull
99111 public static OkHttpClient .Builder create (@ Nullable GraphClientOption graphClientOption ) {
112+ return create (graphClientOption , new RequestOption [0 ]);
113+ }
114+
115+ @ Nonnull
116+ public static OkHttpClient .Builder create (@ Nullable GraphClientOption graphClientOption , @ Nonnull RequestOption [] requestOptions ) {
100117 GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption ();
101- return KiotaClientFactory .create (createDefaultGraphInterceptors (options ));
118+ return KiotaClientFactory .create (createDefaultGraphInterceptors (options , requestOptions ));
102119 }
120+
103121 /**
104122 * Creates the default Interceptors for use with Graph.
105123 *
@@ -108,14 +126,31 @@ public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClien
108126 */
109127 @ Nonnull
110128 public static Interceptor [] createDefaultGraphInterceptors (@ Nonnull GraphClientOption graphClientOption ) {
111- List <Interceptor > handlers = new ArrayList <>();
112- addDefaultFeatureUsages (graphClientOption );
129+ return createDefaultGraphInterceptors (graphClientOption , new RequestOption [0 ]);
130+ }
131+
132+ @ Nonnull
133+ public static Interceptor [] createDefaultGraphInterceptors (@ Nonnull GraphClientOption graphClientOption , @ Nonnull RequestOption [] requestOptions ) {
134+ Objects .requireNonNull (requestOptions , "parameter requestOptions cannot be null" );
135+
136+ UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption (CoreConstants .ReplacementConstants .getDefaultReplacementPairs ());
113137
114- handlers .add (new UrlReplaceHandler (new UrlReplaceHandlerOption (CoreConstants .ReplacementConstants .getDefaultReplacementPairs ())));
138+ for (RequestOption option : requestOptions ) {
139+ if (option instanceof UrlReplaceHandlerOption ) {
140+ urlReplaceHandlerOption = (UrlReplaceHandlerOption ) option ;
141+ }
142+ }
143+
144+ List <Interceptor > handlers = new ArrayList <>();
145+ handlers .add (urlReplaceHandlerOption == null ?
146+ new UrlReplaceHandler (new UrlReplaceHandlerOption (CoreConstants .ReplacementConstants .getDefaultReplacementPairs ())) :
147+ new UrlReplaceHandler (urlReplaceHandlerOption ));
115148 handlers .add (new GraphTelemetryHandler (graphClientOption ));
116- handlers .addAll (Arrays .asList (KiotaClientFactory .createDefaultInterceptors ()));
149+ handlers .addAll (Arrays .asList (KiotaClientFactory .createDefaultInterceptors (requestOptions )));
150+ addDefaultFeatureUsages (graphClientOption );
117151 return handlers .toArray (new Interceptor [0 ]);
118152 }
153+
119154 //These are the default features used by the Graph Client
120155 private static void addDefaultFeatureUsages (GraphClientOption graphClientOption ) {
121156 graphClientOption .featureTracker .setFeatureUsage (FeatureFlag .RETRY_HANDLER_FLAG );
0 commit comments