Skip to content

Commit

Permalink
feat: support authorization handler in middleware pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Sep 30, 2024
1 parent a9c01ec commit e0b5675
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.microsoft.graph.core.CoreConstants;
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
import com.microsoft.graph.core.requests.options.GraphClientOption;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.KiotaClientFactory;
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
import com.microsoft.kiota.http.middleware.UrlReplaceHandler;
import com.microsoft.kiota.http.middleware.options.UrlReplaceHandlerOption;
import okhttp3.Interceptor;
Expand Down Expand Up @@ -37,6 +39,33 @@ public static OkHttpClient.Builder create() {
public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors) {
return create(new GraphClientOption(), interceptors);
}

/**
* OkHttpClient Builder for Graph with specified Interceptors
* @param interceptors desired interceptors for use in requests.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull List<Interceptor> interceptors) {
return create(new GraphClientOption(), interceptors.toArray(new Interceptor[0]));
}

/**
* OkHttpClient Builder for Graph with specified AuthenticationProvider.
* Adds an AuthorizationHandler to the OkHttpClient Builder.
* @param authenticationProvider the AuthenticationProvider to use for requests.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) {
GraphClientOption graphClientOption = new GraphClientOption();
Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption);
ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
interceptorList.add(new AuthorizationHandler(authenticationProvider));
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
return create(graphClientOption, interceptorList);
}

/**
* OkHttpClient Builder for Graph with specified Interceptors and GraphClientOption.
*
Expand All @@ -60,6 +89,17 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
}
return builder;
}

/**
* OkHttpClient Builder for Graph with specified Interceptors and GraphClientOption.
* @param graphClientOption the GraphClientOption for use in requests.
* @param interceptors desired interceptors for use in requests.
* @return an OkHttpClient Builder instance.
*/
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull List<Interceptor> interceptors) {
return create(graphClientOption, interceptors.toArray(new Interceptor[0]));
}

/**
* The OkHttpClient Builder with optional GraphClientOption
*
Expand Down Expand Up @@ -92,6 +132,5 @@ private static void addDefaultFeatureUsages(GraphClientOption graphClientOption)
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.RETRY_HANDLER_FLAG);
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.REDIRECT_HANDLER_FLAG);
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.URL_REPLACEMENT_FLAG);
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.BATCH_REQUEST_FLAG);
}
}

0 comments on commit e0b5675

Please sign in to comment.