11package com .microsoft .graph .core .requests ;
22
3+ import com .azure .core .credential .TokenCredential ;
34import com .microsoft .graph .core .CoreConstants ;
5+ import com .microsoft .graph .core .authentication .AzureIdentityAccessTokenProvider ;
46import com .microsoft .graph .core .requests .middleware .GraphTelemetryHandler ;
57import com .microsoft .graph .core .requests .options .GraphClientOption ;
8+ import com .microsoft .kiota .RequestOption ;
69import com .microsoft .kiota .authentication .BaseBearerTokenAuthenticationProvider ;
710import com .microsoft .kiota .http .KiotaClientFactory ;
811import com .microsoft .kiota .http .middleware .AuthorizationHandler ;
@@ -36,7 +39,7 @@ public static OkHttpClient.Builder create() {
3639 * @return an OkHttpClient Builder instance.
3740 */
3841 @ Nonnull
39- public static OkHttpClient .Builder create (@ Nonnull Interceptor ... interceptors ) {
42+ public static OkHttpClient .Builder create (@ Nonnull final Interceptor ... interceptors ) {
4043 return create (new GraphClientOption (), interceptors );
4144 }
4245
@@ -46,20 +49,54 @@ public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors)
4649 * @return an OkHttpClient Builder instance.
4750 */
4851 @ Nonnull
49- public static OkHttpClient .Builder create (@ Nonnull List <Interceptor > interceptors ) {
52+ public static OkHttpClient .Builder create (@ Nonnull final List <Interceptor > interceptors ) {
5053 return create (new GraphClientOption (), interceptors .toArray (new Interceptor [0 ]));
5154 }
5255
56+ /**
57+ * OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential.
58+ * @param tokenCredential the TokenCredential to use for authentication.
59+ * @return an OkHttpClient Builder instance.
60+ */
61+ @ Nonnull
62+ public static OkHttpClient .Builder create (@ Nonnull final TokenCredential tokenCredential ) {
63+ return create (tokenCredential , new RequestOption [0 ]);
64+ }
65+
66+ /**
67+ * OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential and RequestOptions to override default graph interceptors.
68+ * @param tokenCredential the TokenCredential to use for authentication.
69+ * @param requestOptions custom request options to override default graph interceptors
70+ * @return an OkHttpClient Builder instance.
71+ */
72+ @ Nonnull
73+ public static OkHttpClient .Builder create (@ Nonnull final TokenCredential tokenCredential , @ Nonnull final RequestOption [] requestOptions ) {
74+ return create (new BaseBearerTokenAuthenticationProvider (new AzureIdentityAccessTokenProvider (tokenCredential )), requestOptions );
75+ }
76+
5377 /**
5478 * OkHttpClient Builder for Graph with specified AuthenticationProvider.
5579 * Adds an AuthorizationHandler to the OkHttpClient Builder.
5680 * @param authenticationProvider the AuthenticationProvider to use for requests.
5781 * @return an OkHttpClient Builder instance.
5882 */
5983 @ Nonnull
60- public static OkHttpClient .Builder create (@ Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider ) {
84+ public static OkHttpClient .Builder create (@ Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider ) {
85+ return create (authenticationProvider , new RequestOption [0 ]);
86+ }
87+
88+ /**
89+ * OkHttpClient Builder for Graph with specified AuthenticationProvider and RequestOptions to override default graph interceptors
90+ * @param authenticationProvider the AuthenticationProvider to use for requests.
91+ * @param requestOptions custom request options to override default graph interceptors
92+ * @return an OkHttpClient Builder instance.
93+ */
94+ @ Nonnull
95+ public static OkHttpClient .Builder create (@ Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider , @ Nonnull final RequestOption [] requestOptions ) {
6196 final GraphClientOption graphClientOption = new GraphClientOption ();
62- final Interceptor [] interceptors = createDefaultGraphInterceptors (graphClientOption );
97+ final List <RequestOption > requestOptionsList = new ArrayList <>(Arrays .asList (requestOptions ));
98+ requestOptionsList .add (graphClientOption );
99+ final Interceptor [] interceptors = createDefaultGraphInterceptors (requestOptionsList .toArray (new RequestOption [0 ]));
63100 final ArrayList <Interceptor > interceptorList = new ArrayList <>(Arrays .asList (interceptors ));
64101 interceptorList .add (new AuthorizationHandler (authenticationProvider ));
65102 graphClientOption .featureTracker .setFeatureUsage (FeatureFlag .AUTH_HANDLER_FLAG );
@@ -74,18 +111,12 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
74111 * @return an OkHttpClient Builder instance.
75112 */
76113 @ Nonnull
77- public static OkHttpClient .Builder create (@ Nonnull GraphClientOption graphClientOption , @ Nonnull Interceptor ... interceptors ) {
78- final OkHttpClient .Builder builder = create (graphClientOption );
79- //Skip adding interceptor if that class of interceptor already exist.
80- final List <String > appliedInterceptors = new ArrayList <>();
81- for (Interceptor interceptor : builder .interceptors ()) {
82- appliedInterceptors .add (interceptor .getClass ().toString ());
83- }
84- for (Interceptor interceptor :interceptors ){
85- if (appliedInterceptors .contains (interceptor .getClass ().toString ())) {
86- continue ;
87- }
88- builder .addInterceptor (interceptor );
114+ public static OkHttpClient .Builder create (@ Nonnull final GraphClientOption graphClientOption , @ Nonnull final Interceptor ... interceptors ) {
115+ final OkHttpClient .Builder builder = KiotaClientFactory .create (interceptors );
116+ final List <Interceptor > customInterceptors = builder .interceptors ();
117+ final boolean telemetryHandlerExists = customInterceptors .stream ().anyMatch (x -> x instanceof GraphTelemetryHandler );
118+ if (!telemetryHandlerExists ) {
119+ customInterceptors .add (new GraphTelemetryHandler (graphClientOption ));
89120 }
90121 return builder ;
91122 }
@@ -97,7 +128,7 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
97128 * @return an OkHttpClient Builder instance.
98129 */
99130 @ Nonnull
100- public static OkHttpClient .Builder create (@ Nonnull GraphClientOption graphClientOption , @ Nonnull List <Interceptor > interceptors ) {
131+ public static OkHttpClient .Builder create (@ Nonnull final GraphClientOption graphClientOption , @ Nonnull final List <Interceptor > interceptors ) {
101132 return create (graphClientOption , interceptors .toArray (new Interceptor [0 ]));
102133 }
103134
@@ -108,26 +139,68 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
108139 * @return an OkHttpClient Builder instance.
109140 */
110141 @ Nonnull
111- public static OkHttpClient .Builder create (@ Nullable GraphClientOption graphClientOption ) {
112- GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption ();
113- return KiotaClientFactory .create (createDefaultGraphInterceptors (options ));
142+ public static OkHttpClient .Builder create (@ Nullable final GraphClientOption graphClientOption ) {
143+ GraphClientOption option = graphClientOption == null ? new GraphClientOption () : graphClientOption ;
144+ return KiotaClientFactory .create (createDefaultGraphInterceptors (option ));
114145 }
146+
147+ /**
148+ * The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors
149+ * @param requestOptions custom request options to override default graph interceptors
150+ * @return an OkHttpClient Builder instance.
151+ */
152+ @ Nonnull
153+ public static OkHttpClient .Builder create (@ Nonnull final RequestOption [] requestOptions ) {
154+ return KiotaClientFactory .create (createDefaultGraphInterceptors (requestOptions ));
155+ }
156+
115157 /**
116158 * Creates the default Interceptors for use with Graph.
117159 *
118160 * @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with.
119161 * @return an array of interceptors.
120162 */
121163 @ Nonnull
122- public static Interceptor [] createDefaultGraphInterceptors (@ Nonnull GraphClientOption graphClientOption ) {
123- List < Interceptor > handlers = new ArrayList <>( );
124- addDefaultFeatureUsages ( graphClientOption );
164+ public static Interceptor [] createDefaultGraphInterceptors (@ Nonnull final GraphClientOption graphClientOption ) {
165+ return getDefaultGraphInterceptors ( new RequestOption []{ graphClientOption }). toArray ( new Interceptor [ 0 ] );
166+ }
125167
126- handlers .add (new UrlReplaceHandler (new UrlReplaceHandlerOption (CoreConstants .ReplacementConstants .getDefaultReplacementPairs ())));
168+ /**
169+ * Creates the default Interceptors for use with Graph configured with the provided RequestOptions.
170+ * @param requestOptions custom request options to override default graph interceptors
171+ * @return an array of interceptors.
172+ */
173+ @ Nonnull
174+ public static Interceptor [] createDefaultGraphInterceptors (@ Nonnull final RequestOption [] requestOptions ) {
175+ Objects .requireNonNull (requestOptions , "parameter requestOptions cannot be null" );
176+ return getDefaultGraphInterceptors (requestOptions ).toArray (new Interceptor [0 ]);
177+ }
178+
179+ /**
180+ * Creates the default Interceptors for use with Graph.
181+ * @param requestOptions custom request options to override default graph interceptors
182+ * @return a list of interceptors.
183+ */
184+ private static List <Interceptor > getDefaultGraphInterceptors (@ Nonnull final RequestOption [] requestOptions ) {
185+ GraphClientOption graphClientOption = new GraphClientOption ();
186+ UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption (CoreConstants .ReplacementConstants .getDefaultReplacementPairs ());
187+ for (RequestOption option : requestOptions ) {
188+ if (option instanceof UrlReplaceHandlerOption ) {
189+ urlReplaceHandlerOption = (UrlReplaceHandlerOption ) option ;
190+ }
191+ if (option instanceof GraphClientOption ) {
192+ graphClientOption = (GraphClientOption ) option ;
193+ }
194+ }
195+
196+ List <Interceptor > handlers = new ArrayList <>();
197+ handlers .add (new UrlReplaceHandler (urlReplaceHandlerOption ));
127198 handlers .add (new GraphTelemetryHandler (graphClientOption ));
128- handlers .addAll (Arrays .asList (KiotaClientFactory .createDefaultInterceptors ()));
129- return handlers .toArray (new Interceptor [0 ]);
199+ handlers .addAll (Arrays .asList (KiotaClientFactory .createDefaultInterceptors (requestOptions )));
200+ addDefaultFeatureUsages (graphClientOption );
201+ return handlers ;
130202 }
203+
131204 //These are the default features used by the Graph Client
132205 private static void addDefaultFeatureUsages (GraphClientOption graphClientOption ) {
133206 graphClientOption .featureTracker .setFeatureUsage (FeatureFlag .RETRY_HANDLER_FLAG );
0 commit comments